Files
Nim/lib/system/mm/malloc.nim
Andreas Rumpf a690a9ac90 YRC: threadsafe cycle collection for Nim (#25495)
First performance numbers:

time tests/arc/torcbench   -- YRC
true peak memory: true

real    0m0,163s
user    0m0,161s
sys     0m0,002s


time tests/arc/torcbench   -- ORC
true peak memory: true

real    0m0,107s
user    0m0,104s
sys     0m0,003s


So it's 1.6x slower. But it's threadsafe and provably correct. (Lean and
model checking via TLA+ used.)

Of course there is always the chance that the implementation is wrong
and doesn't match the model.
2026-02-10 00:04:11 +01:00

98 lines
2.6 KiB
Nim

{.push stackTrace: off.}
proc allocImpl(size: Natural): pointer =
result = c_malloc(size.csize_t)
when defined(zephyr):
if result == nil:
raiseOutOfMem()
proc alloc0Impl(size: Natural): pointer =
result = c_calloc(size.csize_t, 1)
when defined(zephyr):
if result == nil:
raiseOutOfMem()
proc reallocImpl(p: pointer, newSize: Natural): pointer =
result = c_realloc(p, newSize.csize_t)
when defined(zephyr):
if result == nil:
raiseOutOfMem()
proc realloc0Impl(p: pointer, oldsize, newSize: Natural): pointer =
result = realloc(p, newSize.csize_t)
if newSize > oldSize:
zeroMem(cast[pointer](cast[uint](result) + uint(oldSize)), newSize - oldSize)
proc deallocImpl(p: pointer) =
c_free(p)
# The shared allocators map on the regular ones
proc allocSharedImpl(size: Natural): pointer =
allocImpl(size)
proc allocShared0Impl(size: Natural): pointer =
alloc0Impl(size)
proc reallocSharedImpl(p: pointer, newSize: Natural): pointer =
reallocImpl(p, newSize)
proc reallocShared0Impl(p: pointer, oldsize, newSize: Natural): pointer =
realloc0Impl(p, oldSize, newSize)
proc deallocSharedImpl(p: pointer) = deallocImpl(p)
# Empty stubs for the GC
proc GC_disable() = discard
proc GC_enable() = discard
when not defined(gcOrc) and not defined(gcYrc):
proc GC_fullCollect() = discard
proc GC_enableMarkAndSweep() = discard
proc GC_disableMarkAndSweep() = discard
proc GC_setStrategy(strategy: GC_Strategy) = discard
proc getOccupiedMem(): int = discard
proc getFreeMem(): int = discard
proc getTotalMem(): int = discard
proc nimGC_setStackBottom(theStackBottom: pointer) = discard
proc initGC() = discard
proc newObjNoInit(typ: PNimType, size: int): pointer =
result = alloc(size)
proc growObj(old: pointer, newsize: int): pointer =
result = realloc(old, newsize)
proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
when not defined(gcDestructors):
proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
dest[] = src
proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
dest[] = src
proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
deprecated: "old compiler compat".} = asgnRef(dest, src)
type
MemRegion = object
proc alloc(r: var MemRegion, size: int): pointer =
result = alloc(size)
proc alloc0(r: var MemRegion, size: int): pointer =
result = alloc0Impl(size)
proc dealloc(r: var MemRegion, p: pointer) = dealloc(p)
proc deallocOsPages(r: var MemRegion) = discard
proc deallocOsPages() = discard
{.pop.}