Skip to content

Fix heap allocations with small alignment on macOS/Windows - #48

Merged
muit merged 11 commits into
mainfrom
hotfix/heap-align
Oct 13, 2026
Merged

muit merged 11 commits into
mainfrom
hotfix/heap-align

Conversation

@muit

@muit muit commented Oct 13, 2026

Copy link
Copy Markdown
Member

HeapArena::Alloc forwarded alignof(T) straight to aligned_alloc/_aligned_malloc. For char arrays (String/TArray<char>) that is 1, which aligned_alloc rejects on macOS (returns NULL) and _aligned_malloc mishandles on Windows (heap corruption). This crashed the first String move during static init in test runners.

Fall back to the unaligned path for alignments at or below the platform default and round the size up to a multiple of the alignment otherwise.

muit added 11 commits October 13, 2026 12:36
HeapArena::Alloc forwarded alignof(T) straight to aligned_alloc/_aligned_malloc.
For char arrays (String / TArray<char>) that is 1, which aligned_alloc rejects
on macOS (returns NULL) and _aligned_malloc mishandles on Windows (heap
corruption), so the first String move during static init crashed the test
runner.

Fall back to the unaligned path for alignments at or below the platform default
and round the size up to a multiple of the alignment otherwise.
- MSVC / MinGW (MSVCRT >= 7): _aligned_malloc / _aligned_free
- MinGW (older): __mingw_aligned_malloc / __mingw_aligned_free
- Android / SunOS: memalign / free
- POSIX (Linux, macOS, iOS, BSD): posix_memalign (clamped to sizeof(void*)) / free

posix_memalign and memalign avoid C11 aligned_alloc's size-multiple requirement,
and _aligned_malloc is the only aligned allocator MSVC provides.
…pFree

HeapAlloc(size) used _aligned_malloc under P_PLATFORM_WINDOWS (which also
covers older MinGW), while HeapFree dispatches older MinGW to
__mingw_aligned_free - a mismatch. Route the unaligned path through the same
MSVC / MinGW / POSIX branches so every allocation pairs with its free.
CMake always passes -DP_PLATFORM_LINUX=1 on Linux (and Android), so the
#ifndef P_PLATFORM_LINUX block was skipped entirely and P_PLATFORM_ANDROID was
never defined. That made the Android memalign branch (and its <malloc.h>
include) dead code. Detect Android in its own block.
@muit
muit merged commit 135e5b9 into main Oct 13, 2026
26 checks passed
@muit
muit deleted the hotfix/heap-align branch October 15, 2026 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant