-
- Downloads
memory: support for larger-than-page alignment
Our existing implementation of posix_memalign() and the C11 aligned_alloc() used our regular malloc(), so it only work worked up to alignment of 4096 bytes - and crashed when it failed to achieve a higher desired alignment. Some applications do ask for higher alignment - for example MongoDB allocates a large buffer at 8192 byte alignment, and accordingly crashes half of the times, when the desired alignment is not achieved (half of the time, it is achieved by chance). This patch makes our support for alignment better organized, and fixes the alignment > 4096 case: The alignment is no longer available only to the outer function like posix_memalign(). Rather, it is passed down to lower-level allocation functions like malloc_large() which allocates whole pages - and this function now knows how to pick pages which start at a properly aligned boundary. This patch does not improve the wastefulness of our malloc_large(), so an overhaul of it would still be welcome. Case in point, malloc_large() always adds a full page to any allocation larger than half a page. Multiple allocations with posix_memalign(8192, 8192), rather than being tightly packed, each take 3 pages and are separated by a free page. This page is not wasted, but causes fragmentation of the heap. Note that after this patch, we still have one other bug in posix_memalign(size, align) - for small sizes and large alignments. For small sizes, we use a pool allocator with "size" alignment, and may not achieve the desired alignment (so causing an assertion failure). This bug can also be fixed, but is unrelated to this patch. This patch also adds a test for posix_memalign(), checking all alignments including large alignments which are the topic of this patch. The tests for small *sizes*, which as explained above are still buggy, are commented out, because they fail. Fixes #266. Signed-off-by:Nadav Har'El <nyh@cloudius-systems.com> Signed-off-by:
Avi Kivity <avi@avi.cloudius>
Loading
Please register or sign in to comment