Skip to content

Commit f71b638

Browse files
authored
[libcxx] Use aligned_alloc for testing instead of posix_memalign (llvm#101748)
Summary: The `aligned_alloc` function is the C11 replacement for `posix_memalign`. We should favor the C standard over the POSIX standard so more C library implementations can run the tests.
1 parent 42555cd commit f71b638

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int main(int, char**) {
7676
test_allocations(64, 64);
7777
// Size being a multiple of alignment also needs to be supported.
7878
test_allocations(64, 32);
79-
// When aligned allocation is implemented using posix_memalign,
79+
// When aligned allocation is implemented using aligned_alloc,
8080
// that function requires a minimum alignment of sizeof(void*).
8181
// Check that we can also create overaligned allocations with
8282
// an alignment argument less than sizeof(void*).

libcxx/test/support/count_new.h

+12
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,23 @@ void operator delete[](void* p, std::nothrow_t const&) TEST_NOEXCEPT {
455455
# define USE_ALIGNED_ALLOC
456456
# endif
457457

458+
# if defined(__APPLE__)
459+
# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
460+
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500)
461+
# define TEST_HAS_NO_C11_ALIGNED_ALLOC
462+
# endif
463+
# elif defined(__ANDROID__) && __ANDROID_API__ < 28
464+
# define TEST_HAS_NO_C11_ALIGNED_ALLOC
465+
# endif
466+
458467
inline void* allocate_aligned_impl(std::size_t size, std::align_val_t align) {
459468
const std::size_t alignment = static_cast<std::size_t>(align);
460469
void* ret = nullptr;
461470
# ifdef USE_ALIGNED_ALLOC
462471
ret = _aligned_malloc(size, alignment);
472+
# elif TEST_STD_VER >= 17 && !defined(TEST_HAS_NO_C11_ALIGNED_ALLOC)
473+
size_t rounded_size = (size + alignment - 1) & ~(alignment - 1);
474+
ret = aligned_alloc(alignment, size > rounded_size ? size : rounded_size);
463475
# else
464476
assert(posix_memalign(&ret, std::max(alignment, sizeof(void*)), size) != EINVAL);
465477
# endif

0 commit comments

Comments
 (0)