hubbertj opened a new pull request, #13361:
URL: https://github.com/apache/gluten/pull/13361

   ## What changes are proposed in this pull request?
   
   Fixes #13359.
   
   When `StdMemoryAllocator::reallocateAligned` shrank an aligned memory block 
(`newSize <= size`), it previously attempted a shrink-to-fit optimization via 
`reallocate(p, size, aligned, out)` calling `std::realloc`. Standard 
`std::realloc` only guarantees fundamental alignment 
(`alignof(std::max_align_t)`), losing 64-byte alignment on macOS arm64 when 
small blocks are relocated into smaller allocator buckets.
   
   This PR makes the following improvements:
   1. Removes the `std::realloc` shrink-to-fit shortcut in 
`StdMemoryAllocator::reallocateAligned`, matching Arrow's 
`SystemMemoryPool::ReallocateAligned` behavior (always allocate an aligned 
block, copy prefix bytes, and free the previous block).
   2. Uses `ROUND_TO_LINE(newSize, alignment)` for the allocation size passed 
to `std::aligned_alloc` in `reallocateAligned`, preventing C11/C++17 failures 
on macOS where `size` must be an integral multiple of `alignment`.
   3. Harmonizes `StdMemoryAllocator::allocateAligned` with 
`ROUND_TO_LINE(size, alignment)` for consistent behavior across platforms.
   4. Preserves accurate byte accounting consistent with 
`ListenableMemoryAllocator`.
   
   ## How was this patch tested?
   
   Added comprehensive unit tests in `cpp/core/tests/MemoryAllocatorTest.cc`:
   - `TEST(StdMemoryAllocator, allocateAligned)`: tests allocating aligned 
blocks with non-multiple sizes and checks address alignment and byte accounting.
   - `TEST(StdMemoryAllocator, reallocateAlignedShrinkAndGrow)`: tests 
shrinking an aligned block (16 KB -> 64 bytes and 64 bytes -> 10 bytes) 
verifying alignment preservation, data integrity, and byte accounting, followed 
by growing the allocation.
   - `TEST(ListenableMemoryAllocator, alignedOperations)`: verifies listener 
callbacks and byte tracking during aligned allocation, shrinking, growing, and 
freeing.
   
   ## Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Antigravity
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to