From b7eba5d35ad704a5396f433d75c743f5df8d9334 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 12 Jun 2024 09:18:56 +0100 Subject: Garbage collect on allocation --- runtime/src/shed/gc/mark-and-sweep/gc.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'runtime/src') 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(); } -- cgit v1.2.3