diff options
Diffstat (limited to 'src/arena.c')
| -rw-r--r-- | src/arena.c | 73 |
1 files changed, 37 insertions, 36 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) { |
