From 6374c47a623f72ba998cf4fb3ce4a01e083e44be Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Sun, 5 Oct 2025 20:14:47 +0200 Subject: [PATCH] some buffer allocator progress --- allocator.h | 16 +++++++++------- buffer_allocator.c | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/allocator.h b/allocator.h index 5760fd3..57a3442 100644 --- a/allocator.h +++ b/allocator.h @@ -41,15 +41,17 @@ typedef struct buffer_allocator { uint8_t *data; } buffer_allocator_t; -#define buffer_allocator_stack_create(CAPACITY) \ - ((buffer_allocator_t){ \ +#define buffer_allocator_create(CAPACITY) \ + ((buffer_allocator_t){ \ .size = 0, .capacity = CAPACITY, .data = (uint8_t[CAPACITY]){}}) -buffer_allocator_t *buffer_allocator_heap_create(allocator_t alloc, size_t capacity); -void buffer_allocator_heap_destroy(allocator_t alloc, buffer_allocator_t *this); -// TODO: maybe use the macro magic to use the standard allcocator as the default -// one? or make users define a STANDARD_ALLOCATOR value? need to think about the -// consequenses +allocator_t buffer_allocator_interface(buffer_allocator_t *this); +void *buffer_allocator_alloc(buffer_allocator_t *this, size_t bytes); +// Very simple resize. Does not free any memory, nor does it keep track of the +// previously allocated size of the old_pointer, just copies over as few bytes +// as it can with the information it has to a new allocation. +void *buffer_allocator_resize(buffer_allocator_t *this, void *old_ptr, size_t bytes); +void buffer_allocator_reset(buffer_allocator_t *this); #ifdef ALLOCATOR_IMPLEMENTATION diff --git a/buffer_allocator.c b/buffer_allocator.c index 3a79495..c126ae6 100644 --- a/buffer_allocator.c +++ b/buffer_allocator.c @@ -1,22 +1,43 @@ #include "allocator.h" -// TODO +void *buffer_allocator_alloc(buffer_allocator_t *this, size_t bytes) { + size_t new_size = this->size + bytes; + if (new_size > this->capacity) { + return NULL; + } + void *ptr = &this->data[this->size] + this->size = new_size; + return ptr; +} + +void *buffer_allocator_resize(buffer_allocator_t *this, void *old_ptr, size_t bytes) { + // TODO: check if the pointer is even in the buffer? + void *new_ptr = buffer_allocator_alloc(this, bytes); + if (new_ptr == NULL) { + return NULL; + } + // TODO: copy over data +} + +void buffer_allocator_reset(buffer_allocator_t *this) { + this->size = 0; +} static void *buffer_alloc(void *this, size_t bytes, const char *file, int line) { - (void)this; - (void)bytes; (void)file; (void)line; - return 0; + return buffer_allocator_alloc((buffer_allocator_t *)this, bytes); } + static void *buffer_resize(void *this, void *old_ptr, size_t bytes, const char *file, int line) { - (void)this; (void)old_ptr; - (void)bytes; (void)file; (void)line; - return 0; + + // TODO: do the concrete implementation first! + void *new_ptr = buffer_allocator_alloc((buffer_allocator_t *)this, bytes); } + static void buffer_free(void *this, void *ptr, const char *file, int line) { (void)this; (void)ptr; @@ -29,3 +50,6 @@ static const allocator_vtbl_t buffer_vtbl = { .resize = buffer_resize, .free = buffer_free, }; + +allocator_t buffer_allocator_interface(buffer_allocator_t *this) { +}