[libcxx-commits] [libcxx] [libcxxabi] [libc++] Provide aligned allocation function iff the compiler claims support (PR #219199)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 28 01:15:34 PDT 2026


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/219199

>From 22d0f724e65ba2ec0a46c30b00b19bda8f922cbf Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 27 Aug 2026 14:26:27 +0200
Subject: [PATCH] [libc++] Provide aligned allocation function iff the compiler
 claims support

---
 libcxx/include/__config                        | 18 ++----------------
 libcxx/include/__new/align_val_t.h             |  8 ++++----
 libcxx/include/__new/global_new_delete.h       |  2 +-
 libcxx/src/include/aligned_alloc.h             |  4 ++--
 libcxx/src/support/new.ipp                     |  4 ++--
 .../new.size_align.except.pass.cpp             |  2 ++
 libcxx/test/support/test_macros.h              |  6 +-----
 libcxxabi/src/fallback_malloc.cpp              |  4 ++--
 8 files changed, 16 insertions(+), 32 deletions(-)

diff --git a/libcxx/include/__config b/libcxx/include/__config
index 31c573ba5dca3..e11bb83afb3ac 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -157,24 +157,10 @@ typedef __char32_t char32_t;
 #    define _DECLARE_C99_LDBL_MATH 1
 #  endif
 
-// If we are getting operator new from the MSVC CRT, then allocation overloads
-// for align_val_t were added in 19.12, aka VS 2017 version 15.3.
-#  if defined(_LIBCPP_MSVCRT) && defined(_MSC_VER) && _MSC_VER < 1912
-#    define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
-#  elif defined(_LIBCPP_ABI_VCRUNTIME) && !defined(__cpp_aligned_new)
-// We're deferring to Microsoft's STL to provide aligned new et al. We don't
-// have it unless the language feature test macro is defined.
-#    define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
-#  elif defined(__MVS__)
-#    define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 0
+#  if defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606
+#    define _LIBCPP_HAS_ALIGNED_ALLOCATION 1
 #  else
-#    define _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION 1
-#  endif
-
-#  if !_LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION || (!defined(__cpp_aligned_new) || __cpp_aligned_new < 201606)
 #    define _LIBCPP_HAS_ALIGNED_ALLOCATION 0
-#  else
-#    define _LIBCPP_HAS_ALIGNED_ALLOCATION 1
 #  endif
 
 #  if defined(__APPLE__) || defined(__FreeBSD__)
diff --git a/libcxx/include/__new/align_val_t.h b/libcxx/include/__new/align_val_t.h
index d8ce5283345fb..657c1dfd8c770 100644
--- a/libcxx/include/__new/align_val_t.h
+++ b/libcxx/include/__new/align_val_t.h
@@ -18,18 +18,18 @@
 
 // <vcruntime_exception.h> defines its own std::align_val_t type,
 // which we use in order to be ABI-compatible with other STLs on Windows.
-#if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION && defined(_LIBCPP_ABI_VCRUNTIME)
+#if defined(_LIBCPP_ABI_VCRUNTIME)
 #  include <vcruntime_new.h>
-#endif
+#elif _LIBCPP_HAS_ALIGNED_ALLOCATION
 
 _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
-#if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION && !defined(_LIBCPP_ABI_VCRUNTIME)
 #  ifndef _LIBCPP_CXX03_LANG
 enum class align_val_t : size_t {};
 #  else
 enum align_val_t { __zero = 0, __max = (size_t)-1 };
 #  endif
-#endif
 _LIBCPP_END_UNVERSIONED_NAMESPACE_STD
 
+#endif
+
 #endif // _LIBCPP___NEW_ALIGN_VAL_T_H
diff --git a/libcxx/include/__new/global_new_delete.h b/libcxx/include/__new/global_new_delete.h
index 4e25295aa20fa..34b715712020d 100644
--- a/libcxx/include/__new/global_new_delete.h
+++ b/libcxx/include/__new/global_new_delete.h
@@ -45,7 +45,7 @@ _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothro
 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
 #  endif
 
-#  if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#  if _LIBCPP_HAS_ALIGNED_ALLOCATION
 [[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
 [[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
 operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
diff --git a/libcxx/src/include/aligned_alloc.h b/libcxx/src/include/aligned_alloc.h
index 2b69a344006a3..1b24b579ebd15 100644
--- a/libcxx/src/include/aligned_alloc.h
+++ b/libcxx/src/include/aligned_alloc.h
@@ -18,7 +18,7 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-#if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#if _LIBCPP_HAS_ALIGNED_ALLOCATION
 
 // Low-level helpers to call the aligned allocation and deallocation functions
 // on the target platform. This is used to implement libc++'s own memory
@@ -58,7 +58,7 @@ inline _LIBCPP_HIDE_FROM_ABI void __libcpp_aligned_free(void* __ptr) {
 #  endif
 }
 
-#endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#endif // _LIBCPP_HAS_ALIGNED_ALLOCATION
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/src/support/new.ipp b/libcxx/src/support/new.ipp
index 761f9bbf187b9..286dc8e1b2564 100644
--- a/libcxx/src/support/new.ipp
+++ b/libcxx/src/support/new.ipp
@@ -113,7 +113,7 @@ OVERRIDABLE_FUNCTION void* operator new[](size_t size) _THROW_BAD_ALLOC { return
 
 [[gnu::weak]] void operator delete[](void* ptr, size_t) noexcept { ::operator delete[](ptr); }
 
-#if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#if _LIBCPP_HAS_ALIGNED_ALLOCATION
 
 template <on_failure failure_mode>
 static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignment) {
@@ -215,4 +215,4 @@ OVERRIDABLE_FUNCTION void* operator new[](size_t size, std::align_val_t alignmen
 [[gnu::weak]] void operator delete[](void* ptr, size_t, std::align_val_t alignment) noexcept {
   ::operator delete[](ptr, alignment);
 }
-#endif // _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#endif // _LIBCPP_HAS_ALIGNED_ALLOCATION
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.except.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.except.pass.cpp
index fbeb880c83d8d..be67fc653286d 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.except.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.except.pass.cpp
@@ -16,6 +16,8 @@
 // nor does the dynamic library shipped with z/OS.
 // XFAIL: target={{.+}}-zos{{.*}}
 
+// ADDITIONAL_COMPILE_FLAGS: -faligned-allocation
+
 #include <new>
 #include <cassert>
 #include <limits>
diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index af5e4e57be5a3..5cceccf1da679 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -231,11 +231,7 @@
 #  define TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT
 #endif
 
-#if defined(_LIBCPP_VERSION) && defined(_LIBCPP_HAS_ALIGNED_ALLOCATION) && !_LIBCPP_HAS_ALIGNED_ALLOCATION
-#  define TEST_HAS_NO_ALIGNED_ALLOCATION
-#elif defined(_LIBCPP_VERSION) && defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) /* old libc++ version */
-#  define TEST_HAS_NO_ALIGNED_ALLOCATION
-#elif TEST_STD_VER < 17 && (!defined(__cpp_aligned_new) || __cpp_aligned_new < 201606L)
+#if (!defined(__cpp_aligned_new) || __cpp_aligned_new < 201606L)
 #  define TEST_HAS_NO_ALIGNED_ALLOCATION
 #endif
 
diff --git a/libcxxabi/src/fallback_malloc.cpp b/libcxxabi/src/fallback_malloc.cpp
index 81195c9a4dc17..7359b1f46d180 100644
--- a/libcxxabi/src/fallback_malloc.cpp
+++ b/libcxxabi/src/fallback_malloc.cpp
@@ -259,7 +259,7 @@ void* __aligned_malloc_with_fallback(size_t size) {
 #if defined(_WIN32)
   if (void* dest = std::__libcpp_aligned_alloc(alignof(__aligned_type), size))
     return dest;
-#elif !_LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#elif !_LIBCPP_HAS_ALIGNED_ALLOCATION
   if (void* dest = ::malloc(size))
     return dest;
 #else
@@ -286,7 +286,7 @@ void __aligned_free_with_fallback(void* ptr) {
   if (is_fallback_ptr(ptr))
     fallback_free(ptr);
   else {
-#if !_LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+#if !_LIBCPP_HAS_ALIGNED_ALLOCATION
     ::free(ptr);
 #else
     std::__libcpp_aligned_free(ptr);



More information about the libcxx-commits mailing list