From cd307d16713f2552f8cf9c1116e860de3aa2cec6 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Fri, 4 Apr 2025 20:16:27 -0500 Subject: Add some documentation to arena.h, fix simplearena_clear not being declared --- src/arena.c | 6 ++---- src/arena.h | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/arena.c b/src/arena.c index de3523b..07c28dc 100644 --- a/src/arena.c +++ b/src/arena.c @@ -51,6 +51,8 @@ arenanode * arenanode_init(size_t bytes) { arena * arena_init(size_t bytes) { if(!ISPOWOF2(MEM_ALIGN_BYTES)) XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); + if(bytes < 1) + RETURNWERR(EINVAL, NULL); arena *a = VALLOC(1, sizeof(arena)); if(!a) @@ -142,10 +144,6 @@ int arena_clear(arena **arena) { -// Simple Arena is an arena that can't expand whenever allocating memory, meaning what you originally allocated is what you get - -typedef arena simplearena; - simplearena * simplearena_init(size_t bytes) { return arena_init(bytes); } diff --git a/src/arena.h b/src/arena.h index 331bbb1..1377fa1 100644 --- a/src/arena.h +++ b/src/arena.h @@ -7,21 +7,45 @@ // enables XALLOC functionality #define ___VXGG___USE_XALLOC_FOR_ARENAS___ 1 +// Normal arena. Can expand in size beyond original allocation typedef struct arena arena; + +// A wrapper for the normal arena struct. Can not expand in size typedef arena simplearena; +// Bitwise operation to check if a number is a power of 2 #define ISPOWOF2(x) (((x) & ((x) - 1)) == 0) -static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); // Hey, the static keyword did something for once! + +// Variable for memory alignment +static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); + +// Macro to get the remaining bytes necessary to be on a power of 2 #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) + +// Initialize an arena with `bytes` bytes of pre-allocated memory. Exits / returns NULL on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` arena * arena_init(size_t bytes); + +// Take `bytes` bytes of memory from `arena`'s memory pool. Exits / returns NULL on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` void * arena_alloc(arena * const arena, size_t bytes); + +// Free an arena its memory pool(s). Returns -1 and sets `errno` if the given arena is NULL int arena_free(arena **arena); + +// Clear an arena of its contents (frees and inits an arena in 1). Returns -1 and sets `errno` if the given arena is NULL int arena_clear(arena **arena); + +// Initializes a simple arena. simplearena * simplearena_init(size_t bytes); + +// Grabs some memory from the simple arena's pool. Exits / returns NULL on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` void * simplearena_alloc(simplearena * const a, size_t bytes); + +// Free a simple arena. Exits / returns -1 on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` int simplearena_free(simplearena **a); -int simplearena_free(simplearena **a); + +// Clear a simple arena and its memory pool (frees and inits a simple arena in 1). Exits / returns -1 on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` +int simplearena_clear(simplearena **a); #endif \ No newline at end of file -- cgit v1.2.3