#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; enum { GC_ROOT_STACK_SIZE = 16384, }; struct ShedGcObjectHeader* shed__runtime__gc_root_stack[GC_ROOT_STACK_SIZE]; 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); } // == Clearing == void shed__runtime__gc_clear_roots(void) { // Clearing all roots allows valgrind to detect leaks. for (size_t gc_root_index = 0; gc_root_index < GC_ROOT_STACK_SIZE; gc_root_index++) { shed__runtime__gc_root_stack[gc_root_index] = NULL; } }