aboutsummaryrefslogtreecommitdiffstats
path: root/clox
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
parent14b3866ac09c80b8f34d79227d743eabb2da0f2a (diff)
downloadcrafting-interpreters-89ffc2e60a7e7473504721874596e7bebcce896a.tar.gz
crafting-interpreters-89ffc2e60a7e7473504721874596e7bebcce896a.zip
Chapter 26.5
Diffstat (limited to 'clox')
-rw-r--r--clox/src/memory.c24
-rw-r--r--clox/src/table.c9
-rw-r--r--clox/src/table.h1
3 files changed, 34 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");
diff --git a/clox/src/table.c b/clox/src/table.c
index ae34ab0..134f455 100644
--- a/clox/src/table.c
+++ b/clox/src/table.c
@@ -141,6 +141,15 @@ ObjString *tableFindString(Table *table, const char *chars, int length,
}
}
+void tableRemoveWhite(Table *table) {
+ for (int i = 0; i < table->capacity; i++) {
+ Entry *entry = &table->entries[i];
+ if (entry->key != NULL && !entry->key->obj.isMarked) {
+ tableDelete(table, entry->key);
+ }
+ }
+}
+
void markTable(Table *table) {
for (int i = 0; i < table->capacity; i++) {
Entry *entry = &table->entries[i];
diff --git a/clox/src/table.h b/clox/src/table.h
index b2bf7a3..8c5aa07 100644
--- a/clox/src/table.h
+++ b/clox/src/table.h
@@ -23,6 +23,7 @@ bool tableDelete(Table *table, ObjString *key);
void tableAddAll(Table *from, Table *to);
ObjString *tableFindString(Table *talbe, const char *chars, int length,
uint32_t hash);
+void tableRemoveWhite(Table *table);
void markTable(Table *table);
#endif