mirror of
https://codeberg.org/andyscott/marCsweep.git
synced 2024-11-09 13:50:51 -05:00
GC: completed defining gc functions
This commit is contained in:
parent
56311a928d
commit
9a54a80ccb
2 changed files with 48 additions and 1 deletions
45
gc.c
Normal file
45
gc.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "gc.h"
|
||||
#include "vm.h"
|
||||
|
||||
void mark(struct garbageObject *obj) {
|
||||
if (obj->mark) return;
|
||||
|
||||
++obj->mark;
|
||||
|
||||
if (obj->type == GARBAGE_PAIR) {
|
||||
mark(obj->head);
|
||||
mark(obj->tail);
|
||||
}
|
||||
}
|
||||
|
||||
void markAll(struct virtualMachine *vm) {
|
||||
for (int i = 0; i < vm->stackSize; ++i) {
|
||||
mark(vm->stack[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void sweep(struct virtualMachine *vm) {
|
||||
struct garbageObject **obj = &vm->head;
|
||||
|
||||
while (*obj) {
|
||||
if (!(*obj)->mark) {
|
||||
struct garbageObject *unreachable = *obj;
|
||||
*obj = unreachable->next;
|
||||
free(unreachable);
|
||||
vm->refCount--;
|
||||
} else {
|
||||
(*obj)->mark = 0;
|
||||
obj = &(*obj)->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int collect(struct virtualMachine *vm) {
|
||||
int count = vm->refCount;
|
||||
markAll(vm);
|
||||
sweep(vm);
|
||||
vm->refMax = vm->refCount * 2;
|
||||
return count - vm->refCount;
|
||||
}
|
4
gc.h
4
gc.h
|
@ -1,6 +1,8 @@
|
|||
#ifndef GC_H
|
||||
#define GC_H
|
||||
|
||||
#include "vm.h"
|
||||
|
||||
// Marks a single reference if it is still in use
|
||||
void mark(struct garbageObject *obj);
|
||||
|
||||
|
@ -11,6 +13,6 @@ void markAll(struct virtualMachine *vm);
|
|||
void sweep(struct virtualMachine *vm);
|
||||
|
||||
// Marks and Sweeps all references
|
||||
void collect(struct virtualMachine *vm);
|
||||
int collect(struct virtualMachine *vm);
|
||||
|
||||
#endif /* GC_H */
|
||||
|
|
Loading…
Reference in a new issue