[libcxx-commits] [libcxx] f71b638 - [libcxx] Use `aligned_alloc` for testing instead of `posix_memalign` (#101748)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 15 05:11:35 PDT 2024


Author: Joseph Huber
Date: 2024-08-15T07:11:31-05:00
New Revision: f71b63865140cf3c286baf3a77ba3e467f929504

URL: https://github.com/llvm/llvm-project/commit/f71b63865140cf3c286baf3a77ba3e467f929504
DIFF: https://github.com/llvm/llvm-project/commit/f71b63865140cf3c286baf3a77ba3e467f929504.diff

LOG: [libcxx] Use `aligned_alloc` for testing instead of `posix_memalign` (#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.

Added: 
    

Modified: 
    libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp
    libcxx/test/support/count_new.h

Removed: 
    


################################################################################
diff  --git a/libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp b/libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp
index 69c46f00fb65dc..87f4783e12973e 100644
--- a/libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp
+++ b/libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp
@@ -76,7 +76,7 @@ int main(int, char**) {
   test_allocations(64, 64);
   // Size being a multiple of alignment also needs to be supported.
   test_allocations(64, 32);
-  // When aligned allocation is implemented using posix_memalign,
+  // When aligned allocation is implemented using aligned_alloc,
   // that function requires a minimum alignment of sizeof(void*).
   // Check that we can also create overaligned allocations with
   // an alignment argument less than sizeof(void*).

diff  --git a/libcxx/test/support/count_new.h b/libcxx/test/support/count_new.h
index 2298c4fd63e845..61c8ca16ab0d04 100644
--- a/libcxx/test/support/count_new.h
+++ b/libcxx/test/support/count_new.h
@@ -455,11 +455,23 @@ void operator delete[](void* p, std::nothrow_t const&) TEST_NOEXCEPT {
 #      define USE_ALIGNED_ALLOC
 #    endif
 
+#    if defined(__APPLE__)
+#      if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) &&                                                   \
+           __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500)
+#        define TEST_HAS_NO_C11_ALIGNED_ALLOC
+#      endif
+#    elif defined(__ANDROID__) && __ANDROID_API__ < 28
+#      define TEST_HAS_NO_C11_ALIGNED_ALLOC
+#    endif
+
 inline void* allocate_aligned_impl(std::size_t size, std::align_val_t align) {
   const std::size_t alignment = static_cast<std::size_t>(align);
   void* ret                   = nullptr;
 #    ifdef USE_ALIGNED_ALLOC
   ret = _aligned_malloc(size, alignment);
+#    elif TEST_STD_VER >= 17 && !defined(TEST_HAS_NO_C11_ALIGNED_ALLOC)
+  size_t rounded_size = (size + alignment - 1) & ~(alignment - 1);
+  ret                 = aligned_alloc(alignment, size > rounded_size ? size : rounded_size);
 #    else
   assert(posix_memalign(&ret, std::max(alignment, sizeof(void*)), size) != EINVAL);
 #    endif


        


More information about the libcxx-commits mailing list