aboutsummaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-06-12 09:18:56 +0100
committerMichael Williamson <mike@zwobble.org>2024-06-12 09:22:20 +0100
commitb7eba5d35ad704a5396f433d75c743f5df8d9334 (patch)
treedb6a1041dff7aef0cde0cff0caf966faffed7e54 /runtime/src
parent72bb083256e85b44b998d15e4b2aa74db0213a6a (diff)
Garbage collect on allocation
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/shed/gc/mark-and-sweep/gc.c27
1 files changed, 14 insertions, 13 deletions
diff --git a/runtime/src/shed/gc/mark-and-sweep/gc.c b/runtime/src/shed/gc/mark-and-sweep/gc.c
index c3ff8298..02759596 100644
--- a/runtime/src/shed/gc/mark-and-sweep/gc.c
+++ b/runtime/src/shed/gc/mark-and-sweep/gc.c
@@ -46,9 +46,20 @@ void shed__runtime__gc_init(void) {
// == Allocation ==
+const int collect_interval = SHED_GC_COLLECT_INTERVAL;
+
+int calls_until_next_collect = collect_interval;
+
struct ShedGcObjectHeader* first = NULL;
void* shed__runtime__gc_alloc(size_t size, uint32_t runtime_type_tag) {
+ if (calls_until_next_collect == 0) {
+ calls_until_next_collect = collect_interval;
+ shed__runtime__gc_collect();
+ } else {
+ calls_until_next_collect--;
+ }
+
struct ShedGcObjectHeader* object = malloc(size);
object->next = first;
object->mark = 0;
@@ -86,20 +97,10 @@ void sweep() {
}
}
-const int collect_interval = SHED_GC_COLLECT_INTERVAL;
-
-int calls_until_next_collect = collect_interval;
-
void shed__runtime__gc_collect(void) {
- if (calls_until_next_collect == 0) {
- calls_until_next_collect = collect_interval;
-
- alternate_mark_value();
+ alternate_mark_value();
- mark_reachable();
+ mark_reachable();
- sweep();
- } else {
- calls_until_next_collect--;
- }
+ sweep();
}