diff --git a/src/arena.c b/src/arena.c index d68a117..fc0390f 100644 --- a/src/arena.c +++ b/src/arena.c @@ -168,12 +168,13 @@ APool *apool_create_ex(Arena *arena, U32 n_elements, U32 element_size, U32 eleme U32 size = AlignUp(element_size, element_align); APool *pool = arena_push_type(arena, APool); + Assert(pool != NULL); pool->align = align; pool->element_size = size; pool->arena = arena; pool->n_elements = n_elements; pool->free_list = NULL; - pool->start == NULL; + pool->start = NULL; if (size != APOOL_AUTOGROW) { U32 total_size = n_elements * size; // Note(nos): guard against unreasonable sizes diff --git a/src/base_core.h b/src/base_core.h index 8a5f7b7..06be6cc 100644 --- a/src/base_core.h +++ b/src/base_core.h @@ -177,7 +177,8 @@ typedef unsigned __int128 U128; #define CeilDiv(x, y) (((x) + ((y) - 1)) / (y)) /** - * Round x up to nearest multiple of y + * Round x up to nearest multiple of y: + * (((x) + (y) - 1) - (((x) + (y) - 1) % (y))) * e.g. RoundUp(30,48) == 48 * e.g. RoundUp(49,48) == 96 * @@ -185,7 +186,15 @@ typedef unsigned __int128 U128; * @param y denominator * @return x rounded up to nearest multiple of y */ -#define RoundUpToMultiple(x, y) (CeilDiv((x), (y)) * (y)) +#define RoundUpToMultiple(x, y) \ +({ \ + __typeof__(x) _x = (x); \ + __typeof__(y) _y = (y); \ + Assert(_y ! = 0); \ + __typeof__(x) _tmp = _x + _y - 1; \ + _tmp - _tmp % _y; \ +}) + /** * Round x down to nearest multiple of y diff --git a/src/main.c b/src/main.c index d596b7b..1284859 100644 --- a/src/main.c +++ b/src/main.c @@ -194,8 +194,8 @@ void miniarena_test1(void) typedef struct PoolTest PoolTest; struct PoolTest{ U32 n; - char name[32]; PoolTest *next; + char name[12]; }; void apool_test(void) @@ -238,7 +238,7 @@ void apool_test(void) for (PoolTest *p = list; p; p = p->next){ printf("Pooltest %u\n",p->n); } - + arena_free(&arena); } void apool_test_autogrow(void) @@ -291,6 +291,7 @@ int main(int argc, char *argv[]) // arena_test_3(); // arena_test_4(); // miniarena_test1(); + apool_test(); apool_test_autogrow(); } diff --git a/src/platform.h b/src/platform.h index dcc28d6..54c3700 100644 --- a/src/platform.h +++ b/src/platform.h @@ -4,9 +4,9 @@ void *platform_malloc(size_t size); U32 platform_page_size(void); +// Memory functions expect mem and size to be page aligned void *memory_allocate_and_commit(size_t size); void *memory_reserve(size_t size); void memory_commit(void *mem, size_t size); void memory_decommit(void *mem, size_t size); - void memory_free(void *mem, size_t size);