Refactor alignment checks in gc.nim

Simplified alignment checks by removing conditional statements.
This commit is contained in:
ringabout
2026-02-17 09:51:20 +08:00
committed by GitHub
parent ac131313bc
commit 31f188dc62

View File

@@ -463,10 +463,7 @@ proc rawNewObj(typ: PNimType, size: int, gch: var GcHeap): pointer =
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
if alignment > MemAlign:
gcAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2.1")
else:
gcAssert((cast[int](res) and (MemAlign-1)) == 0, "newObj: 2.2")
gcAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2.1")
# now it is buffered in the ZCT
res.typ = typ
setFrameInfo(res)
@@ -519,10 +516,7 @@ proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl, noinline, raise
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
if alignment > MemAlign:
sysAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2.1")
else:
sysAssert((cast[int](res) and (MemAlign-1)) == 0, "newObj: 2.2")
sysAssert((cast[int](cellToUsr(res)) and (alignment-1)) == 0, "newObj: 2.1")
# now it is buffered in the ZCT
res.typ = typ
setFrameInfo(res)