mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-04 02:44:44 +00:00
memory tracker is aware of threads (still not threadsafe though)
This commit is contained in:
@@ -28,23 +28,46 @@ type
|
||||
TrackLog* = object
|
||||
count*: int
|
||||
disabled: bool
|
||||
data*: array[4000, LogEntry]
|
||||
TrackLogger* = proc (log: TrackLog) {.nimcall, tags: [], locks: 0.}
|
||||
data*: array[400, LogEntry]
|
||||
TrackLogger* = proc (log: TrackLog) {.nimcall, tags: [], locks: 0, gcsafe.}
|
||||
|
||||
var
|
||||
gLog*: TrackLog
|
||||
gLogger*: TrackLogger = proc (log: TrackLog) = discard
|
||||
ilocs: array[4000, (int, int)]
|
||||
ilocn: int
|
||||
|
||||
proc trackLocation*(p: pointer; size: int) =
|
||||
let x = (cast[int](p), size)
|
||||
for i in 0..ilocn-1:
|
||||
# already known?
|
||||
if ilocs[i] == x: return
|
||||
ilocs[ilocn] = x
|
||||
inc ilocn
|
||||
|
||||
proc setTrackLogger*(logger: TrackLogger) =
|
||||
gLogger = logger
|
||||
|
||||
proc addEntry(entry: LogEntry) =
|
||||
if not gLog.disabled:
|
||||
if gLog.count > high(gLog.data):
|
||||
gLogger(gLog)
|
||||
gLog.count = 0
|
||||
gLog.data[gLog.count] = entry
|
||||
inc gLog.count
|
||||
var interesting = false
|
||||
for i in 0..ilocn-1:
|
||||
let p = ilocs[i]
|
||||
# X..Y and C..D overlap iff (X <= D and C <= Y)
|
||||
let x = p[0]
|
||||
let y = p[0]+p[1]-1
|
||||
let c = cast[int](entry.address)
|
||||
let d = c + entry.size-1
|
||||
if x <= d and c <= y:
|
||||
interesting = true
|
||||
break
|
||||
if interesting:
|
||||
cprintf("interesting %s:%ld\n", entry.file, entry.line)
|
||||
if gLog.count > high(gLog.data):
|
||||
gLogger(gLog)
|
||||
gLog.count = 0
|
||||
gLog.data[gLog.count] = entry
|
||||
inc gLog.count
|
||||
|
||||
proc memTrackerWrite(address: pointer; size: int; file: cstring; line: int) {.compilerProc.} =
|
||||
addEntry LogEntry(op: "write", address: address,
|
||||
|
||||
Reference in New Issue
Block a user