Refactor symbol handling in hex_symbol_symbols; improves memory management and consistency by using hex_string_item constructor.
h3rald h3rald@h3rald.com
Wed, 10 Sep 2025 18:32:03 +0200
2 files changed,
22 insertions(+),
61 deletions(-)
M
src/hex.c
→
src/hex.c
@@ -426,6 +426,7 @@ char *hex_symboltable_get_value(hex_context_t *ctx, uint16_t index);
int hex_decode_bytecode_symboltable(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t count); uint8_t *hex_encode_bytecode_symboltable(hex_context_t *ctx, size_t *out_size); hex_symbol_table_t *hex_symboltable_copy(hex_context_t *ctx); +void hex_symboltable_destroy(hex_symbol_table_t *table); // REPL and initialization void hex_register_symbols(hex_context_t *ctx);@@ -4129,42 +4130,19 @@ {
hex_registry_entry_t *entry = ctx->registry->buckets[i]; while (entry != NULL) { - // Duplicate the symbol ID - char *id = strdup(entry->key); - if (!id) - { - hex_error(ctx, "[symbol symbols] Memory allocation failed for ID"); - // Free already allocated items - for (size_t j = 0; j < quotation_size; j++) - { - free(quotation[j]->data.str_value); // Free string value - free(quotation[j]); // Free hex_item_t - } - free(quotation); - return 1; - } - - // Allocate a new hex_item_t - hex_item_t *item = (hex_item_t *)calloc(1, sizeof(hex_item_t)); + // Build a new string item using constructor for consistency + hex_item_t *item = hex_string_item(ctx, entry->key); if (!item) { - hex_error(ctx, "[symbol symbols] Memory allocation failed for hex_item_t"); - free(id); + hex_error(ctx, "[symbol symbols] Failed to allocate string item"); for (size_t j = 0; j < quotation_size; j++) { - free(quotation[j]->data.str_value); - free(quotation[j]); + hex_free_item(ctx, quotation[j]); } free(quotation); return 1; } - - // Initialize the hex_item_t as a string item - item->type = HEX_TYPE_STRING; - item->data.str_value = id; - - // Add the item to the quotation - quotation[quotation_size++] = item; + quotation[quotation_size++] = item; // ownership transferred to quotation // Move to the next entry in the bucket entry = entry->next;@@ -4177,8 +4155,7 @@ {
hex_error(ctx, "[symbol symbols] Failed to push quotation onto the stack"); for (size_t j = 0; j < quotation_size; j++) { - free(quotation[j]->data.str_value); - free(quotation[j]); + hex_free_item(ctx, quotation[j]); } free(quotation); return 1;@@ -4297,7 +4274,7 @@ for (size_t i = 0; i < item->quotation_size; i++)
{ bytecode[i] = (uint8_t)item->data.quotation_value[i]->data.int_value; } - // Sandbox current symbol table: create working copy, run, then discard mutations + // Sandbox: save current table pointer; create a working copy to mutate hex_symbol_table_t *original_table = ctx->symbol_table; hex_symbol_table_t *working_copy = hex_symboltable_copy(ctx); if (!working_copy)@@ -4309,6 +4286,7 @@ return 1;
} ctx->symbol_table = working_copy; int result = hex_interpret_bytecode(ctx, bytecode, item->quotation_size, file->data.str_value); + // Destroy mutated working copy and restore original hex_symboltable_destroy(ctx->symbol_table); ctx->symbol_table = original_table; free(bytecode);@@ -4372,6 +4350,7 @@ return 1;
} hex_raw_print_item(stdout, *item); printf("\n"); + HEX_FREE(ctx, item); return 0; }@@ -4386,6 +4365,7 @@ return 1;
} hex_raw_print_item(stderr, *item); printf("\n"); + HEX_FREE(ctx, item); return 0; }@@ -4400,6 +4380,7 @@ return 1;
} hex_raw_print_item(stdout, *item); fflush(stdout); + HEX_FREE(ctx, item); return 0; }@@ -7437,3 +7418,4 @@
hex_destroy(ctx); return 0; } +
M
src/symbols.c
→
src/symbols.c
@@ -142,42 +142,19 @@ {
hex_registry_entry_t *entry = ctx->registry->buckets[i]; while (entry != NULL) { - // Duplicate the symbol ID - char *id = strdup(entry->key); - if (!id) - { - hex_error(ctx, "[symbol symbols] Memory allocation failed for ID"); - // Free already allocated items - for (size_t j = 0; j < quotation_size; j++) - { - free(quotation[j]->data.str_value); // Free string value - free(quotation[j]); // Free hex_item_t - } - free(quotation); - return 1; - } - - // Allocate a new hex_item_t - hex_item_t *item = (hex_item_t *)calloc(1, sizeof(hex_item_t)); + // Build a new string item using constructor for consistency + hex_item_t *item = hex_string_item(ctx, entry->key); if (!item) { - hex_error(ctx, "[symbol symbols] Memory allocation failed for hex_item_t"); - free(id); + hex_error(ctx, "[symbol symbols] Failed to allocate string item"); for (size_t j = 0; j < quotation_size; j++) { - free(quotation[j]->data.str_value); - free(quotation[j]); + hex_free_item(ctx, quotation[j]); } free(quotation); return 1; } - - // Initialize the hex_item_t as a string item - item->type = HEX_TYPE_STRING; - item->data.str_value = id; - - // Add the item to the quotation - quotation[quotation_size++] = item; + quotation[quotation_size++] = item; // ownership transferred to quotation // Move to the next entry in the bucket entry = entry->next;@@ -190,8 +167,7 @@ {
hex_error(ctx, "[symbol symbols] Failed to push quotation onto the stack"); for (size_t j = 0; j < quotation_size; j++) { - free(quotation[j]->data.str_value); - free(quotation[j]); + hex_free_item(ctx, quotation[j]); } free(quotation); return 1;@@ -386,6 +362,7 @@ return 1;
} hex_raw_print_item(stdout, *item); printf("\n"); + HEX_FREE(ctx, item); return 0; }@@ -400,6 +377,7 @@ return 1;
} hex_raw_print_item(stderr, *item); printf("\n"); + HEX_FREE(ctx, item); return 0; }@@ -414,6 +392,7 @@ return 1;
} hex_raw_print_item(stdout, *item); fflush(stdout); + HEX_FREE(ctx, item); return 0; }