aboutsummaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-16 15:10:04 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-16 15:10:04 +0000
commit297b999d2a76e9f264295a3cf47111937aaa3482 (patch)
treeea38ac8d4cdbaf844ecce7f2af07aa12fbb11d80 /runtime/src
parent8f1f6e418ddc6bb03331a3aa7b1aa23bb824addb (diff)
Add GC collect function
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/alloc.c11
-rw-r--r--runtime/src/alloc.h11
-rw-r--r--runtime/src/gc.c26
3 files changed, 37 insertions, 11 deletions
diff --git a/runtime/src/alloc.c b/runtime/src/alloc.c
index f7972fbf..f68fb128 100644
--- a/runtime/src/alloc.c
+++ b/runtime/src/alloc.c
@@ -1,14 +1,5 @@
#include "./std/unistd.h"
-
-#ifdef __WASM__
-
-void* malloc(unsigned long);
-
-#else
-
-#include <stdlib.h>
-
-#endif
+#include "./alloc.h"
void* shed__runtime__alloc(size_t size) {
return malloc(size);
diff --git a/runtime/src/alloc.h b/runtime/src/alloc.h
index b0a4e1cd..5e92573f 100644
--- a/runtime/src/alloc.h
+++ b/runtime/src/alloc.h
@@ -3,6 +3,17 @@
#include "./std/unistd.h"
+#ifdef __WASM__
+
+void* malloc(unsigned long);
+void free(void*);
+
+#else
+
+#include <stdlib.h>
+
+#endif
+
void* shed__runtime__alloc(size_t size);
#endif
diff --git a/runtime/src/gc.c b/runtime/src/gc.c
index 6958a772..a6ab16a2 100644
--- a/runtime/src/gc.c
+++ b/runtime/src/gc.c
@@ -50,7 +50,7 @@ void mark(struct ShedGcObjectHeader* object) {
struct ShedGcObjectHeader* first = NULL;
void* shed__runtime__gc_alloc(size_t size, size_t children_offset, size_t children_count) {
- struct ShedGcObjectHeader* object = shed__runtime__alloc(size);
+ struct ShedGcObjectHeader* object = malloc(size);
object->next = first;
object->mark = 0;
object->children_offset = children_offset;
@@ -115,3 +115,27 @@ void mark_reachable() {
mark(gc_root_stack[i]);
}
}
+
+void sweep() {
+ struct ShedGcObjectHeader** object = &first;
+ while (*object != NULL) {
+ if (is_marked(*object)) {
+ object = &(*object)->next;
+ } else {
+ struct ShedGcObjectHeader* next = (*object)->next;
+ free(*object);
+ *object = next;
+ }
+ }
+}
+
+void shed__runtime__gc_collect() {
+ // We only want to collect periodically, but we currently always collect to
+ // hopefully surface errors more quickly.
+
+ alternate_mark_value();
+
+ mark_reachable();
+
+ sweep();
+}