diff options
Diffstat (limited to 'contrib/llvm-project/libcxx/include/new')
-rw-r--r-- | contrib/llvm-project/libcxx/include/new | 173 |
1 files changed, 80 insertions, 93 deletions
diff --git a/contrib/llvm-project/libcxx/include/new b/contrib/llvm-project/libcxx/include/new index 40d351e9b770..0562cef45868 100644 --- a/contrib/llvm-project/libcxx/include/new +++ b/contrib/llvm-project/libcxx/include/new @@ -49,11 +49,11 @@ new_handler get_new_handler() noexcept; template <class T> constexpr T* launder(T* p) noexcept; // C++17 } // std -void* operator new(std::size_t size); // replaceable, nodiscard in C++2a -void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++2a -void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a +void* operator new(std::size_t size); // replaceable, nodiscard in C++20 +void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20 +void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20 void* operator new(std::size_t size, std::align_val_t alignment, - const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a + const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20 void operator delete(void* ptr) noexcept; // replaceable void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14 void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17 @@ -63,12 +63,12 @@ void operator delete(void* ptr, const std::nothrow_t&) noexcept; // repla void operator delete(void* ptr, std:align_val_t alignment, const std::nothrow_t&) noexcept; // replaceable, C++17 -void* operator new[](std::size_t size); // replaceable, nodiscard in C++2a +void* operator new[](std::size_t size); // replaceable, nodiscard in C++20 void* operator new[](std::size_t size, - std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++2a -void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a + std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20 +void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20 void* operator new[](std::size_t size, std::align_val_t alignment, - const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a + const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20 void operator delete[](void* ptr) noexcept; // replaceable void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14 void operator delete[](void* ptr, @@ -79,21 +79,20 @@ void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // repla void operator delete[](void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept; // replaceable, C++17 -void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++2a -void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++2a +void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20 +void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20 void operator delete (void* ptr, void*) noexcept; void operator delete[](void* ptr, void*) noexcept; */ #include <__config> +#include <__availability> +#include <cstddef> +#include <cstdlib> #include <exception> #include <type_traits> -#include <cstddef> #include <version> -#ifdef _LIBCPP_NO_EXCEPTIONS -#include <cstdlib> -#endif #if defined(_LIBCPP_ABI_VCRUNTIME) #include <new.h> @@ -117,11 +116,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,31 +228,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) { +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 + return ::operator new(__args...); +#endif +} + +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 + ::operator delete(__args...); +#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 + return __libcpp_operator_new(__size, __align_val); } -#else - ((void)__align); #endif -#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE - return ::operator new(__size); + + (void)__align; + return __libcpp_operator_new(__size); +} + +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 __libcpp_operator_delete(__ptr, __args...); #else - return __builtin_operator_new(__size); + return __libcpp_operator_delete(__ptr, __size, __args...); #endif } -struct _DeallocateCaller { - 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)) { @@ -268,81 +285,51 @@ 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 - } - - private: - 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 - } - -#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION - static inline void __do_deallocate_handle_size(void *__ptr, size_t __size, align_val_t __align) { -#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION - ((void)__size); - return __do_call(__ptr, __align); -#else - return __do_call(__ptr, __size, __align); -#endif - } -#endif - -private: - 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); -#else - return __builtin_operator_delete(__ptr, __a1, __a2); -#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); +#if !defined(_LIBCPP_HAS_NO_LIBRARY_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 +// allocation routines -- if you need to allocate memory inside the library, +// chances are that you want to use `__libcpp_allocate` instead. +// +// Returns the allocated memory, or `nullptr` on failure. +inline _LIBCPP_INLINE_VISIBILITY +void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) { +#if defined(_LIBCPP_MSVCRT_LIKE) + return ::_aligned_malloc(__size, __alignment); #else - return __builtin_operator_delete(__ptr, __a1); + void* __result = nullptr; + ::posix_memalign(&__result, __alignment, __size); + // If posix_memalign fails, __result is unmodified so we still return `nullptr`. + return __result; #endif - } +} - static inline void __do_call(void *__ptr) { -#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE - return ::operator delete(__ptr); +inline _LIBCPP_INLINE_VISIBILITY +void __libcpp_aligned_free(void* __ptr) { +#if defined(_LIBCPP_MSVCRT_LIKE) + ::_aligned_free(__ptr); #else - return __builtin_operator_delete(__ptr); + ::free(__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); } +#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION -inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) { - _DeallocateCaller::__do_deallocate_handle_align(__ptr, __align); -} template <class _Tp> _LIBCPP_NODISCARD_AFTER_CXX17 inline |