Skip to content

Commit 0c3a2fa

Browse files
authored
[libc++] Simplify the implementation of __libcpp_{,de}allocate (llvm#147989)
GCC 15 also supports `__buitin_operator_{new,delete}` now, so the `#else` cases are dead code. This patch inlines the calls to the wrapper functions and simplifies some surrounding code.
1 parent d7dc536 commit 0c3a2fa

File tree

1 file changed

+20
-51
lines changed

1 file changed

+20
-51
lines changed

libcxx/include/__new/allocate.h

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,16 @@ _LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(siz
3131
#endif
3232
}
3333

34-
template <class... _Args>
35-
_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {
36-
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
37-
return __builtin_operator_new(__args...);
38-
#else
39-
return ::operator new(__args...);
40-
#endif
41-
}
42-
43-
template <class... _Args>
44-
_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) _NOEXCEPT {
45-
#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
46-
__builtin_operator_delete(__args...);
47-
#else
48-
::operator delete(__args...);
49-
#endif
50-
}
51-
5234
template <class _Tp>
5335
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Tp*
54-
__libcpp_allocate(__element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) {
36+
__libcpp_allocate(__element_count __n, [[__maybe_unused__]] size_t __align = _LIBCPP_ALIGNOF(_Tp)) {
5537
size_t __size = static_cast<size_t>(__n) * sizeof(_Tp);
5638
#if _LIBCPP_HAS_ALIGNED_ALLOCATION
57-
if (__is_overaligned_for_new(__align)) {
58-
const align_val_t __align_val = static_cast<align_val_t>(__align);
59-
return static_cast<_Tp*>(std::__libcpp_operator_new(__size, __align_val));
60-
}
39+
if (__is_overaligned_for_new(__align))
40+
return static_cast<_Tp*>(__builtin_operator_new(__size, static_cast<align_val_t>(__align)));
6141
#endif
6242

63-
(void)__align;
64-
return static_cast<_Tp*>(std::__libcpp_operator_new(__size));
43+
return static_cast<_Tp*>(__builtin_operator_new(__size));
6544
}
6645

6746
#if _LIBCPP_HAS_SIZED_DEALLOCATION
@@ -71,39 +50,29 @@ __libcpp_allocate(__element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) {
7150
#endif
7251

7352
template <class _Tp>
74-
inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(
75-
__type_identity_t<_Tp>* __ptr, __element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {
76-
size_t __size = static_cast<size_t>(__n) * sizeof(_Tp);
77-
(void)__size;
78-
#if !_LIBCPP_HAS_ALIGNED_ALLOCATION
79-
(void)__align;
80-
return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size));
81-
#else
82-
if (__is_overaligned_for_new(__align)) {
83-
const align_val_t __align_val = static_cast<align_val_t>(__align);
84-
return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size), __align_val);
85-
} else {
86-
return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size));
87-
}
53+
inline _LIBCPP_HIDE_FROM_ABI void
54+
__libcpp_deallocate(__type_identity_t<_Tp>* __ptr,
55+
__element_count __n,
56+
[[__maybe_unused__]] size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {
57+
[[__maybe_unused__]] size_t __size = static_cast<size_t>(__n) * sizeof(_Tp);
58+
#if _LIBCPP_HAS_ALIGNED_ALLOCATION
59+
if (__is_overaligned_for_new(__align))
60+
return __builtin_operator_delete(
61+
__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size), static_cast<align_val_t>(__align));
8862
#endif
63+
return __builtin_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size));
8964
}
9065

9166
#undef _LIBCPP_ONLY_IF_SIZED_DEALLOCATION
9267

9368
template <class _Tp>
94-
inline _LIBCPP_HIDE_FROM_ABI void
95-
__libcpp_deallocate_unsized(__type_identity_t<_Tp>* __ptr, size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {
96-
#if !_LIBCPP_HAS_ALIGNED_ALLOCATION
97-
(void)__align;
98-
return std::__libcpp_operator_delete(__ptr);
99-
#else
100-
if (__is_overaligned_for_new(__align)) {
101-
const align_val_t __align_val = static_cast<align_val_t>(__align);
102-
return std::__libcpp_operator_delete(__ptr, __align_val);
103-
} else {
104-
return std::__libcpp_operator_delete(__ptr);
105-
}
69+
inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(
70+
__type_identity_t<_Tp>* __ptr, [[__maybe_unused__]] size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {
71+
#if _LIBCPP_HAS_ALIGNED_ALLOCATION
72+
if (__is_overaligned_for_new(__align))
73+
return __builtin_operator_delete(__ptr, static_cast<align_val_t>(__align));
10674
#endif
75+
return __builtin_operator_delete(__ptr);
10776
}
10877
_LIBCPP_END_NAMESPACE_STD
10978

0 commit comments

Comments
 (0)