diff options
Diffstat (limited to 'clox/src/memory.c')
-rw-r--r-- | clox/src/memory.c | 24 |
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"); |