made a discovery of some kind of fuckup

This commit is contained in:
2026-05-05 23:05:53 +02:00
parent 5dcddd3474
commit d0d98170ea
9 changed files with 105 additions and 43 deletions
+1
View File
@@ -1,3 +1,4 @@
tags tags
*.un~ *.un~
/foobar /foobar
/foobar-expanded.c
+4 -3
View File
@@ -156,14 +156,14 @@ typedef struct dyn_array_create_func_args {
} dyn_array_create_func_args_t; } dyn_array_create_func_args_t;
void *dyn_array_create_func(dyn_array_create_func_args_t args); 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), \ .item_size = sizeof(TYPE), \
.file = __FILE__, \ .file = __FILE__, \
.line = __LINE__, \ .line = __LINE__, \
.allocator = __VA_ARGS__, \ .allocator = __VA_ARGS__, \
})) }))
#define init_arr(PTR, ...) do { \ #define arr_init(PTR, ...) do { \
(*(PTR)) = dyn_array_create_func((dyn_array_create_func_args_t){ \ (*(PTR)) = dyn_array_create_func((dyn_array_create_func_args_t){ \
.item_size = sizeof(*(*(PTR))), \ .item_size = sizeof(*(*(PTR))), \
.file = __FILE__, \ .file = __FILE__, \
@@ -269,7 +269,7 @@ typedef struct map_create_func_args {
void *map_create_func(map_create_func_args_t 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) { \ (*(PTR)) = map_create_func((map_create_func_args_t) { \
.item_size=sizeof(*(*(PTR))), \ .item_size=sizeof(*(*(PTR))), \
.key_size=sizeof((*(PTR))->key), \ .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 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); 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 // TODO: NOT DONE!!! FIRST try to get the existing in
#define set_add(THIS, VALUE) do { \ #define set_add(THIS, VALUE) do { \
+1 -1
View File
@@ -19,7 +19,7 @@ char *read_entire_file(const char *path, allocator_t allocator) {
// Returns dynamic array, of fixed char strings. // Returns dynamic array, of fixed char strings.
char **read_all_file_lines(const char *path, allocator_t allocator) { char **read_all_file_lines(const char *path, allocator_t allocator) {
char *contents = read_entire_file(path, allocator); char *contents = read_entire_file(path, allocator);
char **lines = make_arr(char*, allocator); char **lines = arr_make(char*, allocator);
arr_append(lines, contents); arr_append(lines, contents);
bool just_split = false; bool just_split = false;
for (char *c = contents; (*c)!='\0'; c++) { for (char *c = contents; (*c)!='\0'; c++) {
+9 -6
View File
@@ -3,12 +3,15 @@
int main(void) { int main(void) {
with_borrow(allocator) { with_borrow(allocator) {
int *ints; int *s;
set_init(&ints, allocator, .initial_capacity=20); set_init(&s, allocator);
map_print_mapping_state(ints, stdout); map_print_mapping_state((void*)s, stdout);
for ( int i = 0; i < 100; i++ ) { fflush(stdout);
set_add(&ints, i);
map_print_mapping_state(ints, stdout); for (int i = 0; i < 10; i++) {
set_add(&s, 1);
map_print_mapping_state((void*)s, stdout);
fflush(stdout);
} }
} }
} }
+24 -6
View File
@@ -1,21 +1,32 @@
CC := "zig cc" CC := "cc"
CFLAGS := "-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" LDFLAGS := if os() == "macos" {
"$(pkg-config --libs --cflags criterion)"
} else {
"-lcriterion"
}
TESTBIN := "/tmp/all_tests" TESTBIN := "/tmp/all_tests"
set shell := ["bash", "-cu"] set shell := ["bash", "-cu"]
FOOBAR := "foobar" FOOBAR := "foobar"
FOOBAR_SOURCE := FOOBAR+".c"
FOOBAR_EXPANDED := FOOBAR+"-expanded.c"
run_foobar: build_foobar run_foobar: build_foobar
./{{FOOBAR}} ./{{FOOBAR}}
build_foobar: build_foobar: foobar_expanded
{{CC}} ./foobar.c -o {{FOOBAR}} {{CFLAGS}} {{CC}} {{FOOBAR_EXPANDED}} -o {{FOOBAR}} {{CFLAGS}}
foobar_expanded:
{{CC}} -P -E {{FOOBAR_SOURCE}} -o {{FOOBAR_EXPANDED}}
clang-format -i {{FOOBAR_EXPANDED}}
clean_foobar: clean_foobar:
rm {{FOOBAR}} rm {{FOOBAR}}
[default]
test: build test: build
{{TESTBIN}} {{TESTBIN}}
@@ -27,7 +38,14 @@ test_memcheck: build
{{TESTBIN}} {{TESTBIN}}
build: 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: 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}} .
+6 -1
View File
@@ -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) { 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); 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 // 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 // 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) // 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; unsigned int mapping_index = map_pair_hash(this, pair).new_index;
header->mapping_arr[mapping_index] = i; header->mapping_arr[mapping_index] = i;
} }
+5 -5
View File
@@ -185,7 +185,7 @@ Test(arena_allocator, dyn_array_resize_pattern) {
allocator_t arena = arena_allocator_create(backing, 10 * KB); allocator_t arena = arena_allocator_create(backing, 10 * KB);
allocator_reset(arena); allocator_reset(arena);
int *arr = make_arr(int, arena); int *arr = arr_make(int, arena);
// Add 100 elements, forcing multiple resizes // Add 100 elements, forcing multiple resizes
for (int i = 0; i < 100; i++) { 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_t arena = arena_allocator_create(backing, 20 * KB);
allocator_reset(arena); allocator_reset(arena);
int *arr1 = make_arr(int, arena); int *arr1 = arr_make(int, arena);
int *arr2 = make_arr(int, arena); int *arr2 = arr_make(int, arena);
// Add to arr1 // Add to arr1
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
@@ -266,11 +266,11 @@ Test(arena_allocator, nested_structures_with_resize) {
allocator_reset(arena); allocator_reset(arena);
// Create array of int pointers (like dyn_array of dyn_arrays) // 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 // Add 5 rows
for (int i = 0; i < 5; i++) { 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++) { for (int j = 0; j < 10; j++) {
arr_append(row, i * 10 + j); arr_append(row, i * 10 + j);
} }
+15 -15
View File
@@ -8,7 +8,7 @@ static bool int_eq(const void *a, const void *b) {
Test(dynamic_arrays, append) { Test(dynamic_arrays, append) {
with_borrow(alloc) { with_borrow(alloc) {
int *numbers = make_arr(int, alloc); int *numbers = arr_make(int, alloc);
arr_append(numbers, 40); arr_append(numbers, 40);
arr_append(numbers, 41); arr_append(numbers, 41);
arr_append(numbers, 42); arr_append(numbers, 42);
@@ -28,7 +28,7 @@ Test(dynamic_arrays, append) {
Test(dynamic_arrays, pop) { Test(dynamic_arrays, pop) {
with_borrow(alloc) { with_borrow(alloc) {
int *numbers = make_arr(int, alloc); int *numbers = arr_make(int, alloc);
arr_append(numbers, 40); arr_append(numbers, 40);
arr_append(numbers, 41); arr_append(numbers, 41);
arr_append(numbers, 42); arr_append(numbers, 42);
@@ -55,7 +55,7 @@ Test(dynamic_arrays, pop) {
Test(dynamic_arrays, contains) { Test(dynamic_arrays, contains) {
with_borrow(alloc) { with_borrow(alloc) {
int *numbers = make_arr(int, alloc); int *numbers = arr_make(int, alloc);
arr_append(numbers, 20); arr_append(numbers, 20);
cr_expect(arr_contains(numbers, (int){20})); cr_expect(arr_contains(numbers, (int){20}));
arr_reset(numbers); arr_reset(numbers);
@@ -75,7 +75,7 @@ Test(dynamic_arrays, contains) {
Test(dynamic_arrays, contains_found) { Test(dynamic_arrays, contains_found) {
with_borrow(alloc) { 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, 10);
arr_append(arr, 20); arr_append(arr, 20);
arr_append(arr, 30); arr_append(arr, 30);
@@ -88,7 +88,7 @@ Test(dynamic_arrays, contains_found) {
Test(dynamic_arrays, contains_not_found) { Test(dynamic_arrays, contains_not_found) {
with_borrow(alloc) { 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, 10);
arr_append(arr, 20); arr_append(arr, 20);
arr_append(arr, 30); arr_append(arr, 30);
@@ -101,7 +101,7 @@ Test(dynamic_arrays, contains_not_found) {
Test(dynamic_arrays, contains_empty) { Test(dynamic_arrays, contains_empty) {
with_borrow(alloc) { 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})); cr_assert_not(arr_contains(arr, (int){10}));
} }
@@ -110,7 +110,7 @@ Test(dynamic_arrays, contains_empty) {
Test(dynamic_arrays, contains_cmp_found) { Test(dynamic_arrays, contains_cmp_found) {
with_borrow(alloc) { 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, 10);
arr_append(arr, 20); arr_append(arr, 20);
arr_append(arr, 30); arr_append(arr, 30);
@@ -123,7 +123,7 @@ Test(dynamic_arrays, contains_cmp_found) {
Test(dynamic_arrays, contains_cmp_not_found) { Test(dynamic_arrays, contains_cmp_not_found) {
with_borrow(alloc) { 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, 10);
arr_append(arr, 20); arr_append(arr, 20);
arr_append(arr, 30); arr_append(arr, 30);
@@ -136,7 +136,7 @@ Test(dynamic_arrays, contains_cmp_not_found) {
Test(dynamic_arrays, contains_cmp_empty) { Test(dynamic_arrays, contains_cmp_empty) {
with_borrow(alloc) { 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})); 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) { Test(dynamic_arrays, contains_cmp_key_value_map) {
with_borrow(alloc) { 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 // Add some key-value pairs
arr_append(map, ((kv_pair_t){.key = 1, .value = "one"})); 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) { Test(dynamic_arrays, get_valid_indices) {
with_borrow(alloc) { with_borrow(alloc) {
int *arr = make_arr(int, alloc); int *arr = arr_make(int, alloc);
arr_append(arr, 10); arr_append(arr, 10);
arr_append(arr, 20); arr_append(arr, 20);
arr_append(arr, 30); arr_append(arr, 30);
@@ -191,7 +191,7 @@ Test(dynamic_arrays, get_valid_indices) {
Test(dynamic_arrays, set_valid_indices) { Test(dynamic_arrays, set_valid_indices) {
with_borrow(alloc) { with_borrow(alloc) {
int *arr = make_arr(int, alloc); int *arr = arr_make(int, alloc);
arr_append(arr, 10); arr_append(arr, 10);
arr_append(arr, 20); arr_append(arr, 20);
arr_append(arr, 30); arr_append(arr, 30);
@@ -208,7 +208,7 @@ Test(dynamic_arrays, set_valid_indices) {
Test(dynamic_arrays, get_out_of_bounds, .exit_code = 1) { Test(dynamic_arrays, get_out_of_bounds, .exit_code = 1) {
with_borrow(alloc) { with_borrow(alloc) {
int *arr = make_arr(int, alloc); int *arr = arr_make(int, alloc);
arr_append(arr, 10); arr_append(arr, 10);
arr_append(arr, 20); arr_append(arr, 20);
dyn_array_bounds_check_func( arr, 2, __FILE__, __LINE__, false); 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) { Test(dynamic_arrays, set_out_of_bounds, .exit_code = 1) {
with_borrow(alloc) { with_borrow(alloc) {
int *arr = make_arr(int, alloc); int *arr = arr_make(int, alloc);
arr_append(arr, 10); arr_append(arr, 10);
arr_append(arr, 20); 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) { Test(dynamic_arrays, get_empty_array, .exit_code = 1) {
with_borrow(alloc) { 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); dyn_array_bounds_check_func( arr, 0, __FILE__, __LINE__, false);
} }
} }
+34
View File
@@ -0,0 +1,34 @@
#include <criterion/criterion.h>
#include "cig.h"
#include <stdio.h>
#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);
}
}
}