diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/arena.c | 6 | ||||
| -rw-r--r-- | src/arena.h | 28 |
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) { | |||
| 51 | arena * arena_init(size_t bytes) { | 51 | arena * 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 | |||
| 147 | typedef arena simplearena; | ||
| 148 | |||
| 149 | simplearena * simplearena_init(size_t bytes) { | 147 | simplearena * 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 | ||
| 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 |
