From 92ca0085166fe28b591f5629db1b3a741889bf8c Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Sat, 15 Nov 2025 18:25:34 +0100 Subject: [PATCH] fixed it somehow, still WIP --- buffer_allocator.c | 2 ++ cig.h | 22 +++++++++++++++++----- dyn_array.c | 6 +++++- makefile | 9 +++++++-- test_dynamic_array.c | 1 + 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/buffer_allocator.c b/buffer_allocator.c index f9d958d..bcb85b8 100644 --- a/buffer_allocator.c +++ b/buffer_allocator.c @@ -1,6 +1,8 @@ #include "cig.h" void *buffer_allocator_alloc(buffer_allocator_t *this, size_t bytes) { + // TODO: use max-align + // also make the default function crash if outOf memory? size_t new_size = this->size + bytes; if (new_size > this->capacity) { return NULL; diff --git a/cig.h b/cig.h index 7fd77e4..0c948ce 100644 --- a/cig.h +++ b/cig.h @@ -122,14 +122,26 @@ typedef struct arena_allocator { // initialization this total amount will be allocated as a single chunk. // This freeing of memory happens on reset, the next alloc allocates the // whole chunk. - size_t allocated; - // Initial chunk of memory to allocate. - size_t chunk_size; + size_t total_allocated; + uint8_t *bytes; } arena_allocator_t; -void *arena_allocator_alloc_func(arena_allocator_t *this, size_t bytes, const char *file, int line); +#define arena_allocator_create() \ + (arena_allocator_t) { \ + .borrow_allocator = borrow_allocator_create(), .total_allocated = 0, \ + .bytes = NULL \ + } + +void *arena_allocator_alloc_func( + arena_allocator_t *this, + size_t bytes, + const char *file, + int line +); #define arena_allocator_alloc(THIS, BYTES) \ arena_allocator_alloc_func(THIS, BYTES, __FILE__, __LINE__) -// TODO finish this shite + +void arena_allocator_reset(arena_allocator_t *this); +void arena_allocator_destroy(arena_allocator_t *this); // dynamic arrays ////////////////////////////////////////////////////////////// diff --git a/dyn_array.c b/dyn_array.c index 1fc4081..a7b6291 100644 --- a/dyn_array.c +++ b/dyn_array.c @@ -32,6 +32,7 @@ void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int file, line ); + exit(1); } return bytes; } @@ -70,8 +71,8 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items, const cha ); if (header == NULL) { return NULL; } header->capacity = new_capacity; - header->size = new_size; } + header->size = new_size; return &header->bytes; } @@ -90,17 +91,20 @@ void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *fil } size_t dyn_array_length(void *this) { + if (this == NULL) { return 0; } dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this); return header->size; } size_t dyn_array_capacity(void *this) { + if (this == NULL) { return 0; } dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this); return header->capacity; } void dyn_array_destroy(void *this) { + if (this == NULL) { return; } dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this); allocator_free(header->allocator, header); } diff --git a/makefile b/makefile index 6682961..a44733f 100644 --- a/makefile +++ b/makefile @@ -7,6 +7,13 @@ TESTBIN := /tmp/all_tests .PHONY: test +run_test: test + $(TESTBIN) + +run_test_memcheck: test + valgrind --leak-check=full --show-leak-kinds=all --trace-children=yes --error-exitcode=1 $(TESTBIN); \ + + test: @echo "Discovering test files..." @files=$$(find . -type f -name 'test*.c'); \ @@ -15,8 +22,6 @@ test: else \ echo "Compiling all test files into $(TESTBIN)..."; \ $(CC) $(CFLAGS) _allocator_impl.c $$files -o $(TESTBIN) $(LDFLAGS) || exit 1; \ - echo "Running tests..."; \ - valgrind --leak-check=full --show-leak-kinds=all --trace-children=yes --error-exitcode=1 $(TESTBIN); \ fi PROJECT_ROOT := $(shell pwd) diff --git a/test_dynamic_array.c b/test_dynamic_array.c index f725fc9..ecb1d1a 100644 --- a/test_dynamic_array.c +++ b/test_dynamic_array.c @@ -38,6 +38,7 @@ Test(dynamic_arrays, pop) { dyn_array_append(numbers, 48); dyn_array_append(numbers, 49); dyn_array_append(numbers, 50); + cr_assert_eq(dyn_array_length(numbers), 11); // NOTE: you can stack for loops to have scoped variables you can abuse // in macros. e.g. // for (TYPE UNIQUE = (int)dyn_array_length(numbers); UNIQUE != 0; UNIQUE = 0;)