diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-11-23 06:08:38 +0100 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-11-23 06:08:38 +0100 |
commit | 66d7b0398ef5cad84d474f5db1fcccd1f36c0e88 (patch) | |
tree | 47b7a8cd030b6f653b845fe129ee1904ed8f8d8c /bxgen.c | |
parent | c6052ed87c983f35bb53207f1add7d642c9f34f9 (diff) | |
download | bxgen-66d7b0398ef5cad84d474f5db1fcccd1f36c0e88.zip |
Simple string table impl
Diffstat (limited to 'bxgen.c')
-rwxr-xr-x | bxgen.c | 89 |
1 files changed, 81 insertions, 8 deletions
@@ -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 @@ -809,6 +807,81 @@ u64 u64_from_dec_str(c8 *s, c8 *s_end) { // ================================================================ // +// * 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) { |