diff --git a/CMakeLists.txt b/CMakeLists.txt index e80e5f9..f274f86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.28) project(HAL64 C) set(CMAKE_C_STANDARD 90) -add_compile_options(-Ofast) +add_compile_options(-O0) set(LEXER_DIR "${CMAKE_CURRENT_BINARY_DIR}") set(LEXER_OUT "${LEXER_DIR}/lexer.c") diff --git a/src/hal64.c b/src/hal64.c index f83bf9e..bbb6213 100644 --- a/src/hal64.c +++ b/src/hal64.c @@ -4,12 +4,26 @@ #include "hal64.h" #include "utils/memory.h" +static void +free_instructions(Instruction *instructions, size_t count) +{ + size_t i; + for (i = 0; i < count; i++) { + if (instructions[i].op == OP_PUSH_LITERAL_STRING) + free(instructions[i].data.string.ptr); + } +} + void free_program(Program program) { size_t i; - for (i = 0; i < program.functions_count; i++) + for (i = 0; i < program.functions_count; i++) { + free_instructions( + program.functions[i].instructions, + program.functions[i].instructions_count); free(program.functions[i].instructions); + } free(program.functions); } diff --git a/src/vm.c b/src/vm.c index 542fbfa..f7a0dc6 100644 --- a/src/vm.c +++ b/src/vm.c @@ -16,6 +16,7 @@ init_vm(void) vm.operands_stack.capacity = 1024; vm.pointers_stack.capacity = 1024; vm.objects.capacity = 1024; + vm.allocated_heap_size = 0; vm.call_stack.data = safe_malloc(vm.call_stack.capacity * sizeof(uint64_t)); vm.operands_stack.data = safe_malloc(vm.operands_stack.capacity * sizeof(uint64_t)); vm.pointers_stack.data = safe_malloc(vm.pointers_stack.capacity * sizeof(HeapObject *)); @@ -26,8 +27,15 @@ init_vm(void) void free_vm(VM vm) { + size_t i; free(vm.call_stack.data); free(vm.operands_stack.data); + free(vm.pointers_stack.data); + for (i = 0; i < vm.objects.size; i++) { + free(vm.objects.data[i]->data); + free(vm.objects.data[i]); + } + free(vm.objects.data); } static void @@ -45,6 +53,7 @@ gc_sweep(VM *vm) size_t i; for (i = 0; i < vm->objects.size; i++) { if (!vm->objects.data[i]->marked) { + vm->allocated_heap_size -= vm->objects.data[i]->size; free(vm->objects.data[i]->data); free(vm->objects.data[i]); vm->objects.data[i] = vm->objects.data[vm->objects.size - 1];