42 lines
972 B
Nim
42 lines
972 B
Nim
import config
|
|
import strutils
|
|
import strformat
|
|
|
|
var collectGarbage*: proc: void {.closure.}
|
|
|
|
var enableGC* = false
|
|
# set by the compiler/VM as needed
|
|
|
|
proc ndAlloc*(size: int): pointer =
|
|
when debugGC:
|
|
echo &"Allocating {$size} bytes."
|
|
when stressGC:
|
|
if enableGC:
|
|
collectGarbage()
|
|
alloc(size)
|
|
|
|
proc ndAlloc0*(size: int): pointer =
|
|
when debugGC:
|
|
echo &"Allocating and zeroing {$size} bytes."
|
|
when stressGC:
|
|
if enableGC:
|
|
collectGarbage()
|
|
alloc0(size)
|
|
|
|
proc ndRealloc*[T](source: ptr T, oldsize: int, newsize: int): ptr T =
|
|
when debugGC:
|
|
let niceAddr = cast[uint](source).toHex()
|
|
echo &"Reallocating {niceAddr} from size {$oldsize} to {$newsize}."
|
|
if newsize > oldsize:
|
|
when stressGC:
|
|
if enableGC:
|
|
collectGarbage()
|
|
discard
|
|
cast[ptr T](realloc(source, newsize))
|
|
|
|
proc ndDealloc*[T](mem: ptr T) =
|
|
when debugGC:
|
|
let niceAddr = cast[uint](mem).toHex()
|
|
echo &"Freeing {niceAddr}"
|
|
dealloc(mem)
|