From 9b1eaea653e39943affeeb0432cbe23e629c509e Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Tue, 4 Nov 2025 20:41:21 +0100 Subject: [PATCH] dynamic arrays seem to be working --- cig.h | 24 ++++++++++++++++++++--- dyn_array.c | 31 +++++++++++++++++++++-------- makefile | 2 +- test_dynamic_array.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ test_macro_magic.c | 4 ++-- 5 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 test_dynamic_array.c diff --git a/cig.h b/cig.h index 49e4287..36ff25a 100644 --- a/cig.h +++ b/cig.h @@ -25,9 +25,9 @@ typedef struct allocator_vtbl { // An instance of an allocator. typedef struct allocator { // pointer to the behind-the-scenes data that an allocator may store. - void *const this; + void *this; // pointer to the method implementations of an allocator. - const allocator_vtbl_t *const vtbl; + const allocator_vtbl_t *vtbl; } allocator_t; void *allocator_alloc_func(allocator_t this, size_t bytes, const char *file, int line); @@ -125,7 +125,18 @@ void *dyn_array_create_func(dyn_array_create_func_args_t args); // Always reassign the array. if multiple variables reference the same growing // array, then you should be using pointer pointers. void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line); -#define dyn_array_grow(THIS, N_NEW_ITEMS) dyn_array_grow_func(THIS, N_NEW_ITEMS, __FILE__, __LINE__) +void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *file, int line); + +#define dyn_array_grow(THIS, N_NEW_ITEMS) \ + dyn_array_grow_func(THIS, N_NEW_ITEMS, __FILE__, __LINE__) + +#define dyn_array_shrink(THIS, N_ITEMS_TO_REMOVE) \ + dyn_array_shrink_func(THIS, N_ITEMS_TO_REMOVE, __FILE__, __LINE__) + +#define dyn_array_reset(THIS) \ + do { \ + dyn_array_shrink(THIS, dyn_array_length(THIS)); \ + } while (0) typedef struct dyn_array_create_non_crashing_func_args { allocator_t allocator; @@ -136,6 +147,7 @@ void *dyn_array_create_non_crashing_func(dyn_array_create_non_crashing_func_args // This version returns a NULL pointer instead of crashing if the allocator return NULL. // It is up to you to check that the pointer returned isn't NULL #define dyn_array_create_non_crashing(ALLOCATOR, TYPE, ...) ((TYPE*) dyn_array_create_func((dyn_array_create_non_crashing_func_args_t){.allocator=ALLOCATOR, .itemsize=sizeof(TYPE), __VA_ARGS__})) + // This version returns a NULL pointer instead of crashing if the allocator return NULL. // It is up to you to check that the pointer returned isn't NULL // Always reassign the array. if multiple variables reference the same growing @@ -145,6 +157,12 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items); size_t dyn_array_length(void *this); size_t dyn_array_capacity(void *this); +#define dyn_array_append(THIS, VAL) do { \ + THIS = dyn_array_grow(THIS, 1); \ + THIS[dyn_array_length(THIS)-1] = VAL; \ +} while(0) + +#define dyn_array_pop(THIS) (dyn_array_shrink(THIS, 1), THIS[dyn_array_length(THIS)]) #ifdef ALLOCATOR_IMPLEMENTATION diff --git a/dyn_array.c b/dyn_array.c index 71937eb..5860ca5 100644 --- a/dyn_array.c +++ b/dyn_array.c @@ -24,7 +24,7 @@ void *dyn_array_create_func(dyn_array_create_func_args_t args) { void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line) { void *bytes = dyn_array_grow_non_crashing_func(this, n_new_items); if (bytes == NULL) { - fpritnf( + fprintf( stderr, "%s:%d: allocator returned NULL\n", file, @@ -54,9 +54,9 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items) { if (header->capacity < new_size) { size_t cap_times_2 = header->capacity * 2; size_t new_capacity = - cap_times_2 > new_size - ? cap_times_2 - : new_size; + cap_times_2 < new_size + ? new_size + : cap_times_2; header = allocator_resize( header->allocator, header, @@ -65,15 +65,30 @@ void *dyn_array_grow_non_crashing_func(void *this, size_t n_new_items) { if (header == NULL) { return NULL; } header->capacity = new_capacity; } - return &header->bytes + return &header->bytes; +} + +void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *file, int line) { + if (dyn_array_length(this) < n_items_to_remove) { + fprintf( + stderr, + "%s:%d: tried to remove more items than contained in dynamic array.\n", + file, + line + ); + exit(1); + } + dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this); + header->size -= n_items_to_remove; } size_t dyn_array_length(void *this) { 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); { +size_t dyn_array_capacity(void *this) { dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this); + return header->capacity; } - - diff --git a/makefile b/makefile index 40b8bf2..1279a08 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ CC := gcc -CFLAGS := -std=c99 -pedantic -Wall -Wextra -Wno-override-init -O0 -g -fno-omit-frame-pointer -fno-inline +CFLAGS := -pedantic -Wall -Wextra -Wno-override-init -O0 -g -fno-omit-frame-pointer -fno-inline LDFLAGS := -lcriterion TESTBIN := /tmp/all_tests diff --git a/test_dynamic_array.c b/test_dynamic_array.c new file mode 100644 index 0000000..c93f427 --- /dev/null +++ b/test_dynamic_array.c @@ -0,0 +1,46 @@ +#include + +#include + +#include "cig.h" + +Test(dynamic_arrays, append) { + with_borrow(alloc) { + int *numbers = dyn_array_create(alloc, int); + dyn_array_append(numbers, 40); + dyn_array_append(numbers, 41); + dyn_array_append(numbers, 42); + dyn_array_append(numbers, 43); + dyn_array_append(numbers, 44); + dyn_array_append(numbers, 45); + dyn_array_append(numbers, 46); + dyn_array_append(numbers, 47); + dyn_array_append(numbers, 48); + dyn_array_append(numbers, 49); + dyn_array_append(numbers, 50); + for (int i = 0; i < (int)dyn_array_length(numbers); i++) { + cr_assert_eq(numbers[i], i+40); + } + } +} + +Test(dynamic_arrays, pop) { + with_borrow(alloc) { + int *numbers = dyn_array_create(alloc, int); + dyn_array_append(numbers, 40); + dyn_array_append(numbers, 41); + dyn_array_append(numbers, 42); + dyn_array_append(numbers, 43); + dyn_array_append(numbers, 44); + dyn_array_append(numbers, 45); + dyn_array_append(numbers, 46); + dyn_array_append(numbers, 47); + dyn_array_append(numbers, 48); + dyn_array_append(numbers, 49); + dyn_array_append(numbers, 50); + for (int i = 0; i < (int)dyn_array_length(numbers); i++) { + int num = dyn_array_pop(numbers); + cr_assert_eq(num, 50-i); + } + } +} diff --git a/test_macro_magic.c b/test_macro_magic.c index 5071056..4207f8d 100644 --- a/test_macro_magic.c +++ b/test_macro_magic.c @@ -23,6 +23,6 @@ Test(macro_magic, default_values) { cr_assert_eq(a1.is_true, true); cr_assert_eq(a2.is_true, true); cr_assert_eq(a3.is_true, false); - malloc(100); - // TODO: make this shite crash the tests... + // TODO if there is an allocation error it does not actually cause a crash... + // FIX!!! }