diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-02-11 21:29:15 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-02-11 21:29:15 +0000 |
| commit | a01ee103f9bc0c6defe9ce8cd85651f19f7127bd (patch) | |
| tree | 8634fd33dde74b4aab1519e1b61bc3343d99606f /runtime/src/gc.c | |
| parent | 3d4e3f4a54d128a2a280b5adc921f3d95fb963bb (diff) | |
Create separate lib for patio runtime
Diffstat (limited to 'runtime/src/gc.c')
| -rw-r--r-- | runtime/src/gc.c | 132 |
1 files changed, 0 insertions, 132 deletions
diff --git a/runtime/src/gc.c b/runtime/src/gc.c deleted file mode 100644 index 12f668cb..00000000 --- a/runtime/src/gc.c +++ /dev/null @@ -1,132 +0,0 @@ -#include <stdbool.h> - -#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 == - -// The mark value for each object is initialized to 0. Each mark phase -// alternates between using 0b010 and 0b100 as the mark value. We check only for -// the bit set to 1, meaning static values can have a mark value of 0b110 to be -// considered always marked. -PatioInt current_mark_value = 0b010; - -void alternate_mark_value() { - current_mark_value ^= 0b110; -} - -bool is_marked(struct ShedGcObjectHeader* object) { - return (object->mark & current_mark_value) == current_mark_value; -} - -void mark(struct ShedGcObjectHeader* object) { - if (object == NULL || is_marked(object)) { - return; - } - - object->mark = current_mark_value; - - struct RuntimeType* runtime_type = &shed__rtti[object->runtime_type_tag]; - - struct ShedGcObjectHeader** children = (struct ShedGcObjectHeader**) ( - ((void*) object) + runtime_type->children_offset - ); - - for (size_t child_index = 0; child_index < runtime_type->children_count; child_index++) { - mark(children[child_index]); - } -} - -// == 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; - -void* shed__runtime__gc_alloc(size_t size, uint32_t runtime_type_tag) { - struct ShedGcObjectHeader* object = malloc(size); - object->next = first; - object->mark = 0; - object->runtime_type_tag = runtime_type_tag; - - // TODO: is it possible to leave adding the root to the caller, and avoid - // having to add so many roots? (Since some values will immediately become a - // descendant of a root.) - - shed__runtime__gc_push_root_gc_object(object); - - first = object; - - return object; -} - -// == Collection == - -void mark_reachable() { - for (size_t i = 0; i < gc_root_stack_index; i++) { - mark(gc_root_stack[i]); - } -} - -void sweep() { - struct ShedGcObjectHeader** object = &first; - while (*object != NULL) { - if (is_marked(*object)) { - object = &(*object)->next; - } else { - struct ShedGcObjectHeader* next = (*object)->next; - free(*object); - *object = next; - } - } -} - -void shed__runtime__gc_collect() { - // We only want to collect periodically, but we currently always collect to - // hopefully surface errors more quickly. - - alternate_mark_value(); - - mark_reachable(); - - sweep(); -} |
