From 56311a928d78b1dae0c66b2dd4f5479678065cbb Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 20 Aug 2024 10:55:53 -0400 Subject: [PATCH] VM: added reference tracing --- vm.c | 9 +++++++++ vm.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/vm.c b/vm.c index b955cd2..68d37b7 100644 --- a/vm.c +++ b/vm.c @@ -2,10 +2,14 @@ #include #include "vm.h" +#include "gc.h" struct virtualMachine *initVM() { struct virtualMachine *vm = malloc(sizeof(struct virtualMachine)); vm->stackSize = 0; + vm->refCount = 0; + vm->refMax = STACK_MAX / 2; + vm->head = NULL; return vm; } @@ -29,9 +33,14 @@ struct garbageObject *pop(struct virtualMachine *vm) { struct garbageObject *initGarbage(struct virtualMachine *vm, enum garbageData type) { + if (vm->refCount == vm->refMax) collect(vm); + struct garbageObject *obj = malloc(sizeof(struct garbageObject)); obj->type = type; obj->mark = 0; + obj->next = vm->head; + vm->head = obj; + vm->refCount++; return obj; } diff --git a/vm.h b/vm.h index f37f724..90803e4 100644 --- a/vm.h +++ b/vm.h @@ -28,6 +28,8 @@ struct garbageObject { // Virtual machine hold the stack to trace references struct virtualMachine { int stackSize; + unsigned refCount; + unsigned refMax; struct garbageObject *head; struct garbageObject *stack[STACK_MAX]; };