mirror of
https://codeberg.org/andyscott/marCsweep.git
synced 2024-11-12 15:20:49 -05:00
VM: added reference tracing
This commit is contained in:
parent
286fbb1a49
commit
56311a928d
2 changed files with 11 additions and 0 deletions
9
vm.c
9
vm.c
|
@ -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
2
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];
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue