From d6e016f6ae4cab7d4aa3b780fccd1a523400d873 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 2 Feb 2026 17:30:27 +0800 Subject: [PATCH] progress --- lib/system/alloc.nim | 9 ++--- tests/align/talign.nim | 74 +++++++++++++++++++++--------------------- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim index 63f96a23da..8a61852dde 100644 --- a/lib/system/alloc.nim +++ b/lib/system/alloc.nim @@ -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 diff --git a/tests/align/talign.nim b/tests/align/talign.nim index 6397e31214..9c4c238098 100644 --- a/tests/align/talign.nim +++ b/tests/align/talign.nim @@ -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..