diff options
Diffstat (limited to 'src/arena.c')
| -rw-r--r-- | src/arena.c | 24 |
1 files changed, 10 insertions, 14 deletions
diff --git a/src/arena.c b/src/arena.c index 40c72d5..8f32064 100644 --- a/src/arena.c +++ b/src/arena.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #include "arena.h" | 1 | #include "arena.h" |
| 2 | #include "shared.h" | 2 | #include "shared.h" |
| 3 | 3 | ||
| 4 | #include <asm-generic/errno-base.h> | ||
| 4 | #include <stddef.h> | 5 | #include <stddef.h> |
| 5 | #include <stdint.h> | 6 | #include <stdint.h> |
| 6 | #include <stdlib.h> | 7 | #include <stdlib.h> |
| @@ -26,6 +27,8 @@ typedef struct arena { | |||
| 26 | } arena; | 27 | } arena; |
| 27 | 28 | ||
| 28 | arenanode * arenanode_init(size_t bytes) { | 29 | arenanode * arenanode_init(size_t bytes) { |
| 30 | if(bytes < 1) ERRRET(EINVAL, NULL); | ||
| 31 | |||
| 29 | arenanode *an = VALLOC(1, sizeof(*an)); | 32 | arenanode *an = VALLOC(1, sizeof(*an)); |
| 30 | if(!an) | 33 | if(!an) |
| 31 | return NULL; | 34 | return NULL; |
| @@ -47,10 +50,8 @@ arenanode * arenanode_init(size_t bytes) { | |||
| 47 | } | 50 | } |
| 48 | 51 | ||
| 49 | arena * arena_init(size_t bytes) { | 52 | arena * arena_init(size_t bytes) { |
| 50 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | 53 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); |
| 51 | XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); | 54 | if(bytes < 1) ERRRET(EINVAL, NULL); |
| 52 | if(bytes < 1) | ||
| 53 | RETURNWERR(EINVAL, NULL); | ||
| 54 | 55 | ||
| 55 | arena *a = VALLOC(1, sizeof(arena)); | 56 | arena *a = VALLOC(1, sizeof(arena)); |
| 56 | if(!a) | 57 | if(!a) |
| @@ -73,12 +74,9 @@ arena * arena_init(size_t bytes) { | |||
| 73 | } | 74 | } |
| 74 | 75 | ||
| 75 | void * arena_alloc(arena * const arena, size_t bytes) { | 76 | void * arena_alloc(arena * const arena, size_t bytes) { |
| 76 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | 77 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) XALLOC_EXIT("<arena_alloc> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory", ); |
| 77 | XALLOC_EXIT("<arena_alloc> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory", ); | 78 | if(!arena) ERRRET(EINVAL, NULL); |
| 78 | if(!arena) | 79 | if(bytes > arena->node_memspace) ERRRET(ENOMEM, NULL); |
| 79 | RETURNWERR(EINVAL, NULL); | ||
| 80 | if(bytes > arena->node_memspace) | ||
| 81 | RETURNWERR(ENOMEM, NULL); | ||
| 82 | 80 | ||
| 83 | if(bytes > ((arena->current)->allocated - (arena->current)->used)) { | 81 | if(bytes > ((arena->current)->allocated - (arena->current)->used)) { |
| 84 | arenanode *new = arenanode_init(arena->node_memspace); | 82 | arenanode *new = arenanode_init(arena->node_memspace); |
| @@ -133,10 +131,8 @@ void * simplearena_alloc(simplearena * const a, size_t bytes) { | |||
| 133 | // The criteria to allocate new memory in arena_alloc is 'bytes > ((a->current)->allocated - (a->current)->used)', so if this | 131 | // The criteria to allocate new memory in arena_alloc is 'bytes > ((a->current)->allocated - (a->current)->used)', so if this |
| 134 | // is true, just return NULL & set errno | 132 | // is true, just return NULL & set errno |
| 135 | 133 | ||
| 136 | if(!a) | 134 | if(!a) ERRRET(EINVAL, NULL); |
| 137 | RETURNWERR(EINVAL, NULL); | 135 | if(bytes > ((a->current)->allocated - (a->current)->used)) ERRRET(ENOMEM, NULL); |
| 138 | if(bytes > ((a->current)->allocated - (a->current)->used)) | ||
| 139 | RETURNWERR(ENOMEM, NULL); | ||
| 140 | 136 | ||
| 141 | return arena_alloc(a, bytes); | 137 | return arena_alloc(a, bytes); |
| 142 | } | 138 | } |
