added optional debug printing to gc allocator
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 Sep 2023 04:52:12 +0000 (23:52 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 Sep 2023 04:52:12 +0000 (23:52 -0500)
core/alloc/gc.onyx

index 01ad229f5e93b41016c2b744ad504686a39fc7e9..3fbec3fedcfb64a17b3bb5cca4abe5828fc72bb1 100644 (file)
@@ -16,6 +16,8 @@ package core.alloc.gc
 //       // Every allocation here will automatically be freed
 //   }
 
+
+use runtime
 use core {package, *}
 
 GCState :: struct {
@@ -36,10 +38,20 @@ make :: (backing := context.allocator) -> GCState {
 }
 
 clear :: (hs: &GCState) {
+    Debug_Printing :: #defined(runtime.vars.Enable_GC_Debug)
+
+    count := 0;
+    size := 0;
+
     while l := hs.first; l != null {
         n := l.next;
 
         if l.magic_number == GC_Link_Magic_Number {
+            #if Debug_Printing {
+                count += 1;
+                size += *cast(&u32) (cast([&] u8) l - 8);
+            }
+
             l.magic_number = 0;
             raw_free(hs.backing_allocator, l);
         }
@@ -47,6 +59,11 @@ clear :: (hs: &GCState) {
         l = n;
     }
 
+    #if Debug_Printing {
+        logf(.Debug, "Garbage collected items: {}", count);
+        logf(.Debug, "Garbage collected bytes: {}", size);
+    }
+
     hs.first = null;
 }