summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2018-07-21 00:43:13 +0100
committerAndreas Rumpf <rumpf_a@web.de>2018-07-21 01:43:13 +0200
commit5ea3b4d581c1997ba04ed6c7ba75623019afb95d (patch)
tree3bd010417bd5e1c3fce8edebcd4280462283d6d5
parent2b6f324929ec2977f34fd3631beb04e0b24c7964 (diff)
downloadNim-5ea3b4d581c1997ba04ed6c7ba75623019afb95d.tar.gz
Implements alloc/dealloc counters for better leak debugging. (#8384)
-rw-r--r--lib/system/alloc.nim13
-rw-r--r--lib/system/gc_common.nim3
2 files changed, 16 insertions, 0 deletions
diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim
index 95becde22..ca2d76225 100644
--- a/lib/system/alloc.nim
+++ b/lib/system/alloc.nim
@@ -116,6 +116,8 @@ type
     nextChunkSize: int
     bottomData: AvlNode
     heapLinks: HeapLinks
+    when defined(nimTypeNames):
+      allocCounter, deallocCounter: int
 
 const
   fsLookupTable: array[byte, int8] = [
@@ -737,6 +739,8 @@ when false:
         result = nil
 
 proc rawAlloc(a: var MemRegion, requestedSize: int): pointer =
+  when defined(nimTypeNames):
+    inc(a.allocCounter)
   sysAssert(allocInv(a), "rawAlloc: begin")
   sysAssert(roundup(65, 8) == 72, "rawAlloc: roundup broken")
   sysAssert(requestedSize >= sizeof(FreeCell), "rawAlloc: requested size too small")
@@ -810,6 +814,8 @@ proc rawAlloc0(a: var MemRegion, requestedSize: int): pointer =
   zeroMem(result, requestedSize)
 
 proc rawDealloc(a: var MemRegion, p: pointer) =
+  when defined(nimTypeNames):
+    inc(a.deallocCounter)
   #sysAssert(isAllocatedPtr(a, p), "rawDealloc: no allocated pointer")
   sysAssert(allocInv(a), "rawDealloc: begin")
   var c = pageAddr(p)
@@ -975,6 +981,10 @@ proc getOccupiedMem(a: MemRegion): int {.inline.} =
   result = a.occ
   # a.currMem - a.freeMem
 
+when defined(nimTypeNames):
+  proc getMemCounters(a: MemRegion): (int, int) {.inline.} =
+    (a.allocCounter, a.deallocCounter)
+
 # ---------------------- thread memory region -------------------------------
 
 template instantiateForRegion(allocator: untyped) =
@@ -1018,6 +1028,9 @@ template instantiateForRegion(allocator: untyped) =
   proc getOccupiedMem(): int = return allocator.occ #getTotalMem() - getFreeMem()
   proc getMaxMem*(): int = return getMaxMem(allocator)
 
+  when defined(nimTypeNames):
+    proc getMemCounters*(): (int, int) = getMemCounters(allocator)
+
   # -------------------- shared heap region ----------------------------------
   when hasThreadSupport:
     var sharedHeap: MemRegion
diff --git a/lib/system/gc_common.nim b/lib/system/gc_common.nim
index 711a610bf..dcea0c4cc 100644
--- a/lib/system/gc_common.nim
+++ b/lib/system/gc_common.nim
@@ -57,6 +57,9 @@ when defined(nimTypeNames):
     for i in 0 .. n-1:
       c_fprintf(stdout, "[Heap] %s: #%ld; bytes: %ld\n", a[i][0], a[i][1], a[i][2])
     c_fprintf(stdout, "[Heap] total number of bytes: %ld\n", totalAllocated)
+    when defined(nimTypeNames):
+      let (allocs, deallocs) = getMemCounters()
+      c_fprintf(stdout, "[Heap] allocs/deallocs: %ld/%ld\n", allocs, deallocs)
 
   when defined(nimGcRefLeak):
     proc oomhandler() =