This commit is contained in:
ringabout
2026-02-02 17:30:27 +08:00
parent 9c7f2cfad4
commit d6e016f6ae
2 changed files with 42 additions and 41 deletions

View File

@@ -849,10 +849,11 @@ when defined(heaptrack):
proc applyAlignment(basePtr: pointer, alignment: int, offset: int, c: PBigChunk): pointer {.inline.} =
# Apply alignment if needed: align (basePtr + offset) to alignment boundary
if alignment > MemAlign:
let base = basePtr +! offset
let alignOffset = alignment -% cast[int](cast[uint](base) and uint(alignment - 1))
result = base +! alignOffset
c.alignOffset = cast[uint16](alignOffset +% offset)
# Use integer modulo-safe arithmetic for addresses and ptrarith for casts
let alignedUserData = align(cast[int](basePtr) +% offset, alignment)
let finalResult = alignedUserData -% offset
c.alignOffset = cast[uint16](finalResult -% cast[int](basePtr))
result = cast[pointer](finalResult)
else:
c.alignOffset = 0
result = basePtr

View File

@@ -123,49 +123,49 @@ for q in 0..500:
z[i].a = q
doAssert(cast[int](z[i]) mod alignof(MyType64) == 0)
type
MyType128 = object
a{.align(128).}: int
# type
# MyType128 = object
# a{.align(128).}: int
var w: array[10, ref MyType128]
for q in 0..500:
for i in 0..<w.len:
new w[i]
w[i].a = q
doAssert(cast[int](w[i]) mod alignof(MyType128) == 0)
# var w: array[10, ref MyType128]
# for q in 0..500:
# for i in 0..<w.len:
# new w[i]
# w[i].a = q
# doAssert(cast[int](w[i]) mod alignof(MyType128) == 0)
# Nested aligned-object tests
type
Inner128 = object
v {.align(128).}: byte
# # Nested aligned-object tests
# type
# Inner128 = object
# v {.align(128).}: byte
OuterWithInner = object
prefix: int
inner: Inner128
# OuterWithInner = object
# prefix: int
# inner: Inner128
var outerArr: array[8, ref OuterWithInner]
for q in 0..200:
for i in 0..<outerArr.len:
new outerArr[i]
# write to inner to ensure it's allocated
outerArr[i].inner.v = cast[byte](q and 0xFF)
doAssert(cast[uint](addr outerArr[i].inner) mod uint(alignof(Inner128)) == 0)
# var outerArr: array[8, ref OuterWithInner]
# for q in 0..200:
# for i in 0..<outerArr.len:
# new outerArr[i]
# # write to inner to ensure it's allocated
# outerArr[i].inner.v = cast[byte](q and 0xFF)
# doAssert(cast[uint](addr outerArr[i].inner) mod uint(alignof(Inner128)) == 0)
# Nested two-level alignment
type
DeepInner = object
b {.align(128).}: int
# # Nested two-level alignment
# type
# DeepInner = object
# b {.align(128).}: int
Mid = object
di: DeepInner
# Mid = object
# di: DeepInner
Top = object
m: Mid
# Top = object
# m: Mid
var topArr: array[4, ref Top]
for q in 0..100:
for i in 0..<topArr.len:
new topArr[i]
topArr[i].m.di.b = q
doAssert(cast[uint](addr topArr[i].m.di) mod uint(alignof(DeepInner)) == 0)
# var topArr: array[4, ref Top]
# for q in 0..100:
# for i in 0..<topArr.len:
# new topArr[i]
# topArr[i].m.di.b = q
# doAssert(cast[uint](addr topArr[i].m.di) mod uint(alignof(DeepInner)) == 0)