toys

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

arena.c (2362B)


      1 #define HEADER_IMPL
      2 #include "arena.h"
      3 
      4 #include <stdio.h>
      5 
      6 struct mystruct {
      7 	int a, b, c;
      8 };
      9 
     10 struct alignas(64) mystruct2 {
     11 	char buf[17];
     12 };
     13 
     14 int
     15 main(void)
     16 {
     17 	printf("IS_POW2(1): %d, IS_POW2(2): %d, IS_POW2(3): %d\n",
     18 			IS_POW2(1), IS_POW2(2), IS_POW2(3));
     19 
     20 	printf("IS_ALIGNED(2, 2): %d, IS_ALIGNED(1, 2): %d\n",
     21 			IS_ALIGNED(2, 2), IS_ALIGNED(1, 2));
     22 
     23 	printf("ALIGN_PREV(2, 2): %d, ALIGN_PREV(1, 2): %d\n",
     24 			ALIGN_PREV(2, 2), ALIGN_PREV(1, 2));
     25 
     26 	printf("ALIGN_NEXT(2, 2): %d, ALIGN_NEXT(1, 2): %d\n",
     27 			ALIGN_NEXT(2, 2), ALIGN_NEXT(1, 2));
     28 
     29 	char buf[1024];
     30 	struct arena arena = { .ptr = buf, .cap = sizeof buf, .len = 0, };
     31 
     32 	printf("arena: buf: %p, cap: %zu, len: %zu\n",
     33 			arena.ptr, arena.cap, arena.len);
     34 
     35 	struct mystruct *foo = arena_alloc(&arena, sizeof *foo, alignof(struct mystruct));
     36 	assert(foo);
     37 
     38 	printf("foo: %p, sizeof foo: %zu, alignof foo: %zu\n",
     39 			foo, sizeof *foo, alignof(struct mystruct));
     40 
     41 	foo->a = foo->b = foo->c = 42;
     42 
     43 	struct mystruct *bar = ARENA_ALLOC_SIZED(&arena, struct mystruct);
     44 	assert(bar);
     45 
     46 	printf("bar: %p, sizeof bar: %zu, alignof bar: %zu\n",
     47 			bar, sizeof *bar, alignof(struct mystruct));
     48 
     49 	bar->a = bar->b = bar->c = 69;
     50 
     51 	assert(foo != bar);
     52 
     53 	printf("arena_reset() ...\n");
     54 
     55 	printf("\tbefore reset: foo->a = %d\n", foo->a);
     56 	arena_reset(&arena);
     57 	printf("\tafter reset: foo->a = %d\n", foo->a);
     58 
     59 	struct mystruct *baz = ARENA_ALLOC_SIZED(&arena, struct mystruct);
     60 	assert(baz);
     61 	assert(foo == baz);
     62 
     63 	printf("\tafter reset: baz: %p, baz->a = %d, baz == foo: %d\n",
     64 			baz, baz->a, baz == foo);
     65 
     66 	// struct mystruct2 *fee = ARENA_ALLOC_ARRAY(&arena, struct mystruct2, 2);
     67 	struct mystruct2 *fee = arena_alloc(&arena, sizeof *fee * 2, alignof(struct mystruct2));
     68 	assert(fee);
     69 
     70 	printf("fee: %p, sizeof fee: %zu, alignof fee: %zu\n",
     71 			fee, sizeof *fee, alignof(struct mystruct2));
     72 
     73 	printf("fee[0]: %p, fee[0] is aligned: %d, fee[1]: %p, fee[1] is aligned: %d\n",
     74 			&fee[0], IS_ALIGNED((uintptr_t) &fee[0], alignof(struct mystruct2)),
     75 			&fee[1], IS_ALIGNED((uintptr_t) &fee[1], alignof(struct mystruct2)));
     76 
     77 	assert(IS_ALIGNED((uintptr_t) &fee[0], 64));
     78 	assert(IS_ALIGNED((uintptr_t) &fee[1], 64));
     79 
     80 	char *mybuf = arena_alloc(&arena, 128, 256);
     81 	assert(mybuf);
     82 
     83 	printf("mybuf: %p, sizeof mybuf: 128, alignof mybuf: 256\n", mybuf);
     84 
     85 	assert(IS_ALIGNED((uintptr_t) mybuf, 256));
     86 
     87 	return 0;
     88 }