aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/memory.c
diff options
context:
space:
mode:
authorGravatar Tom Willemse2022-08-12 17:55:32 -0700
committerGravatar Tom Willemse2022-08-12 17:55:32 -0700
commit89ffc2e60a7e7473504721874596e7bebcce896a (patch)
treebabdcdf20db8ce57f32bbf332cf169f34bb46bfb /clox/src/memory.c
parent14b3866ac09c80b8f34d79227d743eabb2da0f2a (diff)
downloadcrafting-interpreters-89ffc2e60a7e7473504721874596e7bebcce896a.tar.gz
crafting-interpreters-89ffc2e60a7e7473504721874596e7bebcce896a.zip
Chapter 26.5
Diffstat (limited to 'clox/src/memory.c')
-rw-r--r--clox/src/memory.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/clox/src/memory.c b/clox/src/memory.c
index 7d403d1..c639b65 100644
--- a/clox/src/memory.c
+++ b/clox/src/memory.c
@@ -150,6 +150,28 @@ static void traceReferences() {
}
}
+static void sweep() {
+ Obj *previous = NULL;
+ Obj *object = vm.objects;
+ while (object != NULL) {
+ if (object->isMarked) {
+ object->isMarked = false;
+ previous = object;
+ object = object->next;
+ } else {
+ Obj *unreached = object;
+ object = object->next;
+ if (previous != NULL) {
+ previous->next = object;
+ } else {
+ vm.objects = object;
+ }
+
+ freeObject(unreached);
+ }
+ }
+}
+
void collectGarbage() {
#ifdef DEBUG_LOG_GC
printf("-- gc begin\n");
@@ -157,6 +179,8 @@ void collectGarbage() {
markRoots();
traceReferences();
+ tableRemoveWhite(&vm.strings);
+ sweep();
#ifdef DEBUG_LOG_GC
printf("-- gd end\n");