From 267a29d19584218d3073bc0e70f0c3c8fe73c849 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 26 Jan 2026 16:24:07 +0800 Subject: [PATCH] progress --- lib/system/gc.nim | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/system/gc.nim b/lib/system/gc.nim index 11c00774f1..1a0e16802d 100644 --- a/lib/system/gc.nim +++ b/lib/system/gc.nim @@ -453,7 +453,8 @@ template setFrameInfo(c: PCell) = c.line = 0 proc allocCellWithAlignment(typ: PNimType, size: int, gch: var GcHeap): PCell {.inline.} = - # Allocate a cell with proper alignment based on type requirements + # Allocate a cell with proper alignment based on type requirements. + # Follows the pattern from alignedAlloc in memalloc.nim let requiredAlign = if typ.base != nil and typ.base.align > 0: typ.base.align @@ -465,10 +466,12 @@ proc allocCellWithAlignment(typ: PNimType, size: int, gch: var GcHeap): PCell {. result = cast[PCell](rawAlloc(gch.region, size +% sizeof(Cell))) else: let extra = requiredAlign -% 1 + # allocate (size + align - 1) to ensure alignment let base = rawAlloc(gch.region, size +% sizeof(Cell) +% extra) - let baseInt = cast[BiggestInt](base) - let userInt = align(baseInt +% sizeof(Cell), requiredAlign) - result = cast[PCell](cast[pointer](userInt -% sizeof(Cell))) + # calculate offset to align the user data (after the Cell header) + # the user data starts at (base + sizeof(Cell)), so we align that address + let offset = requiredAlign -% cast[int]((cast[uint](base) + cast[uint](sizeof(Cell))) and cast[uint](extra)) + result = cast[PCell](base +! offset) proc rawNewObj(typ: PNimType, size: int, gch: var GcHeap): pointer = # generates a new object and sets its reference counter to 0