[libcxx-commits] [libcxx] [libc++] Provide aligned allocation function iff the compiler claims support (PR #219199)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Aug 27 05:26:54 PDT 2026
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/219199
None
>From 2075097dc4862b6d41745b3d065b5ee6372e594f 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 ++--
libcxx/test/support/test_macros.h | 6 +-----
6 files changed, 12 insertions(+), 30 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/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
More information about the libcxx-commits
mailing list