From d394e4fa3411d8827255532cc83399f3dc3401ad Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Mon, 6 Oct 2025 19:52:20 +0200 Subject: [PATCH] complete buffer allocator --- buffer_allocator.c | 17 +++++++++++------ test_buffer_allocator.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 test_buffer_allocator.c diff --git a/buffer_allocator.c b/buffer_allocator.c index daaca94..088fb54 100644 --- a/buffer_allocator.c +++ b/buffer_allocator.c @@ -11,12 +11,16 @@ void *buffer_allocator_alloc(buffer_allocator_t *this, size_t bytes) { } 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 + for (size_t i = 0; i < bytes; i++) { + unsigned char *new_ptr_b = (unsigned char *)new_ptr; + unsigned char *old_ptr_b = (unsigned char *)old_ptr; + new_ptr_b[i] = old_ptr_b[i]; + } + return new_ptr; } void buffer_allocator_reset(buffer_allocator_t *this) { @@ -30,12 +34,9 @@ static void *buffer_alloc(void *this, size_t bytes, const char *file, int line) } static void *buffer_resize(void *this, void *old_ptr, size_t bytes, const char *file, int line) { - (void)old_ptr; (void)file; (void)line; - - // TODO: do the concrete implementation first! - void *new_ptr = buffer_allocator_alloc((buffer_allocator_t *)this, bytes); + return buffer_allocator_resize(this, old_ptr, bytes); } static void buffer_free(void *this, void *ptr, const char *file, int line) { @@ -52,4 +53,8 @@ static const allocator_vtbl_t buffer_vtbl = { }; allocator_t buffer_allocator_interface(buffer_allocator_t *this) { + return (allocator_t) { + .this=this, + .vtbl=&buffer_vtbl, + }; } diff --git a/test_buffer_allocator.c b/test_buffer_allocator.c new file mode 100644 index 0000000..8ea700d --- /dev/null +++ b/test_buffer_allocator.c @@ -0,0 +1,39 @@ +#include +#include "allocator.h" + +static void test_buffer_alloc(buffer_allocator_t impl) { + allocator_t inter = buffer_allocator_interface(&impl); + const int n_ints = 100; + int *ints = allocator_alloc(inter, sizeof(int) * n_ints); + for (int i = 0; i < n_ints; i++) { + ints[i] = i; + } + for (int i = 0; i < n_ints; i++) { + cr_assert_eq(ints[i], i); + } + cr_assert_eq(impl.size, sizeof(int)*n_ints); + ints = allocator_alloc(inter, sizeof(int) * n_ints); + for (int i = 0; i < n_ints; i++) { + ints[i] = i; + } + for (int i = 0; i < n_ints; i++) { + cr_assert_eq(ints[i], i); + } + cr_assert_eq(impl.size, sizeof(int)*2*n_ints); + allocator_free(inter, ints); + size_t remaining_bytes = impl.capacity - impl.size; + size_t double_remaining_bytes = 2 * remaining_bytes; + void *should_be_null = allocator_resize(inter, ints, double_remaining_bytes); + cr_assert_eq(should_be_null, NULL); + size_t double_ints = ((size_t) n_ints) * 2; + void *should_be_addr = allocator_resize(inter, ints, double_ints); + cr_assert_neq(should_be_addr, NULL); + buffer_allocator_reset(&impl); +} + +Test(buffer_allocator, test) { + buffer_allocator_t impl = buffer_allocator_create(MB); + test_buffer_alloc(impl); + cr_assert_eq(impl.size, 0); + test_buffer_alloc(impl); +}