diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-06-12 09:18:56 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-06-12 09:22:20 +0100 |
| commit | b7eba5d35ad704a5396f433d75c743f5df8d9334 (patch) | |
| tree | db6a1041dff7aef0cde0cff0caf966faffed7e54 /runtime/src | |
| parent | 72bb083256e85b44b998d15e4b2aa74db0213a6a (diff) | |
Garbage collect on allocation
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/shed/gc/mark-and-sweep/gc.c | 27 |
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(); } |
