Libstdc++ now allocates only what it reports for P0401.
Libstdc++'s allocate_at_least now rounds allocation requests down to the reported size, addressing discrepancies during deallocation.
The allocate_at_least function in libstdc++ was rounding up allocation requests to the default alignment, potentially allocating more memory than reported. This difference caused issues during deallocation. The patch modifies the allocation algorithm to align request sizes with what’s reported, avoiding over-allocation and fixing a test failure on -m32. It also adds guards for -fno-aligned-new and -fno-sized-deallocation.
In Details
The allocate_at_least function in new_allocator.h now avoids rounding up the allocation size to its default alignment. This patch ensures that the amount of memory allocated matches what is reported to the caller, fixing discrepancies during deallocation. The algorithm was tweaked after Godbolt inspection suggested multiplication is better than division on non-x86; this commit also fixes the remaining -m32 test failure under the new allocation method, and adds guards for -fno-aligned-new and -fno-sized-deallocation.
For Context
libstdc++ is the C++ standard library that comes with GCC. Memory allocators manage how programs obtain and release chunks of memory. When a program asks for a certain amount of memory, the allocator might actually provide a slightly larger block due to alignment requirements. This commit ensures that the library keeps track of the actual amount of memory it reports as allocated, preventing potential mismatches and errors when the memory is later released back to the system. This change mainly affects programs that rely on precise memory management and custom allocators.