diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/arena.c | 56 | ||||
| -rw-r--r-- | src/arena.h | 4 |
2 files changed, 25 insertions, 35 deletions
diff --git a/src/arena.c b/src/arena.c index d340f3a..870edca 100644 --- a/src/arena.c +++ b/src/arena.c | |||
| @@ -7,6 +7,8 @@ | |||
| 7 | #include <errno.h> | 7 | #include <errno.h> |
| 8 | #include <error.h> | 8 | #include <error.h> |
| 9 | 9 | ||
| 10 | #define VALLOC(nmemb, size) ((___VXGG___USE_XALLOC_FOR_ARENAS___ > 0) ? xcalloc((nmemb), (size)) : malloc((nmemb) * (size))) | ||
| 11 | |||
| 10 | typedef struct an { | 12 | typedef struct an { |
| 11 | void *membase; | 13 | void *membase; |
| 12 | void *memcur; | 14 | void *memcur; |
| @@ -25,7 +27,7 @@ typedef struct arena { | |||
| 25 | } arena; | 27 | } arena; |
| 26 | 28 | ||
| 27 | int arenanode_init(arenanode **an, size_t bytes) { | 29 | int arenanode_init(arenanode **an, size_t bytes) { |
| 28 | (*an) = xcalloc(1, sizeof(**an)); | 30 | (*an) = VALLOC(1, sizeof(**an)); |
| 29 | if(!(*an)) | 31 | if(!(*an)) |
| 30 | return -1; | 32 | return -1; |
| 31 | 33 | ||
| @@ -33,7 +35,7 @@ int arenanode_init(arenanode **an, size_t bytes) { | |||
| 33 | (*an)->used = 0; | 35 | (*an)->used = 0; |
| 34 | (*an)->next = NULL; | 36 | (*an)->next = NULL; |
| 35 | 37 | ||
| 36 | void *mem = xcalloc(bytes, 1); | 38 | void *mem = VALLOC(bytes, 1); |
| 37 | if(!mem) { | 39 | if(!mem) { |
| 38 | free((*an)); | 40 | free((*an)); |
| 39 | (*an) = NULL; | 41 | (*an) = NULL; |
| @@ -50,7 +52,7 @@ int arena_init(arena **a, size_t bytes) { | |||
| 50 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | 52 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) |
| 51 | 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"); |
| 52 | 54 | ||
| 53 | (*a) = xcalloc(1, sizeof(**a)); | 55 | (*a) = VALLOC(1, sizeof(**a)); |
| 54 | if(!(*a)) | 56 | if(!(*a)) |
| 55 | return -1; | 57 | return -1; |
| 56 | (*a)->start = NULL; | 58 | (*a)->start = NULL; |
| @@ -71,16 +73,12 @@ int arena_init(arena **a, size_t bytes) { | |||
| 71 | } | 73 | } |
| 72 | 74 | ||
| 73 | void* arena_alloc(arena * const arena, size_t bytes) { | 75 | void* arena_alloc(arena * const arena, size_t bytes) { |
| 74 | if(!arena) { | ||
| 75 | errno = EINVAL; | ||
| 76 | return NULL; | ||
| 77 | } | ||
| 78 | if(bytes > arena->node_memspace) { | ||
| 79 | errno = ENOMEM; | ||
| 80 | return NULL; | ||
| 81 | } | ||
| 82 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) | 76 | if(!ISPOWOF2(MEM_ALIGN_BYTES)) |
| 83 | 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) | ||
| 79 | RETURNWERR(EINVAL, NULL); | ||
| 80 | if(bytes > arena->node_memspace) | ||
| 81 | RETURNWERR(ENOMEM, NULL); | ||
| 84 | 82 | ||
| 85 | if(bytes > ((arena->current)->allocated - (arena->current)->used)) { | 83 | if(bytes > ((arena->current)->allocated - (arena->current)->used)) { |
| 86 | arenanode *new; | 84 | arenanode *new; |
| @@ -106,14 +104,10 @@ void* arena_alloc(arena * const arena, size_t bytes) { | |||
| 106 | } | 104 | } |
| 107 | 105 | ||
| 108 | int arena_free(arena **arena) { | 106 | int arena_free(arena **arena) { |
| 109 | if(!arena) { | 107 | if(!arena) |
| 110 | errno = EINVAL; | 108 | RETURNWERR(EINVAL, -1); |
| 111 | return -1; | 109 | if(!(*arena)) |
| 112 | } | 110 | RETURNWERR(EINVAL, -1); |
| 113 | if(!(*arena)) { | ||
| 114 | errno = EINVAL; | ||
| 115 | return -1; | ||
| 116 | } | ||
| 117 | 111 | ||
| 118 | (*arena)->current = (*arena)->start; | 112 | (*arena)->current = (*arena)->start; |
| 119 | for(arenanode *n; (*arena)->current != NULL;) { | 113 | for(arenanode *n; (*arena)->current != NULL;) { |
| @@ -131,14 +125,10 @@ int arena_free(arena **arena) { | |||
| 131 | } | 125 | } |
| 132 | 126 | ||
| 133 | int arena_clear(arena **arena) { | 127 | int arena_clear(arena **arena) { |
| 134 | if(!arena) { | 128 | if(!arena) |
| 135 | errno = EINVAL; | 129 | RETURNWERR(EINVAL, -1); |
| 136 | return -1; | 130 | if(!(*arena)) |
| 137 | } | 131 | RETURNWERR(EINVAL, -1); |
| 138 | if(!(*arena)) { | ||
| 139 | errno = EINVAL; | ||
| 140 | return -1; | ||
| 141 | } | ||
| 142 | 132 | ||
| 143 | size_t bytes = (*arena)->node_memspace; | 133 | size_t bytes = (*arena)->node_memspace; |
| 144 | 134 | ||
| @@ -162,14 +152,10 @@ void* simplearena_alloc(simplearena * const a, size_t bytes) { | |||
| 162 | // The criteria to allocate new memory in arena_alloc is 'bytes > ((a->current)->allocated - (a->current)->used)', so if this | 152 | // The criteria to allocate new memory in arena_alloc is 'bytes > ((a->current)->allocated - (a->current)->used)', so if this |
| 163 | // is true, just return NULL & set errno | 153 | // is true, just return NULL & set errno |
| 164 | 154 | ||
| 165 | if(!a) { | 155 | if(!a) |
| 166 | errno = EINVAL; | 156 | RETURNWERR(EINVAL, NULL); |
| 167 | return NULL; | 157 | if(bytes > ((a->current)->allocated - (a->current)->used)) |
| 168 | } | 158 | RETURNWERR(ENOMEM, NULL); |
| 169 | if(bytes > ((a->current)->allocated - (a->current)->used)) { | ||
| 170 | errno = ENOMEM; | ||
| 171 | return NULL; | ||
| 172 | } | ||
| 173 | 159 | ||
| 174 | return arena_alloc(a, bytes); | 160 | return arena_alloc(a, bytes); |
| 175 | } | 161 | } |
diff --git a/src/arena.h b/src/arena.h index 23dc664..a94001b 100644 --- a/src/arena.h +++ b/src/arena.h | |||
| @@ -3,6 +3,10 @@ | |||
| 3 | 3 | ||
| 4 | #include <stddef.h> | 4 | #include <stddef.h> |
| 5 | 5 | ||
| 6 | // Whether to use `XALLOC` or normal `malloc` functions for acquiring new memory in an arena. `> 0` | ||
| 7 | // enables XALLOC functionality | ||
| 8 | #define ___VXGG___USE_XALLOC_FOR_ARENAS___ 1 | ||
| 9 | |||
| 6 | typedef struct arena arena; | 10 | typedef struct arena arena; |
| 7 | typedef arena simplearena; | 11 | typedef arena simplearena; |
| 8 | 12 | ||
