From e3aad385a1de7798e359b9be453dd22b5c037eb1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 16 Dec 2023 15:04:31 +0000 Subject: Add GC root stack --- runtime/src/gc.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'runtime/src') diff --git a/runtime/src/gc.c b/runtime/src/gc.c index 08c39cf9..42fc95f2 100644 --- a/runtime/src/gc.c +++ b/runtime/src/gc.c @@ -3,6 +3,17 @@ #include "./patio/patio.h" #include "./alloc.h" #include "./gc.h" +#include "./primitives.h" + +// == GC objects == + +bool is_gc_object(ShedObject object) { + // Check that the object is not one of: + // * unit (0) + // * false (0) + // * true (1) + return !(object == 0 || object == 1); +} // == Marking == @@ -50,3 +61,27 @@ void* shed__runtime__gc_alloc(size_t size, size_t children_offset, size_t childr return object; } +// == Roots == + +typedef size_t ShedGcRootStackIndex; + +ShedGcRootStackIndex gc_root_stack_index = 0; + +struct ShedGcObjectHeader* gc_root_stack[16384]; + +ShedGcRootStackIndex shed__runtime__gc_get_root_stack_index() { + return gc_root_stack_index; +} + +void shed__runtime__gc_set_root_stack_index(ShedGcRootStackIndex new_gc_root_stack_index) { + gc_root_stack_index = new_gc_root_stack_index; +} + +void shed__runtime__gc_push_root(ShedObject gc_root) { + if (!is_gc_object(gc_root)) { + return; + } + + gc_root_stack[gc_root_stack_index] = (struct ShedGcObjectHeader*) gc_root; + gc_root_stack_index++; +} -- cgit v1.2.3