diff options
Diffstat (limited to 'src/utils.c')
| -rw-r--r-- | src/utils.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..354159b --- /dev/null +++ b/src/utils.c @@ -0,0 +1,37 @@ +#include "utils.h" + +struct allocator { + void** bufs; + size_t cap; + size_t len; +}; + +Allocator newAllocator() { + Allocator alct = malloc(sizeof(struct allocator)); + alct->bufs = malloc(sizeof(void*) * 16); + alct->cap = 16; + alct->len = 0; + alct->bufs[0] = NULL; + return alct; +} + +void deleteAllocator(Allocator alct) { + for (size_t i = 0; i < alct->len; i++) { + free(alct->bufs[i]); + } + free(alct); +} + +void * allocate(Allocator alct, size_t size) { + if (alct->len >= alct->cap) { + alct->cap = alct->cap * 2; // Doubling the capacity + alct->bufs = realloc(alct->bufs, sizeof(void*) * alct->cap); + } + + void* ptr = malloc(size); // Allocate requested size + alct->bufs[alct->len] = ptr; // Store pointer in array + alct->len++; + return ptr; +} + + |
