[libc++] Use aligned_alloc instead of posix_memalign for C++17
C++17 defines the C11 `aligned_alloc`, so we can use that instead of posix_memalign. This change allows building against picolibc without defining _DEFAULT_SOURCE/_GNU_SOURCE. The C11 `aligned_alloc` function should be available on all supported non-Windows platforms except for macOS where we need version 10.15. There is one caveat: aligned_alloc() requires that __size is a multiple of __alignment, but [new.delete.general] only states "if the value of an alignment argument passed to any of these functions is not a valid alignment value, the behavior is undefined". To handle calls such as ::operator new(1, std::align_val_t(128)), we round up __size to __alignment (and check for wrap-around). This is required at least for macOS where aligned_alloc(128, 1) returns an error instead of allocating memory (glibc ignores the specification). Differential Revision: https://reviews.llvm.org/D138196
Loading
Please sign in to comment