From 89ffc2e60a7e7473504721874596e7bebcce896a Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Fri, 12 Aug 2022 17:55:32 -0700 Subject: Chapter 26.5 --- clox/src/memory.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'clox/src/memory.c') 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"); -- cgit v1.2.3-54-g00ecf