aboutsummaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
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();
+}