From 596b1c1fde6b94326c7cc1ee35c81586fa04ab72 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 9 Mar 2025 15:49:44 +0000 Subject: Clear GC roots so valgrind can find leaks --- runtime/src/shed/gc.c | 15 ++++++++++++++- runtime/src/shed/gc.h | 5 +++++ runtime/src/shed/gc/bump/gc.c | 6 ++++++ runtime/src/shed/gc/mark-and-sweep/gc.c | 7 +++++++ 4 files changed, 32 insertions(+), 1 deletion(-) (limited to 'runtime/src') diff --git a/runtime/src/shed/gc.c b/runtime/src/shed/gc.c index b11b2bd7..dd31318d 100644 --- a/runtime/src/shed/gc.c +++ b/runtime/src/shed/gc.c @@ -18,7 +18,11 @@ bool shed__runtime__gc_is_gc_object(ShedObject object) { ShedGcRootIndex shed__runtime__gc_root_stack_index = 0; -struct ShedGcObjectHeader* shed__runtime__gc_root_stack[16384]; +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; @@ -40,3 +44,12 @@ void shed__runtime__gc_push_root(ShedObject gc_root) { 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; + } +} diff --git a/runtime/src/shed/gc.h b/runtime/src/shed/gc.h index 70932148..8377e4cb 100644 --- a/runtime/src/shed/gc.h +++ b/runtime/src/shed/gc.h @@ -48,4 +48,9 @@ void* shed__runtime__gc_alloc(size_t size, uint32_t runtime_type_tag); void shed__runtime__gc_collect(void); +// == Clearing == + +void shed__runtime__gc_clear(void); +void shed__runtime__gc_clear_roots(void); + #endif diff --git a/runtime/src/shed/gc/bump/gc.c b/runtime/src/shed/gc/bump/gc.c index fe859dff..7cd9e199 100644 --- a/runtime/src/shed/gc/bump/gc.c +++ b/runtime/src/shed/gc/bump/gc.c @@ -44,3 +44,9 @@ void* shed__runtime__gc_alloc(size_t size, uint32_t runtime_type_tag) { void shed__runtime__gc_collect(void) { } + +// == Clearing == + +void shed__runtime__gc_clear(void) { + shed__runtime__gc_clear_roots(); +} diff --git a/runtime/src/shed/gc/mark-and-sweep/gc.c b/runtime/src/shed/gc/mark-and-sweep/gc.c index 02759596..b9b6d7d8 100644 --- a/runtime/src/shed/gc/mark-and-sweep/gc.c +++ b/runtime/src/shed/gc/mark-and-sweep/gc.c @@ -104,3 +104,10 @@ void shed__runtime__gc_collect(void) { sweep(); } + +// == Clearing == + +void shed__runtime__gc_clear(void) { + first = NULL; + shed__runtime__gc_clear_roots(); +} -- cgit v1.2.3