From 812d7c7992a09c1ef0fec9c00108540462d06211 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 19 Mar 2024 16:34:27 +0000 Subject: Extract common GC code --- runtime/src/shed/gc.c | 42 ++++++++++++++++++++++++++++++++ runtime/src/shed/gc.h | 32 +++++++++++++++++++----- runtime/src/shed/gc/mark-and-sweep/gc.c | 43 ++------------------------------- 3 files changed, 70 insertions(+), 47 deletions(-) create mode 100644 runtime/src/shed/gc.c (limited to 'runtime/src') diff --git a/runtime/src/shed/gc.c b/runtime/src/shed/gc.c new file mode 100644 index 00000000..b11b2bd7 --- /dev/null +++ b/runtime/src/shed/gc.c @@ -0,0 +1,42 @@ +#include + +#include "./gc.h" +#include "./primitives.h" +#include "./std/unistd.h" + +// == GC objects == + +bool shed__runtime__gc_is_gc_object(ShedObject object) { + // Check that the object is not one of: + // * unit (0) + // * false (0) + // * true (1) + return !(object == 0 || object == 1); +} + +// == Roots == + +ShedGcRootIndex shed__runtime__gc_root_stack_index = 0; + +struct ShedGcObjectHeader* shed__runtime__gc_root_stack[16384]; + +ShedGcRootIndex shed__runtime__gc_get_root_stack_index() { + return shed__runtime__gc_root_stack_index; +} + +void shed__runtime__gc_set_root_stack_index(ShedGcRootIndex new_shed__runtime__gc_root_stack_index) { + shed__runtime__gc_root_stack_index = new_shed__runtime__gc_root_stack_index; +} + +void shed__runtime__gc_push_root_gc_object(struct ShedGcObjectHeader* gc_root) { + shed__runtime__gc_root_stack[shed__runtime__gc_root_stack_index] = gc_root; + shed__runtime__gc_root_stack_index++; +} + +void shed__runtime__gc_push_root(ShedObject gc_root) { + if (!shed__runtime__gc_is_gc_object(gc_root)) { + return; + } + + shed__runtime__gc_push_root_gc_object((struct ShedGcObjectHeader*) gc_root); +} diff --git a/runtime/src/shed/gc.h b/runtime/src/shed/gc.h index 9e39e7b7..ba1c98a4 100644 --- a/runtime/src/shed/gc.h +++ b/runtime/src/shed/gc.h @@ -1,15 +1,13 @@ #ifndef SHED_GC_H #define SHED_GC_H -#include "./std/unistd.h" +#include #include "../patio/patio.h" +#include "./std/unistd.h" +#include "./primitives.h" -struct ShedGcObjectHeader { - struct ShedGcObjectHeader* next; - uint32_t mark; - uint32_t runtime_type_tag; -}; +// == Runtime types == struct RuntimeType { uint32_t children_offset; @@ -18,6 +16,28 @@ struct RuntimeType { extern struct RuntimeType shed__rtti[]; +// == GC objects == + +struct ShedGcObjectHeader { + struct ShedGcObjectHeader* next; + uint32_t mark; + uint32_t runtime_type_tag; +}; + +bool shed__runtime__gc_is_gc_object(ShedObject object); + +// == Roots == + +typedef size_t ShedGcRootIndex; + +extern ShedGcRootIndex shed__runtime__gc_root_stack_index; + +extern struct ShedGcObjectHeader* shed__runtime__gc_root_stack[]; + +void shed__runtime__gc_push_root_gc_object(struct ShedGcObjectHeader* gc_root); + +// == Allocation == + void* shed__runtime__gc_alloc(size_t size, uint32_t runtime_type_tag); #endif diff --git a/runtime/src/shed/gc/mark-and-sweep/gc.c b/runtime/src/shed/gc/mark-and-sweep/gc.c index e6cbc0fa..40ffa98b 100644 --- a/runtime/src/shed/gc/mark-and-sweep/gc.c +++ b/runtime/src/shed/gc/mark-and-sweep/gc.c @@ -5,16 +5,6 @@ #include "../../primitives.h" #include "../../std/malloc.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 == // The mark value for each object is initialized to 0. Each mark phase @@ -49,35 +39,6 @@ void mark(struct ShedGcObjectHeader* object) { } } -// == Roots == - -typedef size_t ShedGcRootIndex; - -ShedGcRootIndex gc_root_stack_index = 0; - -struct ShedGcObjectHeader* gc_root_stack[16384]; - -ShedGcRootIndex shed__runtime__gc_get_root_stack_index() { - return gc_root_stack_index; -} - -void shed__runtime__gc_set_root_stack_index(ShedGcRootIndex new_gc_root_stack_index) { - gc_root_stack_index = new_gc_root_stack_index; -} - -static void shed__runtime__gc_push_root_gc_object(struct ShedGcObjectHeader* gc_root) { - gc_root_stack[gc_root_stack_index] = gc_root; - gc_root_stack_index++; -} - -void shed__runtime__gc_push_root(ShedObject gc_root) { - if (!is_gc_object(gc_root)) { - return; - } - - shed__runtime__gc_push_root_gc_object((struct ShedGcObjectHeader*) gc_root); -} - // == Allocation == struct ShedGcObjectHeader* first = NULL; @@ -102,8 +63,8 @@ void* shed__runtime__gc_alloc(size_t size, uint32_t runtime_type_tag) { // == Collection == void mark_reachable() { - for (size_t i = 0; i < gc_root_stack_index; i++) { - mark(gc_root_stack[i]); + for (size_t i = 0; i < shed__runtime__gc_root_stack_index; i++) { + mark(shed__runtime__gc_root_stack[i]); } } -- cgit v1.2.3