Skip to content

Commit

Permalink
add heaptrack support
Browse files Browse the repository at this point in the history
  • Loading branch information
Menduist committed May 24, 2022
1 parent c6a9f27 commit 577ad68
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/system/alloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,15 @@ when false:
else:
result = nil

when defined(heaptracker):
const heaptrackLib =
when defined(heaptracker_inject):
"libheaptrack_inject.so"
else:
"libheaptrack_preload.so"
proc heaptrack_malloc(a: pointer, size: int) {.cdecl, importc, dynlib:heaptrackLib.}
proc heaptrack_free(a: pointer) {.cdecl, importc, dynlib:heaptrackLib.}

proc rawAlloc(a: var MemRegion, requestedSize: int): pointer =
when defined(nimTypeNames):
inc(a.allocCounter)
Expand Down Expand Up @@ -812,13 +821,18 @@ proc rawAlloc(a: var MemRegion, requestedSize: int): pointer =
sysAssert(allocInv(a), "rawAlloc: end")
when logAlloc: cprintf("var pointer_%p = alloc(%ld)\n", result, requestedSize)

when defined(heaptracker):
heaptrack_malloc(result, requestedSize)

proc rawAlloc0(a: var MemRegion, requestedSize: int): pointer =
result = rawAlloc(a, requestedSize)
zeroMem(result, requestedSize)

proc rawDealloc(a: var MemRegion, p: pointer) =
when defined(nimTypeNames):
inc(a.deallocCounter)
when defined(heaptracker):
heaptrack_free(p)
#sysAssert(isAllocatedPtr(a, p), "rawDealloc: no allocated pointer")
sysAssert(allocInv(a), "rawDealloc: begin")
var c = pageAddr(p)
Expand Down

0 comments on commit 577ad68

Please sign in to comment.