toyspp

toyspp.git
git clone git://git.lenczewski.org/toyspp.git
Log | Files | Refs | Submodules | README | LICENSE

test.cpp (3025B)


      1 #include "utility.hpp"
      2 #include "memory.hpp"
      3 
      4 #include <cstdio>
      5 
      6 struct alignas(8) mystruct {
      7 	int a, b, c, d;
      8 };
      9 
     10 int
     11 main(void)
     12 {
     13 	uint8_t *arr0 = Test::Memory::GenAlloc(sizeof(struct mystruct), alignof(struct mystruct));
     14 	assert(arr0);
     15 
     16 	struct mystruct *mystruct0 = (struct mystruct *) arr0;
     17 	mystruct0->a = 42;
     18 
     19 	printf("arr0: %p, mystruct0.a: %d\n", arr0, mystruct0->a);
     20 
     21 	uint8_t *arr1 = Test::Memory::PageAlloc(sizeof(struct mystruct), Test::Memory::MEM_ALIGN_2_MIB);
     22 	assert(arr1);
     23 
     24 	struct mystruct *mystruct1 = (struct mystruct *) arr1;
     25 	mystruct1->a = 69;
     26 
     27 	printf("arr1: %p, mystruct1.a: %d\n",
     28 			arr1, mystruct1->a);
     29 	
     30 	Test::Memory::MirrorAllocResult arr2 = Test::Memory::MirrorAlloc(Test::Memory::MEM_ALIGN_4_KIB, 2);
     31 	assert(arr2.ptr);
     32 
     33 	struct mystruct *mystruct2a = (struct mystruct *) arr2.ptr;
     34 	struct mystruct *mystruct2b = (struct mystruct *) (arr2.ptr + arr2.len);
     35 
     36 	mystruct2a->a = 100;
     37 
     38 	printf("arr2: %p, mystruct2a.a: %d, mystruct2b.a: %d\n",
     39 			arr2.ptr, mystruct2a->a, mystruct2b->a);
     40 
     41 	{
     42 		uint8_t buf[1024];
     43 		Test::Memory::LinearAlloc arena{};
     44 		arena.Init(buf, sizeof buf);
     45 
     46 		arena.Reset();
     47 		struct mystruct *mystruct = arena.Alloc<struct mystruct>(1, 2, 3, 4);
     48 		assert(mystruct);
     49 
     50 		printf("LinearAlloc: mystruct: %p, %d, %d, %d, %d\n",
     51 				mystruct, mystruct->a, mystruct->b, mystruct->c, mystruct->d);
     52 
     53 		struct mystruct *mystruct2 = arena.Alloc<struct mystruct>(0, 0, 0, 0);
     54 		assert(mystruct2);
     55 		assert(mystruct2 != mystruct);
     56 
     57 		printf("LinearAlloc: mystruct2: %p, %d, %d, %d, %d\n",
     58 				mystruct2, mystruct2->a, mystruct2->b, mystruct2->c, mystruct2->d);
     59 	}
     60 
     61 	{
     62 		uint8_t buf[1024];
     63 		Test::Memory::BlockAlloc blocks{};
     64 		blocks.Init(buf, sizeof buf, sizeof(mystruct), alignof(mystruct));
     65 
     66 		struct mystruct *mystruct = blocks.Alloc<struct mystruct>(1, 2, 3, 4);
     67 		assert(mystruct);
     68 
     69 		printf("BlockAlloc: mystruct: %p, %d, %d, %d, %d\n",
     70 				mystruct, mystruct->a, mystruct->b, mystruct->c, mystruct->d);
     71 
     72 		struct mystruct *mystruct2 = blocks.Alloc<struct mystruct>(0, 0, 0, 0);
     73 		assert(mystruct2);
     74 		assert(mystruct2 != mystruct);
     75 
     76 		printf("BlockAlloc: mystruct2: %p, %d, %d, %d, %d\n",
     77 				mystruct2, mystruct2->a, mystruct2->b, mystruct2->c, mystruct2->d);
     78 	}
     79 
     80 	{
     81 		uint8_t buf[1024];
     82 		Test::Memory::StackAlloc stack{};
     83 		stack.Init(buf, sizeof buf);
     84 
     85 		Test::Memory::StackAlloc::Result region0 = stack.AllocRaw(256, 16);
     86 		assert(region0.ptr);
     87 
     88 		printf("StackAlloc: region0: %p\n", region0.ptr);
     89 
     90 		{
     91 			Test::Memory::StackAlloc::Result region1 = stack.AllocRaw(64, 64);
     92 			assert(region1.ptr);
     93 
     94 			printf("StackAlloc: region1: %p\n", region1.ptr);
     95 
     96 			stack.Free(region1);
     97 		}
     98 
     99 		{
    100 			Test::Memory::StackAlloc::Result region2 = stack.AllocRaw(64, 64);
    101 			assert(region2.ptr);
    102 
    103 			printf("StackAlloc: region2: %p\n", region2.ptr);
    104 
    105 		}
    106 
    107 		stack.Free(region0);
    108 
    109 		Test::Memory::StackAlloc::Result region3 = stack.AllocRaw(64, 64);
    110 		assert(region3.ptr);
    111 
    112 		printf("StackAlloc: region3: %p\n", region3.ptr);
    113 	}
    114 
    115 	return 0;
    116 }
    117 
    118 #include "utility.cpp"
    119 #include "memory.cpp"