VM: added reference tracing

This commit is contained in:
Andrew Scott 2024-08-20 10:55:53 -04:00
parent 286fbb1a49
commit 56311a928d
Signed by: a
GPG key ID: 7CD5A5977E4931C1
2 changed files with 11 additions and 0 deletions

9
vm.c
View file

@ -2,10 +2,14 @@
#include <stdlib.h>
#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;
}

2
vm.h
View file

@ -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];
};