wrote with_arena macro
This commit is contained in:
+2
-1
@@ -26,7 +26,7 @@ void *arena_allocator_alloc_func(
|
|||||||
return NULL;
|
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);
|
borrow_allocator_free_all(&this->borrow_allocator);
|
||||||
if (this->bytes == NULL) {
|
if (this->bytes == NULL) {
|
||||||
this->bytes = dyn_array_create_non_crashing_func(
|
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) {
|
void arena_allocator_destroy(arena_allocator_t *this) {
|
||||||
borrow_allocator_free_all(&this->borrow_allocator);
|
borrow_allocator_free_all(&this->borrow_allocator);
|
||||||
dyn_array_destroy(this->bytes);
|
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) {
|
static void *arena_allocator_alloc_impl(void *this, size_t bytes, const char *file, int line) {
|
||||||
|
|||||||
@@ -145,10 +145,19 @@ void *arena_allocator_alloc_func(
|
|||||||
#define arena_allocator_alloc(THIS, BYTES) \
|
#define arena_allocator_alloc(THIS, BYTES) \
|
||||||
arena_allocator_alloc_func(THIS, BYTES, __FILE__, __LINE__)
|
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);
|
void arena_allocator_destroy(arena_allocator_t *this);
|
||||||
allocator_t arena_allocator_interface(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 //////////////////////////////////////////////////////////////
|
// dynamic arrays //////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
typedef struct dyn_array_header {
|
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