#include "utils.h" #include #include struct allocator { void** bufs; size_t cap; size_t len; }; struct allocator * new_allocator() { struct 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 delete_allocator(struct allocator * alct) { for (size_t i = 0; i < alct->len; i++) { free(alct->bufs[i]); } free(alct->bufs); free(alct); } void * allocate(struct allocator * alct, size_t size) { assert(size > 0); 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; }