VM: added deinitVM() and replaced calls to free()

This commit is contained in:
Andrew Scott 2024-08-20 14:45:13 -04:00
parent ba08dd966c
commit d0865a644c
Signed by: a
GPG key ID: 7CD5A5977E4931C1
3 changed files with 14 additions and 6 deletions

12
main.c
View file

@ -15,7 +15,7 @@ void test_int_alloc(void) {
assert(vm->refCount == 4 && assert(vm->refCount == 4 &&
"test_int_alloc: GARBAGE_INT allocation failure\n"); "test_int_alloc: GARBAGE_INT allocation failure\n");
printf("test_int_alloc: PASS\n"); printf("test_int_alloc: PASS\n");
free(vm); deinitVM(vm);
} }
void test_pair_alloc(void) { void test_pair_alloc(void) {
@ -29,7 +29,7 @@ void test_pair_alloc(void) {
assert(vm->refCount == 6 && assert(vm->refCount == 6 &&
"test_pair_alloc: FAILED: GARBAGE_PAIR allocation failure\n"); "test_pair_alloc: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n"); printf("test_pair_alloc: PASS\n");
free(vm); deinitVM(vm);
} }
void test_obj_count(void) { void test_obj_count(void) {
@ -42,7 +42,7 @@ void test_obj_count(void) {
assert(vm->refCount == 4 && assert(vm->refCount == 4 &&
"test_obj_count: FAILED: GC occurred when it shouldn't have\n"); "test_obj_count: FAILED: GC occurred when it shouldn't have\n");
printf("test_obj_count: PASS\n"); printf("test_obj_count: PASS\n");
free(vm); deinitVM(vm);
} }
void test_nested_pair(void) { void test_nested_pair(void) {
@ -58,7 +58,7 @@ void test_nested_pair(void) {
assert(vm->refCount == 7 && assert(vm->refCount == 7 &&
"test_nested_pair: FAILED: GARBAGE_PAIR allocation failure\n"); "test_nested_pair: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n"); printf("test_pair_alloc: PASS\n");
free(vm); deinitVM(vm);
} }
void test_unreachable(void) { void test_unreachable(void) {
@ -71,7 +71,7 @@ void test_unreachable(void) {
assert(vm->refCount == 0 && assert(vm->refCount == 0 &&
"test_unreachable: FAILED: 2 GARBAGE_INT should have been freed\n"); "test_unreachable: FAILED: 2 GARBAGE_INT should have been freed\n");
printf("test_unreachable: PASS\n"); printf("test_unreachable: PASS\n");
free(vm); deinitVM(vm);
} }
void test_auto_gc(void) { void test_auto_gc(void) {
@ -92,7 +92,7 @@ void test_auto_gc(void) {
assert(vm->refCount == 550 && assert(vm->refCount == 550 &&
"test_auto_gc: FAILED: 5 references should have been freed\n"); "test_auto_gc: FAILED: 5 references should have been freed\n");
printf("test_auto_gc: PASS\n"); printf("test_auto_gc: PASS\n");
free(vm); deinitVM(vm);
} }
int main(void) { int main(void) {

6
vm.c
View file

@ -13,6 +13,12 @@ struct virtualMachine *initVM() {
return vm; return vm;
} }
void deinitVM(struct virtualMachine *vm) {
vm->stackSize = 0;
collect(vm);
free(vm);
}
void push(struct virtualMachine *vm, struct garbageObject *value) { void push(struct virtualMachine *vm, struct garbageObject *value) {
if (vm->stackSize >= STACK_MAX) { if (vm->stackSize >= STACK_MAX) {
fprintf(stderr, "ERROR: push(): refusing to overflow the stack!\n"); fprintf(stderr, "ERROR: push(): refusing to overflow the stack!\n");

2
vm.h
View file

@ -42,6 +42,8 @@ struct garbageObject *initGarbage(struct virtualMachine *vm,
// Create a new VM // Create a new VM
struct virtualMachine *initVM(void); struct virtualMachine *initVM(void);
void deinitVM(struct virtualMachine *vm);
// Generic function to push data onto the VM stack // Generic function to push data onto the VM stack
void push(struct virtualMachine *vm, struct garbageObject *value); void push(struct virtualMachine *vm, struct garbageObject *value);