From f57690001e6fbb1d7590a1a7184a82ca4848a189 Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 12:36:12 +0200 Subject: [PATCH 01/11] Fix heap allocations with small alignment on macOS/Windows HeapArena::Alloc forwarded alignof(T) straight to aligned_alloc/_aligned_malloc. For char arrays (String / TArray) 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. --- Src/PipeMemory.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index 50d3ac71..87ca5c56 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -128,10 +128,19 @@ namespace p void* HeapAlloc(sizet size, sizet align) { + // aligned_alloc/_aligned_malloc require a valid alignment (a power of two and at least + // the platform minimum); aligned_alloc additionally requires a size that is a multiple + // of it. Small alignments (e.g. alignof(char) == 1) fall back to the unaligned path, + // which already guarantees the default alignment. + if (align <= __STDCPP_DEFAULT_NEW_ALIGNMENT__) + { + return HeapAlloc(size); + } + const sizet alignedSize = (size + align - 1) & ~(align - 1); #if P_PLATFORM_WINDOWS - return _aligned_malloc(size, align); + return _aligned_malloc(alignedSize, align); #else - return aligned_alloc(align, size); + return aligned_alloc(align, alignedSize); #endif } From 91a73999e8afaf7602a2043165d8e886f8ec1eb4 Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 12:46:48 +0200 Subject: [PATCH 02/11] Use posix_memalign with a clamped alignment (mirrors boost::alignment) --- Src/PipeMemory.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index 87ca5c56..c455a6ce 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -128,19 +128,22 @@ namespace p void* HeapAlloc(sizet size, sizet align) { - // aligned_alloc/_aligned_malloc require a valid alignment (a power of two and at least - // the platform minimum); aligned_alloc additionally requires a size that is a multiple - // of it. Small alignments (e.g. alignof(char) == 1) fall back to the unaligned path, - // which already guarantees the default alignment. - if (align <= __STDCPP_DEFAULT_NEW_ALIGNMENT__) + // Mirrors boost::alignment: posix_memalign / _aligned_malloc require a valid alignment + // (a power of two and at least sizeof(void*)). posix_memalign, unlike aligned_alloc, + // does not require the size to be a multiple of the alignment. + if (align < sizeof(void*)) { - return HeapAlloc(size); + align = sizeof(void*); } - const sizet alignedSize = (size + align - 1) & ~(align - 1); #if P_PLATFORM_WINDOWS - return _aligned_malloc(alignedSize, align); + return _aligned_malloc(size, align); #else - return aligned_alloc(align, alignedSize); + void* ptr = nullptr; + if (::posix_memalign(&ptr, align, size) != 0) + { + ptr = nullptr; + } + return ptr; #endif } From 3b4b6daa6695ce6ba0353035ad26ae54f8edf249 Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 12:52:59 +0200 Subject: [PATCH 03/11] Match boost::alignment per-platform aligned allocation (posix_memalign/memalign/_aligned_malloc) --- Src/PipeMemory.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index c455a6ce..9c8b4ab8 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -11,6 +11,8 @@ #if P_PLATFORM_WINDOWS #include // _aligned_malloc, _aligned_free, _aligned_realloc +#elif P_PLATFORM_ANDROID + #include // memalign #endif @@ -128,15 +130,21 @@ namespace p void* HeapAlloc(sizet size, sizet align) { - // Mirrors boost::alignment: posix_memalign / _aligned_malloc require a valid alignment - // (a power of two and at least sizeof(void*)). posix_memalign, unlike aligned_alloc, - // does not require the size to be a multiple of the alignment. + // Mirrors boost::alignment, whose backends differ per platform: + // - Windows: _aligned_malloc / _aligned_free + // - Android: memalign / free + // - POSIX (Linux, macOS, iOS, BSD): posix_memalign / free + // All of them need a power-of-two alignment; posix_memalign also requires it to be at + // least sizeof(void*). Unlike C11 aligned_alloc, none require size to be a multiple of + // the alignment. if (align < sizeof(void*)) { align = sizeof(void*); } #if P_PLATFORM_WINDOWS return _aligned_malloc(size, align); +#elif P_PLATFORM_ANDROID + return ::memalign(align, size); #else void* ptr = nullptr; if (::posix_memalign(&ptr, align, size) != 0) From 0ce5ee7653fc1d2867cbbfb32283106bb0305213 Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 13:06:10 +0200 Subject: [PATCH 04/11] Match boost::alignment per-platform behavior for aligned allocation - 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. --- Src/PipeMemory.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index 9c8b4ab8..00bed707 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -130,22 +130,25 @@ namespace p void* HeapAlloc(sizet size, sizet align) { - // Mirrors boost::alignment, whose backends differ per platform: - // - Windows: _aligned_malloc / _aligned_free - // - Android: memalign / free - // - POSIX (Linux, macOS, iOS, BSD): posix_memalign / free - // All of them need a power-of-two alignment; posix_memalign also requires it to be at - // least sizeof(void*). Unlike C11 aligned_alloc, none require size to be a multiple of - // the alignment. + // Same behavior as boost::alignment::aligned_alloc, which dispatches per platform: + // - 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, SunOS 5.11+): posix_memalign -> free + // posix_memalign needs a power-of-two alignment of at least sizeof(void*); the others + // accept any power of two. None require the size to be a multiple of the alignment + // (unlike C11 aligned_alloc). +#if defined(_MSC_VER) || (defined(__MINGW32__) && (__MSVCRT_VERSION__ >= 0x0700)) + return ::_aligned_malloc(size, align); +#elif defined(__MINGW32__) + return ::__mingw_aligned_malloc(size, align); +#elif P_PLATFORM_ANDROID + return ::memalign(align, size); +#else if (align < sizeof(void*)) { align = sizeof(void*); } -#if P_PLATFORM_WINDOWS - return _aligned_malloc(size, align); -#elif P_PLATFORM_ANDROID - return ::memalign(align, size); -#else void* ptr = nullptr; if (::posix_memalign(&ptr, align, size) != 0) { @@ -166,10 +169,12 @@ namespace p void HeapFree(void* ptr) { -#if P_PLATFORM_WINDOWS - _aligned_free(ptr); +#if defined(_MSC_VER) || (defined(__MINGW32__) && (__MSVCRT_VERSION__ >= 0x0700)) + ::_aligned_free(ptr); +#elif defined(__MINGW32__) + ::__mingw_aligned_free(ptr); #else - free(ptr); + ::free(ptr); #endif } From 82a717ecfe8861b43a31addc7af10f512b44950d Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 13:22:37 +0200 Subject: [PATCH 05/11] Make unaligned HeapAlloc follow the same per-platform dispatch as HeapFree 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. --- Src/PipeMemory.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index 00bed707..f1333582 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -121,10 +121,14 @@ namespace p void* HeapAlloc(sizet size) { -#if P_PLATFORM_WINDOWS - return _aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__); + // Must stay paired with HeapFree: MSVC/MinGW use the aligned CRT allocators, POSIX uses + // malloc. Both provide at least the default new alignment. +#if defined(_MSC_VER) || (defined(__MINGW32__) && (__MSVCRT_VERSION__ >= 0x0700)) + return ::_aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__); +#elif defined(__MINGW32__) + return ::__mingw_aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__); #else - return malloc(size); + return ::malloc(size); #endif } From 08f51904b8c76e44c0a8f34bbd0573c52390848f Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 13:43:42 +0200 Subject: [PATCH 06/11] Define P_PLATFORM_ANDROID independently of P_PLATFORM_LINUX 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 include) dead code. Detect Android in its own block. --- Include/PipePlatform.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Include/PipePlatform.h b/Include/PipePlatform.h index d29b955b..b56a10bb 100644 --- a/Include/PipePlatform.h +++ b/Include/PipePlatform.h @@ -33,15 +33,18 @@ #define P_PLATFORM_WINDOWS 0 #endif #endif +#ifndef P_PLATFORM_ANDROID + #if defined(__ANDROID__) || defined(ANDROID) + #define P_PLATFORM_ANDROID 1 + #else + #define P_PLATFORM_ANDROID 0 + #endif +#endif #ifndef P_PLATFORM_LINUX #if defined(__linux__) #define P_PLATFORM_LINUX 1 - #if defined(__ANDROID__) || defined(ANDROID) - #define P_PLATFORM_ANDROID 1 - #endif #else #define P_PLATFORM_LINUX 0 - #define P_PLATFORM_ANDROID 0 #endif #endif #ifndef P_PLATFORM_APPLE From cc8c7782b5108e002935df586f2d7d182a9f588a Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 14:01:04 +0200 Subject: [PATCH 07/11] Drop boost reference from the aligned allocation comment --- Src/PipeMemory.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index f1333582..5ce5cf40 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -134,7 +134,6 @@ namespace p void* HeapAlloc(sizet size, sizet align) { - // Same behavior as boost::alignment::aligned_alloc, which dispatches per platform: // - MSVC / MinGW (MSVCRT >= 7): _aligned_malloc -> _aligned_free // - MinGW (older): __mingw_aligned_malloc -> __mingw_aligned_free // - Android / SunOS: memalign -> free From 749142aa4d4a3d68f457426cc48589a86f66d718 Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 14:06:29 +0200 Subject: [PATCH 08/11] Move Android platform detection after the other platform blocks --- Include/PipePlatform.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Include/PipePlatform.h b/Include/PipePlatform.h index b56a10bb..ae71fa52 100644 --- a/Include/PipePlatform.h +++ b/Include/PipePlatform.h @@ -33,13 +33,6 @@ #define P_PLATFORM_WINDOWS 0 #endif #endif -#ifndef P_PLATFORM_ANDROID - #if defined(__ANDROID__) || defined(ANDROID) - #define P_PLATFORM_ANDROID 1 - #else - #define P_PLATFORM_ANDROID 0 - #endif -#endif #ifndef P_PLATFORM_LINUX #if defined(__linux__) #define P_PLATFORM_LINUX 1 @@ -72,6 +65,13 @@ #define P_PLATFORM_BSD 0 #endif #endif +#ifndef P_PLATFORM_ANDROID + #if defined(__ANDROID__) || defined(ANDROID) + #define P_PLATFORM_ANDROID 1 + #else + #define P_PLATFORM_ANDROID 0 + #endif +#endif #if P_PLATFORM_WINDOWS From 9a2919f845dc843b6e3a3f28b8d0bd8ae74e96ac Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 14:10:19 +0200 Subject: [PATCH 09/11] Drop the pre-MSVCRT7 MinGW aligned-allocation special case --- Src/PipeMemory.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index 5ce5cf40..60818784 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -123,10 +123,8 @@ namespace p { // Must stay paired with HeapFree: MSVC/MinGW use the aligned CRT allocators, POSIX uses // malloc. Both provide at least the default new alignment. -#if defined(_MSC_VER) || (defined(__MINGW32__) && (__MSVCRT_VERSION__ >= 0x0700)) +#if defined(_MSC_VER) || defined(__MINGW32__) return ::_aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__); -#elif defined(__MINGW32__) - return ::__mingw_aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__); #else return ::malloc(size); #endif @@ -134,17 +132,15 @@ namespace p void* HeapAlloc(sizet size, sizet align) { - // - 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, SunOS 5.11+): posix_memalign -> free + // Dispatches per platform: + // - MSVC / MinGW: _aligned_malloc -> _aligned_free + // - Android: memalign -> free + // - POSIX (Linux, macOS, iOS, BSD): posix_memalign -> free // posix_memalign needs a power-of-two alignment of at least sizeof(void*); the others // accept any power of two. None require the size to be a multiple of the alignment // (unlike C11 aligned_alloc). -#if defined(_MSC_VER) || (defined(__MINGW32__) && (__MSVCRT_VERSION__ >= 0x0700)) +#if defined(_MSC_VER) || defined(__MINGW32__) return ::_aligned_malloc(size, align); -#elif defined(__MINGW32__) - return ::__mingw_aligned_malloc(size, align); #elif P_PLATFORM_ANDROID return ::memalign(align, size); #else @@ -172,10 +168,8 @@ namespace p void HeapFree(void* ptr) { -#if defined(_MSC_VER) || (defined(__MINGW32__) && (__MSVCRT_VERSION__ >= 0x0700)) +#if defined(_MSC_VER) || defined(__MINGW32__) ::_aligned_free(ptr); -#elif defined(__MINGW32__) - ::__mingw_aligned_free(ptr); #else ::free(ptr); #endif From c061a639d1c2c0502b2405ca54439eaaae40e0ce Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 14:14:22 +0200 Subject: [PATCH 10/11] Use P_PLATFORM_WINDOWS instead of compiler checks in heap allocation --- Src/PipeMemory.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index 60818784..60015958 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -121,9 +121,9 @@ namespace p void* HeapAlloc(sizet size) { - // Must stay paired with HeapFree: MSVC/MinGW use the aligned CRT allocators, POSIX uses + // Must stay paired with HeapFree: Windows uses the aligned CRT allocators, POSIX uses // malloc. Both provide at least the default new alignment. -#if defined(_MSC_VER) || defined(__MINGW32__) +#if P_PLATFORM_WINDOWS return ::_aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__); #else return ::malloc(size); @@ -133,13 +133,13 @@ namespace p void* HeapAlloc(sizet size, sizet align) { // Dispatches per platform: - // - MSVC / MinGW: _aligned_malloc -> _aligned_free - // - Android: memalign -> free + // - Windows: _aligned_malloc -> _aligned_free + // - Android: memalign -> free // - POSIX (Linux, macOS, iOS, BSD): posix_memalign -> free // posix_memalign needs a power-of-two alignment of at least sizeof(void*); the others // accept any power of two. None require the size to be a multiple of the alignment // (unlike C11 aligned_alloc). -#if defined(_MSC_VER) || defined(__MINGW32__) +#if P_PLATFORM_WINDOWS return ::_aligned_malloc(size, align); #elif P_PLATFORM_ANDROID return ::memalign(align, size); @@ -168,7 +168,7 @@ namespace p void HeapFree(void* ptr) { -#if defined(_MSC_VER) || defined(__MINGW32__) +#if P_PLATFORM_WINDOWS ::_aligned_free(ptr); #else ::free(ptr); From dc414787b187478d5039c6bbb9870dac23ac76e8 Mon Sep 17 00:00:00 2001 From: muit Date: Tue, 13 Oct 2026 14:16:22 +0200 Subject: [PATCH 11/11] Drop redundant global-scope qualifier on heap allocation calls --- Src/PipeMemory.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Src/PipeMemory.cpp b/Src/PipeMemory.cpp index 60015958..d4fcedde 100644 --- a/Src/PipeMemory.cpp +++ b/Src/PipeMemory.cpp @@ -124,9 +124,9 @@ namespace p // Must stay paired with HeapFree: Windows uses the aligned CRT allocators, POSIX uses // malloc. Both provide at least the default new alignment. #if P_PLATFORM_WINDOWS - return ::_aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__); + return _aligned_malloc(size, __STDCPP_DEFAULT_NEW_ALIGNMENT__); #else - return ::malloc(size); + return malloc(size); #endif } @@ -140,16 +140,16 @@ namespace p // accept any power of two. None require the size to be a multiple of the alignment // (unlike C11 aligned_alloc). #if P_PLATFORM_WINDOWS - return ::_aligned_malloc(size, align); + return _aligned_malloc(size, align); #elif P_PLATFORM_ANDROID - return ::memalign(align, size); + return memalign(align, size); #else if (align < sizeof(void*)) { align = sizeof(void*); } void* ptr = nullptr; - if (::posix_memalign(&ptr, align, size) != 0) + if (posix_memalign(&ptr, align, size) != 0) { ptr = nullptr; } @@ -169,9 +169,9 @@ namespace p void HeapFree(void* ptr) { #if P_PLATFORM_WINDOWS - ::_aligned_free(ptr); + _aligned_free(ptr); #else - ::free(ptr); + free(ptr); #endif }