From 6dc0caceeb8d5ade6014cd5853dff0716f4f6d20 Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Tue, 25 Nov 2025 22:18:54 +0100 Subject: [PATCH] wrote with_arena macro --- arena_allocator.c | 3 ++- cig.h | 13 +++++++++++-- test_arena_allocator.c | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test_arena_allocator.c diff --git a/arena_allocator.c b/arena_allocator.c index 6b0168e..ac321ce 100644 --- a/arena_allocator.c +++ b/arena_allocator.c @@ -26,7 +26,7 @@ void *arena_allocator_alloc_func( return NULL; } -void arena_allocator_reset(arena_allocator_t *this, const char *file, int line) { +void arena_allocator_reset_func(arena_allocator_t *this, const char *file, int line) { borrow_allocator_free_all(&this->borrow_allocator); if (this->bytes == NULL) { this->bytes = dyn_array_create_non_crashing_func( @@ -72,6 +72,7 @@ void arena_allocator_reset(arena_allocator_t *this, const char *file, int line) void arena_allocator_destroy(arena_allocator_t *this) { borrow_allocator_free_all(&this->borrow_allocator); dyn_array_destroy(this->bytes); + *this = arena_allocator_create(); } static void *arena_allocator_alloc_impl(void *this, size_t bytes, const char *file, int line) { diff --git a/cig.h b/cig.h index 820c43d..e818eb6 100644 --- a/cig.h +++ b/cig.h @@ -101,7 +101,7 @@ allocator_t borrow_allocator_interface(borrow_allocator_t *this); borrow_allocator_interface(&borrow_allocator_create()); \ !((borrow_allocator_t *)NAME.this)->head; \ ((borrow_allocator_t *)NAME.this)->head = \ - (borrow_allocator_free_all(((borrow_allocator_t *)NAME.this)), \ + (borrow_allocator_free_all(((borrow_allocator_t *)NAME.this)), \ (linked_allocation_node_t *)1)) \ for (int UNIQUE = 0; UNIQUE < 1; UNIQUE++) @@ -145,10 +145,19 @@ void *arena_allocator_alloc_func( #define arena_allocator_alloc(THIS, BYTES) \ arena_allocator_alloc_func(THIS, BYTES, __FILE__, __LINE__) -void arena_allocator_reset(arena_allocator_t *this, const char *file, int line); +void arena_allocator_reset_func(arena_allocator_t *this, const char *file, int line); +#define arena_allocator_reset(THIS) \ + arena_allocator_reset_func(THIS, __FILE__, __LINE__) void arena_allocator_destroy(arena_allocator_t *this); allocator_t arena_allocator_interface(arena_allocator_t *this); +#define with_arena(ARENA, ALLOCATOR_NAME) \ + for (allocator_t ALLOCATOR_NAME = arena_allocator_interface(ARENA); \ + ALLOCATOR_NAME.vtbl != NULL; \ + ALLOCATOR_NAME.vtbl = \ + (arena_allocator_reset_func(ARENA, __FILE__, __LINE__), NULL)) \ + for (int UNIQUE = 0; UNIQUE < 1; UNIQUE++) + // dynamic arrays ////////////////////////////////////////////////////////////// typedef struct dyn_array_header { diff --git a/test_arena_allocator.c b/test_arena_allocator.c new file mode 100644 index 0000000..4b3dd25 --- /dev/null +++ b/test_arena_allocator.c @@ -0,0 +1,21 @@ +#include +#include "cig.h" + +Test(arena_allocator, repeated_allocations) { + arena_allocator_t aalloc = arena_allocator_create(); + + for ( int i = 0; i < 10; i++ ) { + with_arena(&aalloc, allocator) { + allocator_alloc(allocator, 10); + } + } + + cr_assert_eq(aalloc.total_allocated, 10); + cr_assert_eq(borrow_allocator_count_allocations(&aalloc.borrow_allocator), 0); + cr_assert_eq(dyn_array_capacity(aalloc.bytes), 10); + + arena_allocator_destroy(&aalloc); + cr_assert_eq(aalloc.total_allocated, 0); +} + +// TODO somehow test reallocations