From: Brendan Hansen Date: Mon, 4 Sep 2023 04:52:12 +0000 (-0500) Subject: added optional debug printing to gc allocator X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=f416486be0af78279640b615541fe63c8f8841ad;p=onyx.git added optional debug printing to gc allocator --- diff --git a/core/alloc/gc.onyx b/core/alloc/gc.onyx index 01ad229f..3fbec3fe 100644 --- a/core/alloc/gc.onyx +++ b/core/alloc/gc.onyx @@ -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; }