From d0d98170ea65bb3c09de31630401713e0c6a084b Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Tue, 5 May 2026 23:05:53 +0200 Subject: [PATCH] made a discovery of some kind of fuckup --- .gitignore | 1 + cig.h | 7 ++++--- file_io.c | 2 +- foobar.c | 15 +++++++++------ justfile | 42 ++++++++++++++++++++++++++++++------------ map.c | 7 ++++++- test_arena_allocator.c | 10 +++++----- test_dynamic_array.c | 30 +++++++++++++++--------------- test_map.c | 34 ++++++++++++++++++++++++++++++++++ 9 files changed, 105 insertions(+), 43 deletions(-) create mode 100644 test_map.c diff --git a/.gitignore b/.gitignore index ab9ba69..be9cf3e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ tags *.un~ /foobar +/foobar-expanded.c diff --git a/cig.h b/cig.h index 7046595..e330032 100644 --- a/cig.h +++ b/cig.h @@ -156,14 +156,14 @@ typedef struct dyn_array_create_func_args { } dyn_array_create_func_args_t; void *dyn_array_create_func(dyn_array_create_func_args_t args); -#define make_arr(TYPE, ...) ((TYPE *)dyn_array_create_func((dyn_array_create_func_args_t){ \ +#define arr_make(TYPE, ...) ((TYPE *)dyn_array_create_func((dyn_array_create_func_args_t){ \ .item_size = sizeof(TYPE), \ .file = __FILE__, \ .line = __LINE__, \ .allocator = __VA_ARGS__, \ })) -#define init_arr(PTR, ...) do { \ +#define arr_init(PTR, ...) do { \ (*(PTR)) = dyn_array_create_func((dyn_array_create_func_args_t){ \ .item_size = sizeof(*(*(PTR))), \ .file = __FILE__, \ @@ -269,7 +269,7 @@ typedef struct map_create_func_args { void *map_create_func(map_create_func_args_t args); -#define init_map(PTR, ...) do { \ +#define map_init(PTR, ...) do { \ (*(PTR)) = map_create_func((map_create_func_args_t) { \ .item_size=sizeof(*(*(PTR))), \ .key_size=sizeof((*(PTR))->key), \ @@ -317,6 +317,7 @@ typedef struct index_pair { * index with a tombstone if they are going to write a new value for that key. */ index_pair_t map_pair_hash(void *this, void *pair); +unsigned int map_place(void *this, index_pair_t idx_pair); // TODO: NOT DONE!!! FIRST try to get the existing in #define set_add(THIS, VALUE) do { \ diff --git a/file_io.c b/file_io.c index a4143b9..a10a7c7 100644 --- a/file_io.c +++ b/file_io.c @@ -19,7 +19,7 @@ char *read_entire_file(const char *path, allocator_t allocator) { // Returns dynamic array, of fixed char strings. char **read_all_file_lines(const char *path, allocator_t allocator) { char *contents = read_entire_file(path, allocator); - char **lines = make_arr(char*, allocator); + char **lines = arr_make(char*, allocator); arr_append(lines, contents); bool just_split = false; for (char *c = contents; (*c)!='\0'; c++) { diff --git a/foobar.c b/foobar.c index ecd2737..9c0c5e6 100644 --- a/foobar.c +++ b/foobar.c @@ -3,12 +3,15 @@ int main(void) { with_borrow(allocator) { - int *ints; - set_init(&ints, allocator, .initial_capacity=20); - map_print_mapping_state(ints, stdout); - for ( int i = 0; i < 100; i++ ) { - set_add(&ints, i); - map_print_mapping_state(ints, stdout); + int *s; + set_init(&s, allocator); + map_print_mapping_state((void*)s, stdout); + fflush(stdout); + + for (int i = 0; i < 10; i++) { + set_add(&s, 1); + map_print_mapping_state((void*)s, stdout); + fflush(stdout); } } } diff --git a/justfile b/justfile index 8f8456c..674ba8d 100644 --- a/justfile +++ b/justfile @@ -1,33 +1,51 @@ -CC := "zig cc" +CC := "cc" CFLAGS := "-pedantic -Wall -Wextra -Wno-override-init -O0 -g -fno-omit-frame-pointer -fno-inline" -LDFLAGS := "-lcriterion" +LDFLAGS := if os() == "macos" { + "$(pkg-config --libs --cflags criterion)" +} else { + "-lcriterion" +} TESTBIN := "/tmp/all_tests" set shell := ["bash", "-cu"] FOOBAR := "foobar" +FOOBAR_SOURCE := FOOBAR+".c" +FOOBAR_EXPANDED := FOOBAR+"-expanded.c" run_foobar: build_foobar ./{{FOOBAR}} -build_foobar: - {{CC}} ./foobar.c -o {{FOOBAR}} {{CFLAGS}} +build_foobar: foobar_expanded + {{CC}} {{FOOBAR_EXPANDED}} -o {{FOOBAR}} {{CFLAGS}} + +foobar_expanded: + {{CC}} -P -E {{FOOBAR_SOURCE}} -o {{FOOBAR_EXPANDED}} + clang-format -i {{FOOBAR_EXPANDED}} clean_foobar: rm {{FOOBAR}} +[default] test: build - {{TESTBIN}} + {{TESTBIN}} test_memcheck: build - valgrind --leak-check=full \ - --show-leak-kinds=all \ - --trace-children=yes \ - --error-exitcode=1 \ - {{TESTBIN}} + valgrind --leak-check=full \ + --show-leak-kinds=all \ + --trace-children=yes \ + --error-exitcode=1 \ + {{TESTBIN}} build: - find "$PWD" -type f -name 'test*.c' | xargs -r {{CC}} {{CFLAGS}} _allocator_impl.c -o {{TESTBIN}} {{LDFLAGS}} + find . -type f -name 'test*.c' | xargs -r {{CC}} {{CFLAGS}} {{LDFLAGS}} _allocator_impl.c -o {{TESTBIN}} + +DEPENDENCY_HEADERS := if os() == "macos" { + "$(pkg-config --cflags-only-I criterion | sed 's/-I//g')" +} else { + "TODO" +} tag: - find "$PWD" -type f \( -name '*.c' -o -name '*.h' \) | xargs ctags + echo "Making tags from project and headers in {{DEPENDENCY_HEADERS}}" + ctags --languages=c --langmap=c:.c.h -R {{DEPENDENCY_HEADERS}} . diff --git a/map.c b/map.c index de0c58d..18578a7 100644 --- a/map.c +++ b/map.c @@ -102,7 +102,7 @@ void *map_create_func( static const uint8_t *internal_cig_key_ptr_from_pair_ptr(const void *this, const void *pair) { map_header_t *header = PTR_FROM_FIELD_PTR(map_header_t, bytes, this); - return (const uint8_t *) &((const char *)pair)[header->key_offset]; + return (const uint8_t *) (&((const char *)pair)[header->key_offset]); } // TODO: just make these internal probably @@ -238,6 +238,11 @@ void map_assure_growable_by_1(void **this, const char *file, int line) { // This is okay, because we absolutely know that the keys don't already // exist. (assuming all the other code for map is doing it's job) + // TODO: when the header is retrieved within the map_pair_hash function the address is completely wrong. Scrambles the parameters and fucks everyghing!!! + // TODO!!! + // TODO!!! + // TODO!!! + // TODO!!! unsigned int mapping_index = map_pair_hash(this, pair).new_index; header->mapping_arr[mapping_index] = i; } diff --git a/test_arena_allocator.c b/test_arena_allocator.c index 7fe2176..ef76142 100644 --- a/test_arena_allocator.c +++ b/test_arena_allocator.c @@ -185,7 +185,7 @@ Test(arena_allocator, dyn_array_resize_pattern) { allocator_t arena = arena_allocator_create(backing, 10 * KB); allocator_reset(arena); - int *arr = make_arr(int, arena); + int *arr = arr_make(int, arena); // Add 100 elements, forcing multiple resizes for (int i = 0; i < 100; i++) { @@ -206,8 +206,8 @@ Test(arena_allocator, multiple_arrays_resize) { allocator_t arena = arena_allocator_create(backing, 20 * KB); allocator_reset(arena); - int *arr1 = make_arr(int, arena); - int *arr2 = make_arr(int, arena); + int *arr1 = arr_make(int, arena); + int *arr2 = arr_make(int, arena); // Add to arr1 for (int i = 0; i < 50; i++) { @@ -266,11 +266,11 @@ Test(arena_allocator, nested_structures_with_resize) { allocator_reset(arena); // Create array of int pointers (like dyn_array of dyn_arrays) - int **rows = make_arr(int*, arena); + int **rows = arr_make(int*, arena); // Add 5 rows for (int i = 0; i < 5; i++) { - int *row = make_arr(int, arena); + int *row = arr_make(int, arena); for (int j = 0; j < 10; j++) { arr_append(row, i * 10 + j); } diff --git a/test_dynamic_array.c b/test_dynamic_array.c index 6bfa4fb..189e7da 100644 --- a/test_dynamic_array.c +++ b/test_dynamic_array.c @@ -8,7 +8,7 @@ static bool int_eq(const void *a, const void *b) { Test(dynamic_arrays, append) { with_borrow(alloc) { - int *numbers = make_arr(int, alloc); + int *numbers = arr_make(int, alloc); arr_append(numbers, 40); arr_append(numbers, 41); arr_append(numbers, 42); @@ -28,7 +28,7 @@ Test(dynamic_arrays, append) { Test(dynamic_arrays, pop) { with_borrow(alloc) { - int *numbers = make_arr(int, alloc); + int *numbers = arr_make(int, alloc); arr_append(numbers, 40); arr_append(numbers, 41); arr_append(numbers, 42); @@ -55,7 +55,7 @@ Test(dynamic_arrays, pop) { Test(dynamic_arrays, contains) { with_borrow(alloc) { - int *numbers = make_arr(int, alloc); + int *numbers = arr_make(int, alloc); arr_append(numbers, 20); cr_expect(arr_contains(numbers, (int){20})); arr_reset(numbers); @@ -75,7 +75,7 @@ Test(dynamic_arrays, contains) { Test(dynamic_arrays, contains_found) { with_borrow(alloc) { - int *arr = make_arr(int, alloc, .initial_capacity = 5); + int *arr = arr_make(int, alloc, .initial_capacity = 5); arr_append(arr, 10); arr_append(arr, 20); arr_append(arr, 30); @@ -88,7 +88,7 @@ Test(dynamic_arrays, contains_found) { Test(dynamic_arrays, contains_not_found) { with_borrow(alloc) { - int *arr = make_arr(int, alloc, .initial_capacity = 5); + int *arr = arr_make(int, alloc, .initial_capacity = 5); arr_append(arr, 10); arr_append(arr, 20); arr_append(arr, 30); @@ -101,7 +101,7 @@ Test(dynamic_arrays, contains_not_found) { Test(dynamic_arrays, contains_empty) { with_borrow(alloc) { - int *arr = make_arr(int, alloc, .initial_capacity = 5); + int *arr = arr_make(int, alloc, .initial_capacity = 5); cr_assert_not(arr_contains(arr, (int){10})); } @@ -110,7 +110,7 @@ Test(dynamic_arrays, contains_empty) { Test(dynamic_arrays, contains_cmp_found) { with_borrow(alloc) { - int *arr = make_arr(int, alloc, .initial_capacity = 5); + int *arr = arr_make(int, alloc, .initial_capacity = 5); arr_append(arr, 10); arr_append(arr, 20); arr_append(arr, 30); @@ -123,7 +123,7 @@ Test(dynamic_arrays, contains_cmp_found) { Test(dynamic_arrays, contains_cmp_not_found) { with_borrow(alloc) { - int *arr = make_arr(int, alloc, .initial_capacity = 5); + int *arr = arr_make(int, alloc, .initial_capacity = 5); arr_append(arr, 10); arr_append(arr, 20); arr_append(arr, 30); @@ -136,7 +136,7 @@ Test(dynamic_arrays, contains_cmp_not_found) { Test(dynamic_arrays, contains_cmp_empty) { with_borrow(alloc) { - int *arr = make_arr(int, alloc, .initial_capacity = 5); + int *arr = arr_make(int, alloc, .initial_capacity = 5); cr_assert_not(arr_contains_cmp(arr, int_eq, (int){10})); } @@ -156,7 +156,7 @@ static bool kv_eq_by_key(const void *a, const void *b) { Test(dynamic_arrays, contains_cmp_key_value_map) { with_borrow(alloc) { - kv_pair_t *map = make_arr(kv_pair_t, alloc, .initial_capacity = 10); + kv_pair_t *map = arr_make(kv_pair_t, alloc, .initial_capacity = 10); // Add some key-value pairs arr_append(map, ((kv_pair_t){.key = 1, .value = "one"})); @@ -176,7 +176,7 @@ Test(dynamic_arrays, contains_cmp_key_value_map) { Test(dynamic_arrays, get_valid_indices) { with_borrow(alloc) { - int *arr = make_arr(int, alloc); + int *arr = arr_make(int, alloc); arr_append(arr, 10); arr_append(arr, 20); arr_append(arr, 30); @@ -191,7 +191,7 @@ Test(dynamic_arrays, get_valid_indices) { Test(dynamic_arrays, set_valid_indices) { with_borrow(alloc) { - int *arr = make_arr(int, alloc); + int *arr = arr_make(int, alloc); arr_append(arr, 10); arr_append(arr, 20); arr_append(arr, 30); @@ -208,7 +208,7 @@ Test(dynamic_arrays, set_valid_indices) { Test(dynamic_arrays, get_out_of_bounds, .exit_code = 1) { with_borrow(alloc) { - int *arr = make_arr(int, alloc); + int *arr = arr_make(int, alloc); arr_append(arr, 10); arr_append(arr, 20); dyn_array_bounds_check_func( arr, 2, __FILE__, __LINE__, false); @@ -217,7 +217,7 @@ Test(dynamic_arrays, get_out_of_bounds, .exit_code = 1) { Test(dynamic_arrays, set_out_of_bounds, .exit_code = 1) { with_borrow(alloc) { - int *arr = make_arr(int, alloc); + int *arr = arr_make(int, alloc); arr_append(arr, 10); arr_append(arr, 20); @@ -227,7 +227,7 @@ Test(dynamic_arrays, set_out_of_bounds, .exit_code = 1) { Test(dynamic_arrays, get_empty_array, .exit_code = 1) { with_borrow(alloc) { - int *arr = make_arr(int, alloc); + int *arr = arr_make(int, alloc); dyn_array_bounds_check_func( arr, 0, __FILE__, __LINE__, false); } } diff --git a/test_map.c b/test_map.c new file mode 100644 index 0000000..fc11f86 --- /dev/null +++ b/test_map.c @@ -0,0 +1,34 @@ +#include +#include "cig.h" +#include + +#define FLAG_FREE (-1) +#define FLAG_TOMBSTONE (-2) + +static int count_filled(void *map) { + map_header_t *header = PTR_FROM_FIELD_PTR(map_header_t, bytes, map); + int count = 0; + for (unsigned int i = 0; i < header->mapping_capacity; i++) { + if (header->mapping_arr[i] >= 0) { + count++; + } + } + return count; +} + +Test(map, test) { + with_borrow(allocator) { + int *s; + set_init(&s, allocator); + int count = count_filled((void*)s); + cr_assert_eq(count, 0, "%d != 0", count); + + for (int i = 0; i < 10; i++) { + set_add(&s, 1); + count = count_filled((void*)s); + cr_assert_eq(count, 1, "%d != 0", count); + map_print_mapping_state((void*)s, stdout); + fflush(stdout); + } + } +}