diff options
| author | @syxhe <https://t.me/syxhe> | 2025-04-04 20:16:27 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-04-04 20:16:27 -0500 |
| commit | cd307d16713f2552f8cf9c1116e860de3aa2cec6 (patch) | |
| tree | 9f80d1ca479fd757246554b4034da69223513e01 /src/arena.h | |
| parent | 31f211a5d0969b07e98414fb47a5b5945200ddb6 (diff) | |
Add some documentation to arena.h, fix simplearena_clear not being declared
Diffstat (limited to 'src/arena.h')
| -rw-r--r-- | src/arena.h | 28 |
1 files changed, 26 insertions, 2 deletions
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 @@ | |||
| 7 | // enables XALLOC functionality | 7 | // enables XALLOC functionality |
| 8 | #define ___VXGG___USE_XALLOC_FOR_ARENAS___ 1 | 8 | #define ___VXGG___USE_XALLOC_FOR_ARENAS___ 1 |
| 9 | 9 | ||
| 10 | // Normal arena. Can expand in size beyond original allocation | ||
| 10 | typedef struct arena arena; | 11 | typedef struct arena arena; |
| 12 | |||
| 13 | // A wrapper for the normal arena struct. Can not expand in size | ||
| 11 | typedef arena simplearena; | 14 | typedef arena simplearena; |
| 12 | 15 | ||
| 16 | // Bitwise operation to check if a number is a power of 2 | ||
| 13 | #define ISPOWOF2(x) (((x) & ((x) - 1)) == 0) | 17 | #define ISPOWOF2(x) (((x) & ((x) - 1)) == 0) |
| 14 | static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); // Hey, the static keyword did something for once! | 18 | |
| 19 | // Variable for memory alignment | ||
| 20 | static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); | ||
| 21 | |||
| 22 | // Macro to get the remaining bytes necessary to be on a power of 2 | ||
| 15 | #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) | 23 | #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) |
| 16 | 24 | ||
| 25 | |||
| 26 | // 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___` | ||
| 17 | arena * arena_init(size_t bytes); | 27 | arena * arena_init(size_t bytes); |
| 28 | |||
| 29 | // 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___` | ||
| 18 | void * arena_alloc(arena * const arena, size_t bytes); | 30 | void * arena_alloc(arena * const arena, size_t bytes); |
| 31 | |||
| 32 | // Free an arena its memory pool(s). Returns -1 and sets `errno` if the given arena is NULL | ||
| 19 | int arena_free(arena **arena); | 33 | int arena_free(arena **arena); |
| 34 | |||
| 35 | // Clear an arena of its contents (frees and inits an arena in 1). Returns -1 and sets `errno` if the given arena is NULL | ||
| 20 | int arena_clear(arena **arena); | 36 | int arena_clear(arena **arena); |
| 21 | 37 | ||
| 38 | |||
| 39 | // Initializes a simple arena. | ||
| 22 | simplearena * simplearena_init(size_t bytes); | 40 | simplearena * simplearena_init(size_t bytes); |
| 41 | |||
| 42 | // Grabs some memory from the simple arena's pool. Exits / returns NULL on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` | ||
| 23 | void * simplearena_alloc(simplearena * const a, size_t bytes); | 43 | void * simplearena_alloc(simplearena * const a, size_t bytes); |
| 44 | |||
| 45 | // Free a simple arena. Exits / returns -1 on error depending on the value of `___VXGG___USE_XALLOC_FOR_ARENAS___` | ||
| 24 | int simplearena_free(simplearena **a); | 46 | int simplearena_free(simplearena **a); |
| 25 | int simplearena_free(simplearena **a); | 47 | |
| 48 | // 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___` | ||
| 49 | int simplearena_clear(simplearena **a); | ||
| 26 | 50 | ||
| 27 | #endif \ No newline at end of file | 51 | #endif \ No newline at end of file |
