some progress, but mainly convert the TODO's into static_assert(0, ...)

This commit is contained in:
2026-04-07 22:48:51 +02:00
parent da4de1c560
commit 96c25dc3ad
4 changed files with 60 additions and 18 deletions
+7 -7
View File
@@ -15,10 +15,6 @@ typedef union any_align { char c; int i; long l; long long ll; float f; double d
#define MB (KB * KB) #define MB (KB * KB)
#define GB (KB * KB * KB) #define GB (KB * KB * KB)
#define PTR_FROM_FIELD_PTR(STRUCT, FIELD, PTR) ((STRUCT *) (((char *) PTR) - offsetof(STRUCT, FIELD))) #define PTR_FROM_FIELD_PTR(STRUCT, FIELD, PTR) ((STRUCT *) (((char *) PTR) - offsetof(STRUCT, FIELD)))
#define TODO(...) do { \
fprintf(stderr, "%s:%d: TODO: %s", __FILE__, (int)__LINE__, #__VA_ARGS__);\
exit(1); \
} while (0)
// Contains all operations an allocator can do. Similar interface to sdtlibs // Contains all operations an allocator can do. Similar interface to sdtlibs
// malloc, realloc and free. // malloc, realloc and free.
@@ -239,11 +235,15 @@ void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn);
// maps //////////////////////////////////////////////////////////////////// // maps ////////////////////////////////////////////////////////////////////
typedef int (*hash_func_t)(void *key); typedef unsigned int (*hash_func_t)(void *key);
typedef int (*equals_func_t)(void *key1, void *key2); typedef bool (*equals_func_t)(void *key1, void *key2);
typedef struct map_header { typedef struct map_header {
int n_items, capacity, itemsize, keysize, mapping_capacity; int capacity;
int itemsize;
int keysize;
int mapping_capacity;
int n_items;
allocator_t allocator; allocator_t allocator;
hash_func_t hash; hash_func_t hash;
equals_func_t equals; equals_func_t equals;
+22
View File
@@ -0,0 +1,22 @@
CC := "zig cc"
CFLAGS := "-pedantic -Wall -Wextra -Wno-override-init -O0 -g -fno-omit-frame-pointer -fno-inline"
LDFLAGS := "-lcriterion"
TESTBIN := "/tmp/all_tests"
set shell := ["bash", "-cu"]
test: build
{{TESTBIN}}
test_memcheck: build
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}}
tags:
find "$PWD" -type f \( -name '*.c' -o -name '*.h' \) | xargs ctags
-6
View File
@@ -30,9 +30,3 @@ CRITERION_HEADERS := $(shell find /usr/include/criterion)
ALL_FILES := $(PROJECT_C_FILES) $(CRITERION_HEADERS) ALL_FILES := $(PROJECT_C_FILES) $(CRITERION_HEADERS)
tags: $(ALL_FILES) tags: $(ALL_FILES)
ctags -R $(ALL_FILES) ctags -R $(ALL_FILES)
scratch: /tmp/scratch
valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 /tmp/scratch
/tmp/scratch: scratch.c
gcc scratch.c -o /tmp/scratch
+31 -5
View File
@@ -75,10 +75,13 @@ void *map_grow_func(void *this, const char *file, int line) {
); );
header = allocator_resize_func(allocator, header, new_size.bytes, file, line); header = allocator_resize_func(allocator, header, new_size.bytes, file, line);
TODO( static_assert(0, "Overwrite the mapping arr with -1, then iterate"
Overwrite the mapping arr with -1, then iterate through the items "through the items arr and find the hashes of the values to populate the"
arr and find the hashes of the values to populate the mappings "mappings");
);
static_assert(0, "make sure the mapping_arr field is set to the new and"
"correct pointer!");
} }
header->n_items = new_size; header->n_items = new_size;
@@ -97,7 +100,30 @@ void map_shrink_func(void *this, const char *file, int line) {
} }
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);
header->n_items--; header->n_items--;
TODO(Hash the key at the removed item to find it in the mapping array, and set it to the gravestone value.); static_assert(0, "Hash the key at the removed item to find it in the mapping array, and set it to the gravestone value.");
}
static inline uint32_t fnv_1a(const uint8_t *bytes, const int length) {
// Yanked from: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
const uint32_t PRIME = 0x01000193;
const uint32_t OFFSET = 0x811c9dc5;
uint32_t hash = OFFSET;
for (int i = 0; i < length; i++) {
uint32_t byte = (uint32_t)bytes[i];
hash = hash ^ byte;
hash = hash * PRIME;
}
return hash;
}
unsigned int map_hash(void *this, void *value) {
map_header_t *header = PTR_FROM_FIELD_PTR(map_header_t, bytes, this);
if (header->hash) {
static_assert(0, "hash using the hasing function");
return;
}
static_assert(0, "WRONG!!!, this should hash the fucking value, not the god dammned entire hashmap of values");
return (int) fnv_1a(header->bytes, header->n_items*header->itemsize) % header->mapping_capacity;
} }
int map_len(void *this) { int map_len(void *this) {