From c0a168204251d590ff04c47eb7c653d3ef820ffd Mon Sep 17 00:00:00 2001 From: rhuibertsjr Date: Tue, 4 Jun 2024 12:58:41 +0200 Subject: [PATCH] Fixed: Arena allocator not using arena->current --- src/arena.c | 18 ++++++++---------- src/base.h | 2 ++ src/hash_store.c | 3 ++- src/keywords.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 19 ++++++++++++------- 5 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 src/keywords.h diff --git a/src/arena.c b/src/arena.c index 1e73be6..f88e501 100644 --- a/src/arena.c +++ b/src/arena.c @@ -46,7 +46,7 @@ arena_allocate(Arena *arena, u64 size) if (current->size < pos_new && current->growable) { - Arena *new_memory_bock; + Arena *new_memory_block; if (size > ARENA_DEFAULT_RESERVE_SIZE) { @@ -56,17 +56,15 @@ arena_allocate(Arena *arena, u64 size) } else { - new_memory_bock = arena_initialize_default(); + new_memory_block = arena_initialize_default(); - if (new_memory_bock) + if (new_memory_block) { - stack_push(arena->current, new_memory_bock, prev); + new_memory_block->prev = current; + *current = *new_memory_block; - current = new_memory_bock; - - pos_mem = - memory_align(current->offset, ARENA_DEFAULT_ALIGNMENT); - pos_new = current->offset + size; + pos_mem = memory_align(current->offset, ARENA_DEFAULT_ALIGNMENT); + pos_new = pos_mem + size; } } @@ -74,7 +72,7 @@ arena_allocate(Arena *arena, u64 size) } void *memory = (void*)((u64) current->backing_buffer + pos_mem); - arena->offset = pos_new; + current->offset = pos_new; platform_memory_commit(memory, size); memset(memory, 0, size); diff --git a/src/base.h b/src/base.h index fe22ad5..1c5716e 100644 --- a/src/base.h +++ b/src/base.h @@ -32,6 +32,8 @@ typedef uint64_t b64; #define MIN(A,B) (((A)<(B))?(A):(B)) +#define array_count(a) (sizeof(a) / sizeof((a)[0])) + //= rhjr: linked-list #define check_null(p) ((p)==0) diff --git a/src/hash_store.c b/src/hash_store.c index 49eddb3..3ca91e1 100644 --- a/src/hash_store.c +++ b/src/hash_store.c @@ -41,7 +41,8 @@ hash_store_string8_to_key(Table *table, String8 data) node->data.length = data.length; node->key = result; - memory_copy(&node->data.content, &data.content, data.length); + u8 *ptr = node->data.content; + memory_copy(ptr, data.content, data.length); TableSlot *slot = &table->slots[slot_idx]; queue_push(slot->first, slot->last, node); diff --git a/src/keywords.h b/src/keywords.h new file mode 100644 index 0000000..0d41ff3 --- /dev/null +++ b/src/keywords.h @@ -0,0 +1,45 @@ +#ifndef KEYWORDS_H +#define KEYWORDS_H + +internal String8 keywords[] = { + + //- rhjr: testcase header + str8_lit_comp("testcase"), + str8_lit_comp("identificatiecode"), + str8_lit_comp("titel"), + str8_lit_comp("beschrijving"), + + //- rhjr: testcase precond. + str8_lit_comp("precondities"), + str8_lit_comp("product configuratie"), + str8_lit_comp("toestand"), + //... + + //- rhjr: testcase procedure + str8_lit_comp("testprocedure"), + + //- rhjr: expected results + str8_lit_comp("luchtwasser ventilator"), + str8_lit_comp("luchtwasser waterklep"), + str8_lit_comp("luchtwasser toestand"), + str8_lit_comp("luchtwasser vultijd"), + str8_lit_comp("luchtwasser sproeiklep"), + str8_lit_comp("luchtwasser wasserpomp"), + + //- rhjr: acties + str8_lit_comp("luchtwasser"), + str8_lit_comp("maximale-vlotter"), + str8_lit_comp("minimale-vlotter"), + + //- rhjr: values + str8_lit_comp("wassen"), + str8_lit_comp("vullen"), + + str8_lit_comp("open"), + str8_lit_comp("dicht"), + + str8_lit_comp("aan"), + str8_lit_comp("uit") +}; + +#endif diff --git a/src/main.c b/src/main.c index 32fe950..32d0a09 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include "arena.h" #include "string.h" #include "hash_store.h" +#include "keywords.h" #include "win32_platform.c" #include "arena.c" @@ -13,16 +14,20 @@ int main(void) { Table *table = hash_store_initialize(); + u64 list_size = array_count(keywords); - String8 str8_1 = str8_lit("Hello world"); - String8 str8_2 = str8_lit("ayenooo"); + for (u64 idx = 0; idx < list_size; idx += 1) + { + if (string8_match(keywords[idx], str8_lit("luchtwasser toestand"))){ + int x = 0; + } - Key key1 = hash_store_string8_to_key(table, str8_1); - Key key2 = hash_store_string8_to_key(table, str8_2); - Key key3 = hash_store_string8_to_key(table, str8_1); + Key key = hash_store_string8_to_key(table, keywords[idx]); + String8 string = hash_store_string8_from_key(table, key); - String8 string = hash_store_string8_from_key(table, key2); - printf("%.*s\n", (i32) string.length, string.content); + printf("%.*s\n", (i32) string.length, string.content); + printf("str8 arena: %lld - arena %lld\n", table->str8_arena->current->offset, table->arena->current->offset); + } return 0; };