logos-messaging-nim/docs/tutorial/nim.2.2.4_heaptracker_addon.patch
NagyZoltanPeter 228e637c9f chore: Adapt heaptrack support builds and description to latest nim 2.2.4 compiler (#3522)
* Adapt heaptrack support builds and description to latest nim 2.2.4 compiler
2025-08-29 08:10:22 +02:00

45 lines
1.6 KiB
Diff

diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim
index e2dd43075..7f8c8e04e 100644
--- a/lib/system/alloc.nim
+++ b/lib/system/alloc.nim
@@ -1,4 +1,4 @@
-#
+#!fmt: off
#
# Nim's Runtime Library
# (c) Copyright 2012 Andreas Rumpf
@@ -862,6 +862,15 @@ when defined(gcDestructors):
dec maxIters
if it == nil: break
+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)
@@ -984,6 +993,8 @@ proc rawAlloc(a: var MemRegion, requestedSize: int): pointer =
sysAssert(isAccessible(a, result), "rawAlloc 14")
sysAssert(allocInv(a), "rawAlloc: end")
when logAlloc: cprintf("var pointer_%p = alloc(%ld) # %p\n", result, requestedSize, addr a)
+ when defined(heaptracker):
+ heaptrack_malloc(result, requestedSize)
proc rawAlloc0(a: var MemRegion, requestedSize: int): pointer =
result = rawAlloc(a, requestedSize)
@@ -992,6 +1003,8 @@ proc rawAlloc0(a: var MemRegion, requestedSize: int): pointer =
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)