This commit is contained in:
araq
2026-04-02 13:48:42 +02:00
parent ba65f77ea1
commit ff5108b198
2 changed files with 11 additions and 4 deletions

View File

@@ -1270,13 +1270,16 @@ template instantiateForRegion(allocator: untyped) {.dirty.} =
when defined(gcOrc) or defined(gcYrc):
proc nimAlignedAlloc0(size: Natural, alignment: int): pointer =
incStat(allocCount)
result = rawAlloc(allocator, size, alignment)
zeroMem(result, size)
proc nimAlignedAlloc(size: Natural, alignment: int): pointer =
incStat(allocCount)
result = rawAlloc(allocator, size, alignment)
proc nimAlignedDealloc(p: pointer) =
incStat(deallocCount)
rawDealloc(allocator, p)
proc deallocImpl(p: pointer) =

View File

@@ -95,7 +95,11 @@ when not defined(nimHasQuirky):
# Forward declarations for native allocator alignment (implemented in alloc.nim).
# rawAlloc's contract: result + sizeof(FreeCell) is alignment-aligned.
# For ORC/YRC, sizeof(FreeCell) == sizeof(RefHeader).
when (defined(gcOrc) or defined(gcYrc)) and not defined(useMalloc) and not defined(nimscript) and not defined(nimdoc):
const useNativeAlignedAlloc = (defined(gcOrc) or defined(gcYrc)) and
not defined(useMalloc) and not defined(nimscript) and
not defined(nimdoc) and not defined(useNimRtl)
when useNativeAlignedAlloc:
proc nimAlignedAlloc0(size: Natural, alignment: int): pointer {.gcsafe, raises: [].}
proc nimAlignedAlloc(size: Natural, alignment: int): pointer {.gcsafe, raises: [].}
proc nimAlignedDealloc(p: pointer) {.gcsafe, raises: [].}
@@ -103,7 +107,7 @@ when (defined(gcOrc) or defined(gcYrc)) and not defined(useMalloc) and not defin
proc nimNewObj(size, alignment: int): pointer {.compilerRtl.} =
when defined(nimscript) or defined(nimdoc):
discard
elif (defined(gcOrc) or defined(gcYrc)) and not defined(useMalloc):
elif useNativeAlignedAlloc:
let s = size +% sizeof(RefHeader)
result = nimAlignedAlloc0(s, alignment) +! sizeof(RefHeader)
else:
@@ -124,7 +128,7 @@ proc nimNewObjUninit(size, alignment: int): pointer {.compilerRtl.} =
# Same as 'newNewObj' but do not initialize the memory to zero.
when defined(nimscript) or defined(nimdoc):
discard
elif (defined(gcOrc) or defined(gcYrc)) and not defined(useMalloc):
elif useNativeAlignedAlloc:
let s = size + sizeof(RefHeader)
result = cast[ptr RefHeader](nimAlignedAlloc(s, alignment) +! sizeof(RefHeader))
else:
@@ -202,7 +206,7 @@ proc nimRawDispose(p: pointer, alignment: int) {.compilerRtl.} =
if freedCells.data == nil: init(freedCells)
freedCells.incl head(p)
else:
when (defined(gcOrc) or defined(gcYrc)) and not defined(useMalloc) and not defined(nimdoc):
when useNativeAlignedAlloc:
nimAlignedDealloc(p -! sizeof(RefHeader))
else:
let hdrSize = align(sizeof(RefHeader), alignment)