[libcxx-commits] [libcxx] c778f6c - [libc++] Clean up logic around aligned/sized allocation and deallocation
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Oct 9 09:50:55 PDT 2020
Author: Louis Dionne
Date: 2020-10-09T12:43:28-04:00
New Revision: c778f6c4f9d511023c0d71b9e14a90e60445d314
URL: https://github.com/llvm/llvm-project/commit/c778f6c4f9d511023c0d71b9e14a90e60445d314
DIFF: https://github.com/llvm/llvm-project/commit/c778f6c4f9d511023c0d71b9e14a90e60445d314.diff
LOG: [libc++] Clean up logic around aligned/sized allocation and deallocation
Due to the need to support compilers that implement builtin operator
new/delete but not their align_val_t overloaded versions, there was a
lot of complexity. By assuming that a compiler that supports the builtin
new/delete operators also supports their align_val_t overloads, the code
can be simplified quite a bit.
Differential Revision: https://reviews.llvm.org/D88301
Added:
Modified:
libcxx/include/__config
libcxx/include/new
Removed:
################################################################################
diff --git a/libcxx/include/__config b/libcxx/include/__config
index b51261132ee8..8d2ab2e51b11 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -868,10 +868,6 @@ typedef unsigned int char32_t;
# define _LIBCPP_EXPLICIT
#endif
-#if !__has_builtin(__builtin_operator_new) || !__has_builtin(__builtin_operator_delete)
-#define _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
-#endif
-
#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
# define _LIBCPP_DECLARE_STRONG_ENUM(x) struct _LIBCPP_TYPE_VIS x { enum __lx
# define _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(x) \
diff --git a/libcxx/include/new b/libcxx/include/new
index dbfdab9abe4d..e21c22bc2ed1 100644
--- a/libcxx/include/new
+++ b/libcxx/include/new
@@ -117,11 +117,6 @@ void operator delete[](void* ptr, void*) noexcept;
# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
#endif
-#if !__has_builtin(__builtin_operator_new) || \
- __has_builtin(__builtin_operator_new) < 201802L
-#define _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE
-#endif
-
namespace std // purposefully not using versioning namespace
{
@@ -234,80 +229,54 @@ _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new
#endif
}
-inline _LIBCPP_INLINE_VISIBILITY
-void *__libcpp_allocate(size_t __size, size_t __align) {
-#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
- if (__is_overaligned_for_new(__align)) {
- const align_val_t __align_val = static_cast<align_val_t>(__align);
-# ifdef _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE
- return ::operator new(__size, __align_val);
-# else
- return __builtin_operator_new(__size, __align_val);
-# endif
- }
+template <class ..._Args>
+_LIBCPP_INLINE_VISIBILITY
+void* __libcpp_operator_new(_Args ...__args) {
+#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
+ return __builtin_operator_new(__args...);
#else
- ((void)__align);
-#endif
-#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
- return ::operator new(__size);
-#else
- return __builtin_operator_new(__size);
+ return ::operator new(__args...);
#endif
}
-struct _DeallocateCaller {
- template <class _A1, class _A2>
- static inline void __do_call(void *__ptr, _A1 __a1, _A2 __a2) {
-#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \
- defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)
- return ::operator delete(__ptr, __a1, __a2);
+template <class ..._Args>
+_LIBCPP_INLINE_VISIBILITY
+void __libcpp_operator_delete(_Args ...__args) {
+#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
+ __builtin_operator_delete(__args...);
#else
- return __builtin_operator_delete(__ptr, __a1, __a2);
+ ::operator delete(__args...);
#endif
- }
+}
- template <class _A1>
- static inline void __do_call(void *__ptr, _A1 __a1) {
-#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \
- defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)
- return ::operator delete(__ptr, __a1);
-#else
- return __builtin_operator_delete(__ptr, __a1);
-#endif
+inline _LIBCPP_INLINE_VISIBILITY
+void *__libcpp_allocate(size_t __size, size_t __align) {
+#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
+ if (__is_overaligned_for_new(__align)) {
+ const align_val_t __align_val = static_cast<align_val_t>(__align);
+ return __libcpp_operator_new(__size, __align_val);
}
-
- static inline void __do_call(void *__ptr) {
-#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
- return ::operator delete(__ptr);
-#else
- return __builtin_operator_delete(__ptr);
#endif
- }
- static inline void __do_deallocate_handle_size(void *__ptr, size_t __size) {
-#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
- ((void)__size);
- return __do_call(__ptr);
-#else
- return __do_call(__ptr, __size);
-#endif
- }
+ (void)__align;
+ return __libcpp_operator_new(__size);
+}
-#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
- static inline void __do_deallocate_handle_size(void *__ptr, size_t __size, align_val_t __align) {
+template <class ..._Args>
+_LIBCPP_INLINE_VISIBILITY
+void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
- ((void)__size);
- return __do_call(__ptr, __align);
+ (void)__size;
+ return __libcpp_operator_delete(__ptr, __args...);
#else
- return __do_call(__ptr, __size, __align);
-#endif
- }
+ return __libcpp_operator_delete(__ptr, __size, __args...);
#endif
+}
- static inline _LIBCPP_INLINE_VISIBILITY
- void __do_deallocate_handle_size_align(void *__ptr, size_t __size, size_t __align) {
+inline _LIBCPP_INLINE_VISIBILITY
+void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
- ((void)__align);
+ (void)__align;
return __do_deallocate_handle_size(__ptr, __size);
#else
if (__is_overaligned_for_new(__align)) {
@@ -317,31 +286,20 @@ struct _DeallocateCaller {
return __do_deallocate_handle_size(__ptr, __size);
}
#endif
- }
+}
- static inline _LIBCPP_INLINE_VISIBILITY
- void __do_deallocate_handle_align(void *__ptr, size_t __align) {
+inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
- ((void)__align);
- return __do_call(__ptr);
+ (void)__align;
+ return __libcpp_operator_delete(__ptr);
#else
if (__is_overaligned_for_new(__align)) {
const align_val_t __align_val = static_cast<align_val_t>(__align);
- return __do_call(__ptr, __align_val);
+ return __libcpp_operator_delete(__ptr, __align_val);
} else {
- return __do_call(__ptr);
+ return __libcpp_operator_delete(__ptr);
}
#endif
- }
-};
-
-inline _LIBCPP_INLINE_VISIBILITY
-void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
- _DeallocateCaller::__do_deallocate_handle_size_align(__ptr, __size, __align);
-}
-
-inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
- _DeallocateCaller::__do_deallocate_handle_align(__ptr, __align);
}
template <class _Tp>
More information about the libcxx-commits
mailing list