diff options
| author | @syxhe <https://t.me/syxhe> | 2025-03-25 18:13:14 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-03-25 18:13:14 -0500 |
| commit | cfdd25cdd8efabaa43446036e0ff0c326c001f8f (patch) | |
| tree | 15402876239f5aa6d018fb662ab94f85f81e9277 | |
| parent | 277e3b7dd1bc85cd190c6e4c451c7bfab71ee07e (diff) | |
Change function signature of (simple)arena_init function
| -rw-r--r-- | src/arena.c | 73 | ||||
| -rw-r--r-- | src/arena.h | 4 | ||||
| -rw-r--r-- | src/main.c | 22 |
3 files changed, 60 insertions, 39 deletions
diff --git a/src/arena.c b/src/arena.c index 870edca..36a9ce7 100644 --- a/src/arena.c +++ b/src/arena.c | |||
| @@ -26,53 +26,52 @@ typedef struct arena { | |||
| 26 | size_t node_memspace; | 26 | size_t node_memspace; |
| 27 | } arena; | 27 | } arena; |
| 28 | 28 | ||
| 29 | int arenanode_init(arenanode **an, size_t bytes) { | 29 | arenanode * arenanode_init(size_t bytes) { |
| 30 | (*an) = VALLOC(1, sizeof(**an)); | 30 | arenanode *an = VALLOC(1, sizeof(*an)); |
| 31 | if(!(*an)) | 31 | if(!an) |
| 32 | return -1; | 32 | return NULL; |
| 33 | 33 | ||
| 34 | (*an)->allocated = bytes; | 34 | an->allocated = bytes; |
| 35 | (*an)->used = 0; | 35 | an->used = 0; |
| 36 | (*an)->next = NULL; | 36 | an->next = NULL; |
| 37 | 37 | ||
| 38 | void *mem = VALLOC(bytes, 1); | 38 | void *mem = VALLOC(bytes, 1); |
| 39 | if(!mem) { | 39 | if(!mem) { |
| 40 | free((*an)); | 40 | free(an); |
| 41 | (*an) = NULL; | 41 | return NULL; |
| 42 | return -1; | ||
| 43 | } | 42 | } |
| 44 | 43 | ||
| 45 | (*an)->membase = mem; | 44 | an->membase = mem; |
| 46 | (*an)->memcur = mem; | 45 | an->memcur = mem; |
| 47 | 46 | ||
| 48 | return 0; | 47 | return an; |
| 49 | } | 48 | } |
| 50 | 49 | ||
| 51 | int arena_init(arena **a, size_t bytes) { | 50 | arena * arena_init(size_t bytes) { |
| 52 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | 51 | 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"); | 52 | XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena"); |
| 54 | 53 | ||
| 55 | (*a) = VALLOC(1, sizeof(**a)); | 54 | arena *a = VALLOC(1, sizeof(arena)); |
| 56 | if(!(*a)) | 55 | if(!a) |
| 57 | return -1; | 56 | return NULL; |
| 58 | (*a)->start = NULL; | 57 | |
| 59 | (*a)->current = NULL; | 58 | a->start = NULL; |
| 60 | (*a)->node_memspace = bytes; | 59 | a->current = NULL; |
| 61 | 60 | a->node_memspace = bytes; | |
| 62 | arenanode *base; | 61 | |
| 63 | if(arenanode_init(&base, bytes) < 0) { | 62 | arenanode *base = arenanode_init(bytes); |
| 64 | free(*a); | 63 | if(!base) { |
| 65 | (*a) = NULL; | 64 | free(a); |
| 66 | return -1; | 65 | return NULL; |
| 67 | } | 66 | } |
| 68 | 67 | ||
| 69 | (*a)->start = base; | 68 | a->start = base; |
| 70 | (*a)->current = base; | 69 | a->current = base; |
| 71 | 70 | ||
| 72 | return 0; | 71 | return a; |
| 73 | } | 72 | } |
| 74 | 73 | ||
| 75 | void* arena_alloc(arena * const arena, size_t bytes) { | 74 | void * arena_alloc(arena * const arena, size_t bytes) { |
| 76 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | 75 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) |
| 77 | XALLOC_EXIT("<arena_alloc> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory"); | 76 | XALLOC_EXIT("<arena_alloc> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory"); |
| 78 | if(!arena) | 77 | if(!arena) |
| @@ -81,8 +80,8 @@ void* arena_alloc(arena * const arena, size_t bytes) { | |||
| 81 | RETURNWERR(ENOMEM, NULL); | 80 | RETURNWERR(ENOMEM, NULL); |
| 82 | 81 | ||
| 83 | if(bytes > ((arena->current)->allocated - (arena->current)->used)) { | 82 | if(bytes > ((arena->current)->allocated - (arena->current)->used)) { |
| 84 | arenanode *new; | 83 | arenanode *new = arenanode_init(arena->node_memspace); |
| 85 | if(arenanode_init(&new, arena->node_memspace) < 0) | 84 | if(!new) |
| 86 | return NULL; | 85 | return NULL; |
| 87 | 86 | ||
| 88 | arena->current->next = new; | 87 | arena->current->next = new; |
| @@ -135,7 +134,9 @@ int arena_clear(arena **arena) { | |||
| 135 | int ret = 0; | 134 | int ret = 0; |
| 136 | if((ret = arena_free(arena)) < 0) | 135 | if((ret = arena_free(arena)) < 0) |
| 137 | return ret; | 136 | return ret; |
| 138 | return arena_init(arena, bytes); | 137 | (*arena) = arena_init(bytes); |
| 138 | |||
| 139 | return (!(*arena)) ? -1 : 0; | ||
| 139 | } | 140 | } |
| 140 | 141 | ||
| 141 | 142 | ||
| @@ -144,8 +145,8 @@ int arena_clear(arena **arena) { | |||
| 144 | 145 | ||
| 145 | typedef arena simplearena; | 146 | typedef arena simplearena; |
| 146 | 147 | ||
| 147 | int simplearena_init(simplearena **a, size_t bytes) { | 148 | simplearena * simplearena_init(size_t bytes) { |
| 148 | return arena_init(a, bytes); | 149 | return arena_init(bytes); |
| 149 | } | 150 | } |
| 150 | 151 | ||
| 151 | void* simplearena_alloc(simplearena * const a, size_t bytes) { | 152 | void* simplearena_alloc(simplearena * const a, size_t bytes) { |
diff --git a/src/arena.h b/src/arena.h index e656f9a..1f57f01 100644 --- a/src/arena.h +++ b/src/arena.h | |||
| @@ -14,12 +14,12 @@ typedef arena simplearena; | |||
| 14 | static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); // Hey, the static keyword did something for once! | 14 | static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); // Hey, the static keyword did something for once! |
| 15 | #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) | 15 | #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) |
| 16 | 16 | ||
| 17 | int arena_init(arena **a, size_t bytes); | 17 | arena * arena_init(size_t bytes); |
| 18 | void* arena_alloc(arena * const arena, size_t bytes); | 18 | void* arena_alloc(arena * const arena, size_t bytes); |
| 19 | int arena_free(arena **arena); | 19 | int arena_free(arena **arena); |
| 20 | int arena_clear(arena **arena); | 20 | int arena_clear(arena **arena); |
| 21 | 21 | ||
| 22 | int simplearena_init(simplearena **a, size_t bytes); | 22 | simplearena * simplearena_init(size_t bytes); |
| 23 | void* simplearena_alloc(simplearena * const a, size_t bytes); | 23 | void* simplearena_alloc(simplearena * const a, size_t bytes); |
| 24 | int simplearena_free(simplearena **a); | 24 | int simplearena_free(simplearena **a); |
| 25 | int simplearena_free(simplearena **a); | 25 | int simplearena_free(simplearena **a); |
| @@ -7,8 +7,28 @@ | |||
| 7 | #include <error.h> | 7 | #include <error.h> |
| 8 | #include <stdio.h> | 8 | #include <stdio.h> |
| 9 | 9 | ||
| 10 | #include <string.h> | ||
| 11 | #include <stdlib.h> | ||
| 10 | int main() { | 12 | int main() { |
| 11 | error(1, ENOTSUP, "No main file lol"); | 13 | // error(1, ENOTSUP, "No main file lol"); |
| 14 | |||
| 15 | // Testing Arena changes | ||
| 16 | const int DATA[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | ||
| 17 | |||
| 18 | arena *arena = arena_init(sizeof(DATA)); | ||
| 19 | if(!arena) | ||
| 20 | error(1, errno, "Could not initialize arena"); | ||
| 21 | |||
| 22 | int *nums = arena_alloc(arena, sizeof(DATA)); | ||
| 23 | if(!nums) | ||
| 24 | error(1, errno, "Could not allocate memory"); | ||
| 25 | |||
| 26 | memmove(nums, DATA, sizeof(DATA)); | ||
| 27 | for(unsigned int i = 0; i < STATIC_ARRAY_LEN(DATA); i++) | ||
| 28 | printf("%d ", nums[i]); | ||
| 29 | printf("\n"); | ||
| 30 | |||
| 31 | arena_free(&arena); | ||
| 12 | 32 | ||
| 13 | return 0; | 33 | return 0; |
| 14 | } \ No newline at end of file | 34 | } \ No newline at end of file |
