wrote with_arena macro
This commit is contained in:
+2
-1
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <criterion/criterion.h>
|
||||
#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
|
||||
Reference in New Issue
Block a user