openMSX
monotonic_allocator_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2
4
5#include "ranges.hh"
6
7#include <bit>
8#include <span>
9
10[[nodiscard]] static char* alloc(monotonic_allocator& a, size_t size, size_t alignment)
11{
12 auto* result = static_cast<char*>(a.allocate(size, alignment));
13 CHECK(result != nullptr);
14 CHECK((std::bit_cast<uintptr_t>(result) & (alignment - 1)) == 0);
15 ranges::fill(std::span{result, size}, 0xab); // this shouldn't crash
16 return result;
17}
18
19TEST_CASE("monotonic_allocator")
20{
21 SECTION("default") {
23 char* p1 = alloc(a, 300, 4);
24
25 char* p2 = alloc(a, 600, 4);
26 CHECK(p1 + 300 == p2); // adjacent
27
28 char* p3 = alloc(a, 600, 4);
29 CHECK(p2 + 600 != p3); // not adjacent, doesn't fit in initial 1kB block
30
31 char* p4 = alloc(a, 600, 4);
32 CHECK(p3 + 600 == p4); // adjacent again because 2nd block is 2kB
33
34 char* p5 = alloc(a, 10'000, 4);
35 CHECK(p4 + 600 != p5); // not adjacent
36 }
37 SECTION("initial size") {
39 char* p1 = alloc(a, 32, 4);
40
41 char* p2 = alloc(a, 32, 4);
42 CHECK(p1 + 32 == p2); // adjacent
43
44 char* p3 = alloc(a, 32, 4);
45 CHECK(p1 + 64 == p3); // adjacent
46
47 char* p4 = alloc(a, 32, 4);
48 CHECK(p1 + 96 != p4); // not adjacent
49 }
50 SECTION("initial buffer") {
51 std::array<char, 100> buf;
52 monotonic_allocator a(buf.data(), buf.size());
53
54 char* p1 = alloc(a, 40, 1);
55 CHECK(p1 == buf.data());
56
57 char* p2 = alloc(a, 40, 1);
58 CHECK(p2 == &buf[40]);
59
60 char* p3 = alloc(a, 40, 1);
61 CHECK(p3 != &buf[80]);
62
63 char* p4 = alloc(a, 40, 1);
64 CHECK(p4 == p3 + 40);
65
66 char* p5 = alloc(a, 40, 1);
67 CHECK(p5 == p3 + 80);
68 }
69 SECTION("alignment") {
71 char* p1 = alloc(a, 13, 1);
72
73 char* p2 = alloc(a, 4, 4);
74 CHECK(p2 == p1 + 16);
75
76 char* p3 = alloc(a, 3, 2);
77 CHECK(p3 == p2 + 4);
78
79 char* p4 = alloc(a, 3, 2);
80 CHECK(p4 == p3 + 4);
81 }
82}
void * allocate(size_t bytes, size_t alignment)
CHECK(m3==m3)
mat4 p4(vec4(1, 2, 3, 4), vec4(3, 4, 5, 6), vec4(5, 0, 7, 8), vec4(7, 8, 9, 0))
mat3 p3(vec3(1, 2, 3), vec3(4, 5, 6), vec3(7, 0, 9))
TEST_CASE("monotonic_allocator")
constexpr void fill(ForwardRange &&range, const T &value)
Definition ranges.hh:315
size_t size(std::string_view utf8)