summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
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
commitcd307d16713f2552f8cf9c1116e860de3aa2cec6 (patch)
tree9f80d1ca479fd757246554b4034da69223513e01
parent31f211a5d0969b07e98414fb47a5b5945200ddb6 (diff)
Add some documentation to arena.h, fix simplearena_clear not being declared
-rw-r--r--src/arena.c6
-rw-r--r--src/arena.h28
2 files changed, 28 insertions, 6 deletions
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) {
51arena * arena_init(size_t bytes) { 51arena * arena_init(size_t bytes) {
52 if(!ISPOWOF2(MEM_ALIGN_BYTES)) 52 if(!ISPOWOF2(MEM_ALIGN_BYTES))
53 XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); 53 XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", );
54 if(bytes < 1)
55 RETURNWERR(EINVAL, NULL);
54 56
55 arena *a = VALLOC(1, sizeof(arena)); 57 arena *a = VALLOC(1, sizeof(arena));
56 if(!a) 58 if(!a)
@@ -142,10 +144,6 @@ int arena_clear(arena **arena) {
142 144
143 145
144 146
145// Simple Arena is an arena that can't expand whenever allocating memory, meaning what you originally allocated is what you get
146
147typedef arena simplearena;
148
149simplearena * simplearena_init(size_t bytes) { 147simplearena * simplearena_init(size_t bytes) {
150 return arena_init(bytes); 148 return arena_init(bytes);
151} 149}
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
10typedef struct arena arena; 11typedef struct arena arena;
12
13// A wrapper for the normal arena struct. Can not expand in size
11typedef arena simplearena; 14typedef 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)
14static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); // Hey, the static keyword did something for once! 18
19// Variable for memory alignment
20static 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___`
17arena * arena_init(size_t bytes); 27arena * 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___`
18void * arena_alloc(arena * const arena, size_t bytes); 30void * 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
19int arena_free(arena **arena); 33int 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
20int arena_clear(arena **arena); 36int arena_clear(arena **arena);
21 37
38
39// Initializes a simple arena.
22simplearena * simplearena_init(size_t bytes); 40simplearena * 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___`
23void * simplearena_alloc(simplearena * const a, size_t bytes); 43void * 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___`
24int simplearena_free(simplearena **a); 46int simplearena_free(simplearena **a);
25int 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___`
49int simplearena_clear(simplearena **a);
26 50
27#endif \ No newline at end of file 51#endif \ No newline at end of file