Implemented cleanup stack
Fabio Cevasco h3rald@h3rald.com
Wed, 01 Jan 2025 13:56:42 +0100
3 files changed,
35 insertions(+),
0 deletions(-)
M
src/hex.h
→
src/hex.h
@@ -26,6 +26,7 @@ #define HEX_INITIAL_REGISTRY_SIZE 128
#define HEX_REGISTRY_SIZE 1024 #define HEX_STACK_SIZE 256 #define HEX_STACK_TRACE_SIZE 16 +#define HEX_CLEANUP_STACK_SIZE 16 #define HEX_NATIVE_SYMBOLS 64 #define HEX_MAX_SYMBOL_LENGTH 256 #define HEX_MAX_USER_SYMBOLS (HEX_REGISTRY_SIZE - HEX_NATIVE_SYMBOLS)@@ -90,6 +91,12 @@ hex_token_t **entries;
int start; // Index of the oldest item size_t size; // Current number of items in the buffer } hex_stack_trace_t; + +typedef struct hex_cleanup_stack_t +{ + hex_item_t *entries[16]; + size_t count; +} hex_cleanup_stack_t; typedef struct hex_stack_t {@@ -145,6 +152,7 @@ {
hex_stack_t *stack; hex_registry_t *registry; hex_stack_trace_t *stack_trace; + hex_cleanup_stack_t *cleanup_stack; hex_settings_t *settings; hex_doc_dictionary_t *docs; hex_symbol_table_t *symbol_table;@@ -288,6 +296,10 @@ hex_item_t *hex_pop(hex_context_t *ctx);
hex_item_t *hex_copy_item(hex_context_t *ctx, const hex_item_t *item); hex_token_t *hex_copy_token(hex_context_t *ctx, const hex_token_t *token); +// Cleanup stack management +void hex_cleanup_push(hex_context_t *ctx, hex_item_t *item); +void hex_cleanup_free(hex_context_t *ctx); + // Parser and interpreter hex_token_t *hex_next_token(hex_context_t *ctx, const char **input, hex_file_position_t *position); int32_t hex_parse_integer(const char *hex_str);@@ -415,5 +427,6 @@ #define HEX_POP(ctx, x) hex_item_t *x = hex_pop(ctx)
#define HEX_FREE(ctx, x) hex_free_item(ctx, x) #define HEX_PUSH(ctx, x) hex_push(ctx, x) #define HEX_ALLOC(x) hex_item_t *x = (hex_item_t *)malloc(sizeof(hex_item_t)); +#define HEX_CLEANUP(ctx) hex_cleanup_free(ctx); #endif // HEX_H
M
src/interpreter.c
→
src/interpreter.c
@@ -22,6 +22,7 @@ context->stack_trace = malloc(sizeof(hex_stack_trace_t));
context->stack_trace->entries = malloc(HEX_STACK_TRACE_SIZE * sizeof(hex_token_t)); context->stack_trace->start = 0; context->stack_trace->size = 0; + context->cleanup_stack->count = 0 context->settings = malloc(sizeof(hex_settings_t)); context->settings->debugging_enabled = 0; context->settings->errors_enabled = 1;
M
src/stack.c
→
src/stack.c
@@ -221,6 +221,7 @@ }
*item = *ctx->stack->entries[ctx->stack->top]; hex_debug_item(ctx, " POP", item); ctx->stack->top--; + hex_cleanup_push(ctx, item); return item; }@@ -466,3 +467,23 @@ }
return copy; } + +void hex_cleanup_push(hex_context_t *ctx, hex_item_t *item) +{ + if (ctx->cleanup_stack->count < HEX_CLEANUP_STACK_SIZE) { // Ensure we don't overflow the array + ctx->cleanup_stack->entries[ctx->cleanup_stack->count++] = item; + } + else + { + hex_error(ctx, "[cleanup push] Cleanup stack is full"); + } +} + +void hex_cleanup_free(hex_context_t *ctx) +{ + for (int i = 0; i < ctx->cleanup_stack->count; i++) { + HEX_FREE(ctx, ctx->cleanup_stack->entries[i]); + } + ctx->cleanup_stack->count = 0; +} +