diff --git a/cig.h b/cig.h index 097fefe..ee5cf68 100644 --- a/cig.h +++ b/cig.h @@ -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 GB (KB * KB * KB) #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 // malloc, realloc and free. @@ -239,11 +235,15 @@ void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn); // maps //////////////////////////////////////////////////////////////////// -typedef int (*hash_func_t)(void *key); -typedef int (*equals_func_t)(void *key1, void *key2); +typedef unsigned int (*hash_func_t)(void *key); +typedef bool (*equals_func_t)(void *key1, void *key2); 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; hash_func_t hash; equals_func_t equals; diff --git a/justfile b/justfile new file mode 100644 index 0000000..ab443b5 --- /dev/null +++ b/justfile @@ -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 diff --git a/makefile b/makefile index ce3db8c..05ed79e 100644 --- a/makefile +++ b/makefile @@ -30,9 +30,3 @@ CRITERION_HEADERS := $(shell find /usr/include/criterion) ALL_FILES := $(PROJECT_C_FILES) $(CRITERION_HEADERS) tags: $(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 diff --git a/map.c b/map.c index 83d4310..e426738 100644 --- a/map.c +++ b/map.c @@ -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); - TODO( - Overwrite the mapping arr with -1, then iterate through the items - arr and find the hashes of the values to populate the mappings - ); + static_assert(0, "Overwrite the mapping arr with -1, then iterate" + "through the items arr and find the hashes of the values to populate the" + "mappings"); + + static_assert(0, "make sure the mapping_arr field is set to the new and" + "correct pointer!"); + } 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); 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) {