From 66d7b0398ef5cad84d474f5db1fcccd1f36c0e88 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sat, 23 Nov 2024 06:08:38 +0100 Subject: Simple string table impl --- bxgen.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 81 insertions(+), 8 deletions(-) (limited to 'bxgen.c') diff --git a/bxgen.c b/bxgen.c index 429f242..e8883c9 100755 --- a/bxgen.c +++ b/bxgen.c @@ -308,19 +308,16 @@ enum { REL_ADD_PROC_ADDRESS, }; -// TODO typedef struct { i64 size; i64 offset; } String_Handle; -// TODO typedef struct { i64 size; i64 capacity; u8 *data; - u8 *occupied; -} Strint_Table; +} String_Table; typedef i64 Var; @@ -428,9 +425,10 @@ typedef struct { // We use one single large memory block for *everything*. typedef struct { - i64 num_entities; - i64 capacity; - Entity *entities; + i64 num_entities; + i64 capacity; + Entity * entities; + String_Table str_table; } Pool; // TEMP Codegen and linker buffers @@ -807,6 +805,81 @@ u64 u64_from_dec_str(c8 *s, c8 *s_end) { return x; } +// ================================================================ +// +// * String table +// +// ================================================================ + +String_Handle table_add(Pool *pool, i64 size, u8 *data) { + CHECK(pool != NULL, "Invalid arguments", (String_Handle) {0}); + CHECK(size > 0, "Invalid arguments", (String_Handle) {0}); + CHECK(data != NULL, "Invalid arguments", (String_Handle) {0}); + + CHECK(pool->str_table.size + size <= pool->str_table.capacity, "Out of memory", (String_Handle) {0}); + + String_Handle h = { + .size = size, + .offset = pool->str_table.size, + }; + + mem_cpy(pool->str_table.data + h.offset, data, size); + + pool->str_table.size += size; + + return h; +} + +void table_remove(Pool *pool, String_Handle h) { + (void) pool; + (void) h; +} + +u8 *table_data(Pool *pool, String_Handle h) { + CHECK(pool != NULL, "Invalid arguments", NULL); + CHECK(h.offset >= 0 && h.size > 0, "Invalid arguments", NULL); + CHECK(h.offset + h.size <= pool->str_table.size, "Invalid handle", NULL); + + return pool->str_table.data + h.offset; +} + +String_Handle table_resize(Pool *pool, String_Handle h, i64 size) { + CHECK(pool != NULL, "Invalid arguments", (String_Handle) {0}); + CHECK(size > 0, "Invalid arguments", (String_Handle) {0}); + CHECK(h.offset >= 0 && h.size > 0, "Invalid arguments", (String_Handle) {0}); + CHECK(h.offset + h.size <= pool->str_table.size, "Invalid handle", (String_Handle) {0}); + + if (h.size <= size) + return (String_Handle) { + .size = size, + .offset = h.offset, + }; + + if (h.offset + h.size == pool->str_table.size) { + CHECK(pool->str_table.size + size - h.size <= pool->str_table.capacity, "Out of memory", (String_Handle) {0}); + + pool->str_table.size += size - h.size; + + return (String_Handle) { + .size = size, + .offset = h.offset, + }; + } + + CHECK(pool->str_table.size + size <= pool->str_table.capacity, "Out of memory", (String_Handle) {0}); + + String_Handle dst = { + .size = size, + .offset = pool->str_table.size, + }; + + mem_cpy(pool->str_table.data + dst.offset, pool->str_table.data + h.offset, h.size); + + pool->str_table.size += size; + + return dst; +} + // ================================================================ // // * Semantic graph @@ -837,7 +910,7 @@ void pool_remove(Pool *pool, i64 entity, u16 type) { CHECK(pool->entities[entity].is_enabled, "Entity already removed",); CHECK(pool->entities[entity].type == type, "Invalid entity type",); - pool->entities[entity].is_enabled = 1; + pool->entities[entity].is_enabled = 0; } i64 node_init(Pool *pool, Node data) { -- cgit v1.2.3