From d0865a644c6e2c4c61148243bf1437ee44fec548 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 20 Aug 2024 14:45:13 -0400 Subject: [PATCH] VM: added deinitVM() and replaced calls to free() --- main.c | 12 ++++++------ vm.c | 6 ++++++ vm.h | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index b6a2751..bb7cf3a 100644 --- a/main.c +++ b/main.c @@ -15,7 +15,7 @@ void test_int_alloc(void) { assert(vm->refCount == 4 && "test_int_alloc: GARBAGE_INT allocation failure\n"); printf("test_int_alloc: PASS\n"); - free(vm); + deinitVM(vm); } void test_pair_alloc(void) { @@ -29,7 +29,7 @@ void test_pair_alloc(void) { assert(vm->refCount == 6 && "test_pair_alloc: FAILED: GARBAGE_PAIR allocation failure\n"); printf("test_pair_alloc: PASS\n"); - free(vm); + deinitVM(vm); } void test_obj_count(void) { @@ -42,7 +42,7 @@ void test_obj_count(void) { assert(vm->refCount == 4 && "test_obj_count: FAILED: GC occurred when it shouldn't have\n"); printf("test_obj_count: PASS\n"); - free(vm); + deinitVM(vm); } void test_nested_pair(void) { @@ -58,7 +58,7 @@ void test_nested_pair(void) { assert(vm->refCount == 7 && "test_nested_pair: FAILED: GARBAGE_PAIR allocation failure\n"); printf("test_pair_alloc: PASS\n"); - free(vm); + deinitVM(vm); } void test_unreachable(void) { @@ -71,7 +71,7 @@ void test_unreachable(void) { assert(vm->refCount == 0 && "test_unreachable: FAILED: 2 GARBAGE_INT should have been freed\n"); printf("test_unreachable: PASS\n"); - free(vm); + deinitVM(vm); } void test_auto_gc(void) { @@ -92,7 +92,7 @@ void test_auto_gc(void) { assert(vm->refCount == 550 && "test_auto_gc: FAILED: 5 references should have been freed\n"); printf("test_auto_gc: PASS\n"); - free(vm); + deinitVM(vm); } int main(void) { diff --git a/vm.c b/vm.c index d90dad7..79576e1 100644 --- a/vm.c +++ b/vm.c @@ -13,6 +13,12 @@ struct virtualMachine *initVM() { return vm; } +void deinitVM(struct virtualMachine *vm) { + vm->stackSize = 0; + collect(vm); + free(vm); +} + void push(struct virtualMachine *vm, struct garbageObject *value) { if (vm->stackSize >= STACK_MAX) { fprintf(stderr, "ERROR: push(): refusing to overflow the stack!\n"); diff --git a/vm.h b/vm.h index 445ef6a..5bfde17 100644 --- a/vm.h +++ b/vm.h @@ -42,6 +42,8 @@ struct garbageObject *initGarbage(struct virtualMachine *vm, // Create a new VM struct virtualMachine *initVM(void); +void deinitVM(struct virtualMachine *vm); + // Generic function to push data onto the VM stack void push(struct virtualMachine *vm, struct garbageObject *value);