[libcxx-commits] [libcxx] [libc++] Remove __libcpp_deallocate_unsized (PR #234163)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Oct 21 04:34:41 PDT 2026
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/234163
`__libcpp_deallocate_unsized` is only used in a single place, and there it isn't even useful to use the `__builtin_` version, since the allocation doesn't make use of it. We can simplify the code by inlining the function instead.
>From 5053b0ca9fc9d9c07573a34ffb7aecac3d1c5635 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Wed, 21 Oct 2026 13:32:13 +0200
Subject: [PATCH] [libc++] Remove __libcpp_deallocate_unsized
---
libcxx/include/__configuration/compiler.h | 6 ++++++
libcxx/include/__memory/unique_temporary_buffer.h | 11 +++++++----
libcxx/include/__new/allocate.h | 9 ---------
.../support.dynamic/libcpp_deallocate.sh.cpp | 11 -----------
4 files changed, 13 insertions(+), 24 deletions(-)
diff --git a/libcxx/include/__configuration/compiler.h b/libcxx/include/__configuration/compiler.h
index c4896347921a22..d4d8144ef159f2 100644
--- a/libcxx/include/__configuration/compiler.h
+++ b/libcxx/include/__configuration/compiler.h
@@ -71,6 +71,12 @@
# error "libc++ only supports C++03 with Clang-based compilers. Please enable C++11"
# endif
+# ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
+# define _LIBCPP_DEFAULT_NEW_ALIGNMENT __STDCPP_DEFAULT_NEW_ALIGNMENT__
+# else
+# define _LIBCPP_DEFAULT_NEW_ALIGNMENT __BIGGEST_ALIGNMENT__
+# endif
+
#endif
#endif // _LIBCPP___CONFIGURATION_COMPILER_H
diff --git a/libcxx/include/__memory/unique_temporary_buffer.h b/libcxx/include/__memory/unique_temporary_buffer.h
index 32a3f0f081c00e..e2610f22c96c5b 100644
--- a/libcxx/include/__memory/unique_temporary_buffer.h
+++ b/libcxx/include/__memory/unique_temporary_buffer.h
@@ -16,7 +16,6 @@
#include <__cstddef/ptrdiff_t.h>
#include <__memory/allocator.h>
#include <__memory/unique_ptr.h>
-#include <__new/allocate.h>
#include <__new/global_new_delete.h>
#include <__type_traits/is_constant_evaluated.h>
@@ -40,7 +39,11 @@ struct __temporary_buffer_deleter {
return;
}
- std::__libcpp_deallocate_unsized<_Tp>(__ptr);
+#if _LIBCPP_HAS_ALIGNED_ALLOCATION
+ if _LIBCPP_CONSTEXPR (_LIBCPP_ALIGNOF(_Tp) > _LIBCPP_DEFAULT_NEW_ALIGNMENT)
+ return ::operator delete(__ptr, static_cast<align_val_t>(_LIBCPP_ALIGNOF(_Tp)));
+#endif
+ return ::operator delete(__ptr);
}
};
@@ -64,14 +67,14 @@ __allocate_unique_temporary_buffer(ptrdiff_t __count) {
__count = __max_count;
while (__count > 0) {
#if _LIBCPP_HAS_ALIGNED_ALLOCATION
- if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) {
+ if _LIBCPP_CONSTEXPR (_LIBCPP_ALIGNOF(_Tp) > _LIBCPP_DEFAULT_NEW_ALIGNMENT) {
align_val_t __al = align_val_t(_LIBCPP_ALIGNOF(_Tp));
__ptr = static_cast<_Tp*>(::operator new(__count * sizeof(_Tp), __al, nothrow));
} else {
__ptr = static_cast<_Tp*>(::operator new(__count * sizeof(_Tp), nothrow));
}
#else
- if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp))) {
+ if _LIBCPP_CONSTEXPR (_LIBCPP_ALIGNOF(_Tp) > _LIBCPP_DEFAULT_NEW_ALIGNMENT) {
// Since aligned operator new is unavailable, constructs an empty buffer rather than one with invalid alignment.
return __unique_buffer_type();
}
diff --git a/libcxx/include/__new/allocate.h b/libcxx/include/__new/allocate.h
index b9bc2e1a50710e..506cbb7c3af838 100644
--- a/libcxx/include/__new/allocate.h
+++ b/libcxx/include/__new/allocate.h
@@ -64,15 +64,6 @@ __libcpp_deallocate(__type_identity_t<_Tp>* __ptr,
#undef _LIBCPP_ONLY_IF_SIZED_DEALLOCATION
-template <class _Tp>
-inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(
- __type_identity_t<_Tp>* __ptr, [[__maybe_unused__]] size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {
-#if _LIBCPP_HAS_ALIGNED_ALLOCATION
- if (__is_overaligned_for_new(__align))
- return __builtin_operator_delete(__ptr, static_cast<align_val_t>(__align));
-#endif
- return __builtin_operator_delete(__ptr);
-}
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___NEW_ALLOCATE_H
diff --git a/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp b/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp
index 282d49d727c8c3..b3f7c30981df5f 100644
--- a/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp
+++ b/libcxx/test/libcxx/language.support/support.dynamic/libcpp_deallocate.sh.cpp
@@ -134,12 +134,6 @@ void test_libcpp_dealloc() {
std::size_t under_align_val = TEST_ALIGNOF(int);
std::size_t with_size_val = 2;
- {
- std::__libcpp_deallocate_unsized<char>(static_cast<char*>(p), under_align_val);
- assert(stats.expect_plain());
- }
- stats.reset();
-
#if defined(NO_SIZE) && defined(NO_ALIGN)
{
std::__libcpp_deallocate<char>(static_cast<char*>(p), std::__element_count(with_size_val), over_align_val);
@@ -164,11 +158,6 @@ void test_libcpp_dealloc() {
assert(stats.expect_size_align(with_size_val, over_align_val));
}
stats.reset();
- {
- std::__libcpp_deallocate_unsized<char>(static_cast<char*>(p), over_align_val);
- assert(stats.expect_align(over_align_val));
- }
- stats.reset();
{
std::__libcpp_deallocate<char>(static_cast<char*>(p), std::__element_count(with_size_val), under_align_val);
assert(stats.expect_size(with_size_val));
More information about the libcxx-commits
mailing list