aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-09 15:49:44 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-09 15:49:44 +0000
commit596b1c1fde6b94326c7cc1ee35c81586fa04ab72 (patch)
treeec136bf863a8b1e1469854ce1a1b1d714b2bcfec
parent0272d952e38e3d449ac4d077c6bc6c4ed405fe10 (diff)
Clear GC roots so valgrind can find leaks
-rw-r--r--runtime/src/shed/gc.c15
-rw-r--r--runtime/src/shed/gc.h5
-rw-r--r--runtime/src/shed/gc/bump/gc.c6
-rw-r--r--runtime/src/shed/gc/mark-and-sweep/gc.c7
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java1
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java23
6 files changed, 56 insertions, 1 deletions
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();
+}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
index b3f235cf..51cc0bb8 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
@@ -98,6 +98,7 @@ public class ShidToPatio {
);
mainContext = ShidToPatioGc.collect(source, mainContext);
+ mainContext = ShidToPatioGc.clear(source, mainContext);
mainContext = mainContext.addInstruction(
new PatioReturn(exitCode, source)
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
index 07e8cbaf..39ff3ce5 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioGc.java
@@ -276,4 +276,27 @@ public class ShidToPatioGc {
return context;
}
+
+ // == Clear ==
+
+ private static final PatioFunctionDeclaration CLEAR = new PatioFunctionDeclaration(
+ "shed__runtime__gc_clear",
+ List.of(),
+ PatioTypes.VOID,
+ Sources.RUNTIME
+ );
+
+ public static ShidToPatioContext clear(
+ Source source,
+ ShidToPatioContext context
+ ) {
+ return context
+ .ensureCompilationUnitElement(CLEAR)
+ .addInstruction(new PatioCall(
+ Optional.empty(),
+ CLEAR.name(),
+ List.of(),
+ source
+ ));
+ }
}