From 84961ff43dba2490e08a442641babdefdd18812b Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 9 Feb 2026 18:36:51 +0800 Subject: [PATCH] hard code `sizeof(Cell)` --- lib/system/alloc.nim | 18 +++++++++--------- lib/system/cellsets.nim | 14 -------------- lib/system/gc.nim | 4 ++-- lib/system/mmdisp.nim | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim index 16f664f561..4130ad8cca 100644 --- a/lib/system/alloc.nim +++ b/lib/system/alloc.nim @@ -849,14 +849,14 @@ when defined(heaptrack): proc heaptrack_malloc(a: pointer, size: int) {.cdecl, importc, dynlib: heaptrackLib.} proc heaptrack_free(a: pointer) {.cdecl, importc, dynlib: heaptrackLib.} -proc bigChunkAlignOffset(alignment, offset: int): int {.inline.} = +proc bigChunkAlignOffset(alignment: int): int {.inline.} = ## Compute the alignment offset for big chunk data. - ## Since chunks are page-aligned and sizeof(BigChunk) is a compile-time constant, - ## the offset is deterministic for a given alignment and data offset. - if alignment <= MemAlign: 0 - else: align(sizeof(BigChunk) + offset, alignment) - sizeof(BigChunk) - offset + if alignment <= MemAlign: + result = 0 + else: + result = align(sizeof(BigChunk) + sizeof(Cell), alignment) - sizeof(BigChunk) - sizeof(Cell) -proc rawAlloc(a: var MemRegion, requestedSize: int, alignment: int = MemAlign, offset: int = 0): pointer = +proc rawAlloc(a: var MemRegion, requestedSize: int, alignment: int = MemAlign): pointer = when defined(nimTypeNames): inc(a.allocCounter) sysAssert(allocInv(a), "rawAlloc: begin") @@ -966,7 +966,7 @@ proc rawAlloc(a: var MemRegion, requestedSize: int, alignment: int = MemAlign, o # For big chunks with custom alignment, allocate extra space. # Since chunks are page-aligned, the needed padding is a compile-time # deterministic value rather than a worst-case estimate. - let alignPad = bigChunkAlignOffset(alignment, offset) + let alignPad = bigChunkAlignOffset(alignment) size = requestedSize + bigChunkOverhead() + alignPad # allocate a large block var c = if size >= HugeChunkSize: getHugeChunk(a, size) @@ -991,8 +991,8 @@ proc rawAlloc(a: var MemRegion, requestedSize: int, alignment: int = MemAlign, o when defined(heaptrack): heaptrack_malloc(result, requestedSize) -proc rawAlloc0(a: var MemRegion, requestedSize: int, alignment: int = MemAlign, offset: int = 0): pointer = - result = rawAlloc(a, requestedSize, alignment, offset) +proc rawAlloc0(a: var MemRegion, requestedSize: int): pointer = + result = rawAlloc(a, requestedSize) zeroMem(result, requestedSize) proc rawDealloc(a: var MemRegion, p: pointer) = diff --git a/lib/system/cellsets.nim b/lib/system/cellsets.nim index 1fed45b7b5..f68bfc9703 100644 --- a/lib/system/cellsets.nim +++ b/lib/system/cellsets.nim @@ -49,20 +49,6 @@ when defined(gcOrc) or defined(gcArc) or defined(gcAtomicArc): when not declaredInScope(PageShift): include bitmasks -else: - type - RefCount = int - - Cell {.pure.} = object - refcount: RefCount # the refcount and some flags - typ: PNimType - when trackAllocationSource: - filename: cstring - line: int - when useCellIds: - id: int - - PCell = ptr Cell type PPageDesc = ptr PageDesc diff --git a/lib/system/gc.nim b/lib/system/gc.nim index 7c8240a554..4b02b2f257 100644 --- a/lib/system/gc.nim +++ b/lib/system/gc.nim @@ -460,7 +460,7 @@ proc rawNewObj(typ: PNimType, size: int, gch: var GcHeap): pointer = collectCT(gch) # Use alignment from typ.base if available, otherwise use MemAlign let alignment = if typ.kind == tyRef and typ.base != nil: max(typ.base.align, MemAlign) else: MemAlign - var res = cast[PCell](rawAlloc(gch.region, size + sizeof(Cell), alignment, sizeof(Cell))) + var res = cast[PCell](rawAlloc(gch.region, size + sizeof(Cell), alignment)) #gcAssert typ.kind in {tyString, tySequence} or size >= typ.base.size, "size too small" # Check that the user data (after the Cell header) is properly aligned gcAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2") @@ -513,7 +513,7 @@ proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl, noinline, raise # Use alignment from typ.base if available, otherwise use MemAlign let alignment = if typ.base != nil: max(typ.base.align, MemAlign) else: MemAlign - var res = cast[PCell](rawAlloc(gch.region, size + sizeof(Cell), alignment, sizeof(Cell))) + var res = cast[PCell](rawAlloc(gch.region, size + sizeof(Cell), alignment)) sysAssert(allocInv(gch.region), "newObjRC1 after rawAlloc") # Check that the user data (after the Cell header) is properly aligned sysAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2") diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim index 7fd61e0dc3..a3772ba371 100644 --- a/lib/system/mmdisp.nim +++ b/lib/system/mmdisp.nim @@ -38,6 +38,21 @@ type PByte = ptr ByteArray PString = ptr string +when not (defined(gcOrc) or defined(gcArc) or defined(gcAtomicArc)): + type + RefCount = int + + Cell {.pure.} = object + refcount: RefCount # the refcount and some flags + typ: PNimType + when trackAllocationSource: + filename: cstring + line: int + when useCellIds: + id: int + + PCell = ptr Cell + when declared(IntsPerTrunk): discard else: