Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
allocator_tests.cpp
Go to the documentation of this file.
1#include <sysio/vm/allocator.hpp>
2
3#include <catch2/catch.hpp>
4
5using namespace sysio;
6using namespace sysio::vm;
7
8template<typename T>
9bool check_alignment(T* ptr) {
10 void * p = ptr;
11 std::size_t sz = sizeof(T);
12 return std::align(alignof(T), sizeof(T), p, sz) != nullptr;
13}
14
15TEST_CASE("Testing growable_allocator alignment", "[growable_allocator]") {
16 growable_allocator alloc(1024);
17 unsigned char * cptr = alloc.alloc<unsigned char>(1);
19 uint16_t * sptr = alloc.alloc<uint16_t>(1);
21 uint32_t * iptr = alloc.alloc<uint32_t>(1);
23 uint64_t * lptr = alloc.alloc<uint64_t>(1);
25 *cptr = 0x11u;
26 *sptr = 0x2233u;
27 *iptr = 0x44556677u;
28 *lptr = 0x8899102030405060u;
29 CHECK(*cptr == 0x11u);
30 CHECK(*sptr == 0x2233u);
31 CHECK(*iptr == 0x44556677u);
32 CHECK(*lptr == 0x8899102030405060u);
33}
34
35TEST_CASE("Testing maximum single allocation", "[growable_allocator]") {
36 growable_allocator alloc(0);
37 char * ptr = alloc.alloc<char>(0x40000000);
38 ptr[0] = 'a';
39 ptr[0x3FFFFFFF] = 'z';
40 alloc.alloc<char>(0);
41}
42
43
44TEST_CASE("Testing maximum multiple allocation", "[growable_allocator]") {
45 growable_allocator alloc(1024);
46 for(int i = 0; i < 4; ++i) {
47 char * ptr = alloc.alloc<char>(0x10000000);
48 ptr[0] = 'a';
49 ptr[0x0FFFFFFF] = 'z';
50 }
51 alloc.alloc<char>(0);
52}
53
54TEST_CASE("Testing too large single allocation", "[growable_allocator]") {
55 growable_allocator alloc(1024);
56 CHECK_THROWS_AS(alloc.alloc<char>(0x40000001), wasm_bad_alloc);
57}
58
59TEST_CASE("Testing too large multiple allocation", "[growable_allocator]") {
60 growable_allocator alloc(1024);
61 alloc.alloc<char>(0x10000000);
62 alloc.alloc<char>(0x10000000);
63 alloc.alloc<char>(0x10000000);
64 CHECK_THROWS_AS(alloc.alloc<char>(0x10000001), wasm_bad_alloc);
65}
66
67TEST_CASE("Testing maximum initial size", "[growable_allocator]") {
68 growable_allocator alloc(0x40000000);
69 char * ptr = alloc.alloc<char>(0x40000000);
70 ptr[0] = 'a';
71 ptr[0x3FFFFFFF] = 'z';
72}
73
74TEST_CASE("Testing too large initial size", "[growable_allocator]") {
75 CHECK_THROWS_AS(growable_allocator{0x40000001}, wasm_bad_alloc);
76 // Check that integer overflow in rounding functions won't cause issues
77 CHECK_THROWS_AS(growable_allocator{0x8000000000000000ull}, wasm_bad_alloc);
78 CHECK_THROWS_AS(growable_allocator{0xFFFFFFFFFFFE0001ull}, wasm_bad_alloc);
79 CHECK_THROWS_AS(growable_allocator{0xFFFFFFFFFFFFFFFFull}, wasm_bad_alloc);
80}
81
82TEST_CASE("Testing maximum aligned allocation", "[growable_allocator]") {
83 growable_allocator alloc(1024);
84 struct alignas(8) aligned_t { char a[8]; };
85 alloc.alloc<char>(0x3FFFFFF4);
86 aligned_t * ptr = alloc.alloc<aligned_t>(1);
87 ptr->a[0] = 'a';
88 ptr->a[7] = 'z';
89 alloc.alloc<aligned_t>(0);
90 CHECK_THROWS_AS(alloc.alloc<char>(1), wasm_bad_alloc);
91}
92
93TEST_CASE("Testing reclaim", "[growable_allocator]") {
94 growable_allocator alloc(1024);
95 int * ptr1 = alloc.alloc<int>(10);
96 alloc.reclaim(ptr1 + 2, 8);
97 int * ptr2 = alloc.alloc<int>(10);
98 CHECK(ptr2 == ptr1 + 2);
99}
bool check_alignment(T *ptr)
const mie::Vuint & p
Definition bn.cpp:27
void reclaim(const T *ptr, size_t size=0)
#define CHECK(cond)
Definition util.h:80
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
#define TEST_CASE(...)
Definition catch.hpp:222
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define T(meth, val, expected)
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136