Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------- new ------------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_NEW |
| 11 | #define _LIBCPP_NEW |
| 12 | |
| 13 | /* |
| 14 | new synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | class bad_alloc |
| 20 | : public exception |
| 21 | { |
| 22 | public: |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 23 | bad_alloc() noexcept; |
| 24 | bad_alloc(const bad_alloc&) noexcept; |
| 25 | bad_alloc& operator=(const bad_alloc&) noexcept; |
| 26 | virtual const char* what() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 29 | class bad_array_new_length : public bad_alloc // C++14 |
Marshall Clow | 3bf7713 | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 30 | { |
| 31 | public: |
| 32 | bad_array_new_length() noexcept; |
| 33 | }; |
| 34 | |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 35 | enum class align_val_t : size_t {}; // C++17 |
Eric Fiselier | 02a8a2c | 2019-05-23 23:46:44 +0000 | [diff] [blame^] | 36 | |
| 37 | struct destroying_delete_t { // C++20 |
| 38 | explicit destroying_delete_t() = default; |
| 39 | }; |
| 40 | inline constexpr destroying_delete_t destroying_delete{}; // C++20 |
| 41 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | struct nothrow_t {}; |
| 43 | extern const nothrow_t nothrow; |
| 44 | typedef void (*new_handler)(); |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 45 | new_handler set_new_handler(new_handler new_p) noexcept; |
| 46 | new_handler get_new_handler() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | |
Marshall Clow | df5a11f | 2017-11-22 19:49:03 +0000 | [diff] [blame] | 48 | // 21.6.4, pointer optimization barrier |
| 49 | template <class T> constexpr T* launder(T* p) noexcept; // C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 50 | } // std |
| 51 | |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 52 | void* operator new(std::size_t size); // replaceable, nodiscard in C++2a |
| 53 | void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++2a |
| 54 | void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 55 | void* operator new(std::size_t size, std::align_val_t alignment, |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 56 | const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 57 | void operator delete(void* ptr) noexcept; // replaceable |
Larisse Voufo | 574190d | 2015-02-15 05:18:55 +0000 | [diff] [blame] | 58 | void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14 |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 59 | void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17 |
| 60 | void operator delete(void* ptr, std::size_t size, |
| 61 | std::align_val_t alignment) noexcept; // replaceable, C++17 |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 62 | void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 63 | void operator delete(void* ptr, std:align_val_t alignment, |
| 64 | const std::nothrow_t&) noexcept; // replaceable, C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 66 | void* operator new[](std::size_t size); // replaceable, nodiscard in C++2a |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 67 | void* operator new[](std::size_t size, |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 68 | std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++2a |
| 69 | void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 70 | void* operator new[](std::size_t size, std::align_val_t alignment, |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 71 | const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 72 | void operator delete[](void* ptr) noexcept; // replaceable |
Larisse Voufo | 574190d | 2015-02-15 05:18:55 +0000 | [diff] [blame] | 73 | void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14 |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 74 | void operator delete[](void* ptr, |
| 75 | std::align_val_t alignment) noexcept; // replaceable, C++17 |
| 76 | void operator delete[](void* ptr, std::size_t size, |
| 77 | std::align_val_t alignment) noexcept; // replaceable, C++17 |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 78 | void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 79 | void operator delete[](void* ptr, std::align_val_t alignment, |
| 80 | const std::nothrow_t&) noexcept; // replaceable, C++17 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 82 | void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++2a |
| 83 | void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++2a |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 84 | void operator delete (void* ptr, void*) noexcept; |
| 85 | void operator delete[](void* ptr, void*) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 86 | |
| 87 | */ |
| 88 | |
| 89 | #include <__config> |
| 90 | #include <exception> |
Eric Fiselier | 06548ab | 2018-03-22 04:42:56 +0000 | [diff] [blame] | 91 | #include <type_traits> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | #include <cstddef> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 93 | #include <version> |
Eric Fiselier | 279c319 | 2016-09-06 21:25:27 +0000 | [diff] [blame] | 94 | #ifdef _LIBCPP_NO_EXCEPTIONS |
| 95 | #include <cstdlib> |
| 96 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 97 | |
Eric Fiselier | 85f6633 | 2019-03-05 01:57:01 +0000 | [diff] [blame] | 98 | #if defined(_LIBCPP_ABI_VCRUNTIME) |
Eric Fiselier | ec3a167 | 2017-02-10 08:57:35 +0000 | [diff] [blame] | 99 | #include <new.h> |
| 100 | #endif |
| 101 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 102 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 103 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 104 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 105 | |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 106 | #if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L |
| 107 | #define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION |
| 108 | #endif |
| 109 | |
| 110 | #if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \ |
| 111 | defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION) |
Eric Fiselier | ff4c8a2 | 2018-10-11 00:17:24 +0000 | [diff] [blame] | 112 | # define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION |
| 113 | #endif |
| 114 | |
| 115 | #if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \ |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 116 | defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION) |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 117 | # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION |
| 118 | #endif |
| 119 | |
Eric Fiselier | 06548ab | 2018-03-22 04:42:56 +0000 | [diff] [blame] | 120 | #if !__has_builtin(__builtin_operator_new) || \ |
Eric Fiselier | ff4c8a2 | 2018-10-11 00:17:24 +0000 | [diff] [blame] | 121 | __has_builtin(__builtin_operator_new) < 201802L |
| 122 | #define _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE |
Eric Fiselier | 06548ab | 2018-03-22 04:42:56 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 125 | namespace std // purposefully not using versioning namespace |
| 126 | { |
| 127 | |
Eric Fiselier | 85f6633 | 2019-03-05 01:57:01 +0000 | [diff] [blame] | 128 | #if !defined(_LIBCPP_ABI_VCRUNTIME) |
Eric Fiselier | ec3a167 | 2017-02-10 08:57:35 +0000 | [diff] [blame] | 129 | struct _LIBCPP_TYPE_VIS nothrow_t {}; |
| 130 | extern _LIBCPP_FUNC_VIS const nothrow_t nothrow; |
| 131 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 132 | class _LIBCPP_EXCEPTION_ABI bad_alloc |
| 133 | : public exception |
| 134 | { |
| 135 | public: |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 136 | bad_alloc() _NOEXCEPT; |
| 137 | virtual ~bad_alloc() _NOEXCEPT; |
| 138 | virtual const char* what() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | class _LIBCPP_EXCEPTION_ABI bad_array_new_length |
| 142 | : public bad_alloc |
| 143 | { |
| 144 | public: |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 145 | bad_array_new_length() _NOEXCEPT; |
| 146 | virtual ~bad_array_new_length() _NOEXCEPT; |
| 147 | virtual const char* what() const _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Eric Fiselier | ec3a167 | 2017-02-10 08:57:35 +0000 | [diff] [blame] | 150 | typedef void (*new_handler)(); |
| 151 | _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT; |
| 152 | _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT; |
| 153 | |
Eric Fiselier | 85f6633 | 2019-03-05 01:57:01 +0000 | [diff] [blame] | 154 | #endif // !_LIBCPP_ABI_VCRUNTIME |
Eric Fiselier | ec3a167 | 2017-02-10 08:57:35 +0000 | [diff] [blame] | 155 | |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 156 | _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec |
| 157 | |
Eric Fiselier | ff4c8a2 | 2018-10-11 00:17:24 +0000 | [diff] [blame] | 158 | #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \ |
Eric Fiselier | 85f6633 | 2019-03-05 01:57:01 +0000 | [diff] [blame] | 159 | !defined(_LIBCPP_ABI_VCRUNTIME) |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 160 | #ifndef _LIBCPP_CXX03_LANG |
| 161 | enum class _LIBCPP_ENUM_VIS align_val_t : size_t { }; |
| 162 | #else |
| 163 | enum align_val_t { __zero = 0, __max = (size_t)-1 }; |
| 164 | #endif |
| 165 | #endif |
| 166 | |
Eric Fiselier | 02a8a2c | 2019-05-23 23:46:44 +0000 | [diff] [blame^] | 167 | #if _LIBCPP_STD_VER > 17 |
| 168 | // Enable the declaration even if the compiler doesn't support the language |
| 169 | // feature. |
| 170 | struct destroying_delete_t { |
| 171 | explicit destroying_delete_t() = default; |
| 172 | }; |
| 173 | _LIBCPP_INLINE_VAR constexpr destroying_delete_t destroying_delete{}; |
| 174 | #endif // _LIBCPP_STD_VER > 17 |
| 175 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 176 | } // std |
| 177 | |
Eric Fiselier | 499a0a5 | 2017-01-02 23:27:42 +0000 | [diff] [blame] | 178 | #if defined(_LIBCPP_CXX03_LANG) |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 179 | #define _THROW_BAD_ALLOC throw(std::bad_alloc) |
| 180 | #else |
| 181 | #define _THROW_BAD_ALLOC |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 182 | #endif |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 183 | |
Eric Fiselier | 85f6633 | 2019-03-05 01:57:01 +0000 | [diff] [blame] | 184 | #if !defined(_LIBCPP_ABI_VCRUNTIME) |
Eric Fiselier | ec3a167 | 2017-02-10 08:57:35 +0000 | [diff] [blame] | 185 | |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 186 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC; |
Louis Dionne | e147b35 | 2019-02-26 06:34:42 +0000 | [diff] [blame] | 187 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; |
Shoaib Meenai | 2d71db4 | 2016-11-16 22:18:10 +0000 | [diff] [blame] | 188 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT; |
| 189 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT; |
Eric Fiselier | ff4c8a2 | 2018-10-11 00:17:24 +0000 | [diff] [blame] | 190 | #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 191 | _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT; |
Larisse Voufo | a744a7e | 2015-02-20 06:13:05 +0000 | [diff] [blame] | 192 | #endif |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 193 | |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 194 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC; |
Louis Dionne | e147b35 | 2019-02-26 06:34:42 +0000 | [diff] [blame] | 195 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; |
Shoaib Meenai | 2d71db4 | 2016-11-16 22:18:10 +0000 | [diff] [blame] | 196 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT; |
| 197 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT; |
Eric Fiselier | ff4c8a2 | 2018-10-11 00:17:24 +0000 | [diff] [blame] | 198 | #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 199 | _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT; |
Larisse Voufo | a744a7e | 2015-02-20 06:13:05 +0000 | [diff] [blame] | 200 | #endif |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 201 | |
Eric Fiselier | ff4c8a2 | 2018-10-11 00:17:24 +0000 | [diff] [blame] | 202 | #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 203 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; |
Louis Dionne | e147b35 | 2019-02-26 06:34:42 +0000 | [diff] [blame] | 204 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; |
Akira Hatanaka | 8c77d15 | 2017-06-30 18:50:23 +0000 | [diff] [blame] | 205 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT; |
| 206 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; |
Eric Fiselier | ff4c8a2 | 2018-10-11 00:17:24 +0000 | [diff] [blame] | 207 | #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION |
Akira Hatanaka | 8c77d15 | 2017-06-30 18:50:23 +0000 | [diff] [blame] | 208 | _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 209 | #endif |
| 210 | |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 211 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC; |
Louis Dionne | e147b35 | 2019-02-26 06:34:42 +0000 | [diff] [blame] | 212 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS; |
Akira Hatanaka | 8c77d15 | 2017-06-30 18:50:23 +0000 | [diff] [blame] | 213 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT; |
| 214 | _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT; |
Eric Fiselier | ff4c8a2 | 2018-10-11 00:17:24 +0000 | [diff] [blame] | 215 | #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION |
Akira Hatanaka | 8c77d15 | 2017-06-30 18:50:23 +0000 | [diff] [blame] | 216 | _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT; |
Eric Fiselier | 162922f | 2016-10-14 06:46:30 +0000 | [diff] [blame] | 217 | #endif |
| 218 | #endif |
| 219 | |
Marshall Clow | be8b84d | 2017-12-04 23:03:42 +0000 | [diff] [blame] | 220 | _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;} |
| 221 | _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;} |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 222 | inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {} |
| 223 | inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 224 | |
Eric Fiselier | 85f6633 | 2019-03-05 01:57:01 +0000 | [diff] [blame] | 225 | #endif // !_LIBCPP_ABI_VCRUNTIME |
Eric Fiselier | ec3a167 | 2017-02-10 08:57:35 +0000 | [diff] [blame] | 226 | |
Richard Smith | 1f1c147 | 2014-06-04 19:54:15 +0000 | [diff] [blame] | 227 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 228 | |
Eric Fiselier | 06548ab | 2018-03-22 04:42:56 +0000 | [diff] [blame] | 229 | _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT { |
| 230 | #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ |
| 231 | return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__; |
| 232 | #else |
| 233 | return __align > alignment_of<max_align_t>::value; |
| 234 | #endif |
| 235 | } |
| 236 | |
| 237 | inline _LIBCPP_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) { |
| 238 | #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION |
| 239 | if (__is_overaligned_for_new(__align)) { |
| 240 | const align_val_t __align_val = static_cast<align_val_t>(__align); |
Eric Fiselier | 6535ce2 | 2018-10-11 01:48:00 +0000 | [diff] [blame] | 241 | # ifdef _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE |
Eric Fiselier | 06548ab | 2018-03-22 04:42:56 +0000 | [diff] [blame] | 242 | return ::operator new(__size, __align_val); |
| 243 | # else |
| 244 | return __builtin_operator_new(__size, __align_val); |
| 245 | # endif |
| 246 | } |
| 247 | #else |
| 248 | ((void)__align); |
| 249 | #endif |
Richard Smith | 1f1c147 | 2014-06-04 19:54:15 +0000 | [diff] [blame] | 250 | #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE |
| 251 | return ::operator new(__size); |
| 252 | #else |
| 253 | return __builtin_operator_new(__size); |
| 254 | #endif |
| 255 | } |
| 256 | |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 257 | struct _DeallocateCaller { |
| 258 | static inline _LIBCPP_INLINE_VISIBILITY |
| 259 | void __do_deallocate_handle_size_align(void *__ptr, size_t __size, size_t __align) { |
| 260 | #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) |
| 261 | ((void)__align); |
| 262 | return __do_deallocate_handle_size(__ptr, __size); |
| 263 | #else |
| 264 | if (__is_overaligned_for_new(__align)) { |
| 265 | const align_val_t __align_val = static_cast<align_val_t>(__align); |
| 266 | return __do_deallocate_handle_size(__ptr, __size, __align_val); |
| 267 | } else { |
| 268 | return __do_deallocate_handle_size(__ptr, __size); |
| 269 | } |
| 270 | #endif |
Eric Fiselier | 01744a7 | 2018-10-24 22:44:01 +0000 | [diff] [blame] | 271 | } |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 272 | |
| 273 | static inline _LIBCPP_INLINE_VISIBILITY |
| 274 | void __do_deallocate_handle_align(void *__ptr, size_t __align) { |
| 275 | #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) |
| 276 | ((void)__align); |
| 277 | return __do_call(__ptr); |
Eric Fiselier | 01744a7 | 2018-10-24 22:44:01 +0000 | [diff] [blame] | 278 | #else |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 279 | if (__is_overaligned_for_new(__align)) { |
| 280 | const align_val_t __align_val = static_cast<align_val_t>(__align); |
| 281 | return __do_call(__ptr, __align_val); |
| 282 | } else { |
| 283 | return __do_call(__ptr); |
| 284 | } |
Eric Fiselier | 01744a7 | 2018-10-24 22:44:01 +0000 | [diff] [blame] | 285 | #endif |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | private: |
| 289 | static inline void __do_deallocate_handle_size(void *__ptr, size_t __size) { |
| 290 | #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION |
| 291 | ((void)__size); |
| 292 | return __do_call(__ptr); |
| 293 | #else |
| 294 | return __do_call(__ptr, __size); |
| 295 | #endif |
| 296 | } |
| 297 | |
| 298 | #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION |
| 299 | static inline void __do_deallocate_handle_size(void *__ptr, size_t __size, align_val_t __align) { |
| 300 | #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION |
| 301 | ((void)__size); |
| 302 | return __do_call(__ptr, __align); |
| 303 | #else |
| 304 | return __do_call(__ptr, __size, __align); |
| 305 | #endif |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | private: |
| 310 | template <class _A1, class _A2> |
| 311 | static inline void __do_call(void *__ptr, _A1 __a1, _A2 __a2) { |
| 312 | #if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \ |
| 313 | defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE) |
| 314 | return ::operator delete(__ptr, __a1, __a2); |
| 315 | #else |
| 316 | return __builtin_operator_delete(__ptr, __a1, __a2); |
| 317 | #endif |
| 318 | } |
| 319 | |
| 320 | template <class _A1> |
| 321 | static inline void __do_call(void *__ptr, _A1 __a1) { |
| 322 | #if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \ |
| 323 | defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE) |
| 324 | return ::operator delete(__ptr, __a1); |
| 325 | #else |
| 326 | return __builtin_operator_delete(__ptr, __a1); |
| 327 | #endif |
| 328 | } |
| 329 | |
| 330 | static inline void __do_call(void *__ptr) { |
Eric Fiselier | 06548ab | 2018-03-22 04:42:56 +0000 | [diff] [blame] | 331 | #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 332 | return ::operator delete(__ptr); |
Eric Fiselier | 06548ab | 2018-03-22 04:42:56 +0000 | [diff] [blame] | 333 | #else |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 334 | return __builtin_operator_delete(__ptr); |
Richard Smith | 1f1c147 | 2014-06-04 19:54:15 +0000 | [diff] [blame] | 335 | #endif |
Eric Fiselier | 2856ef8 | 2018-10-25 17:21:30 +0000 | [diff] [blame] | 336 | } |
| 337 | }; |
| 338 | |
| 339 | inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) { |
| 340 | _DeallocateCaller::__do_deallocate_handle_size_align(__ptr, __size, __align); |
| 341 | } |
| 342 | |
| 343 | inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) { |
| 344 | _DeallocateCaller::__do_deallocate_handle_align(__ptr, __align); |
Richard Smith | 1f1c147 | 2014-06-04 19:54:15 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Marshall Clow | df5a11f | 2017-11-22 19:49:03 +0000 | [diff] [blame] | 347 | template <class _Tp> |
Louis Dionne | 44bcff9 | 2018-08-03 22:36:53 +0000 | [diff] [blame] | 348 | _LIBCPP_NODISCARD_AFTER_CXX17 inline |
Marshall Clow | df5a11f | 2017-11-22 19:49:03 +0000 | [diff] [blame] | 349 | _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT |
| 350 | { |
| 351 | static_assert (!(is_function<_Tp>::value), "can't launder functions" ); |
| 352 | static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" ); |
| 353 | #ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER |
| 354 | return __builtin_launder(__p); |
| 355 | #else |
| 356 | return __p; |
| 357 | #endif |
| 358 | } |
| 359 | |
| 360 | |
| 361 | #if _LIBCPP_STD_VER > 14 |
| 362 | template <class _Tp> |
| 363 | _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY |
| 364 | constexpr _Tp* launder(_Tp* __p) noexcept |
| 365 | { |
Marshall Clow | 6ac349c | 2017-11-23 01:25:03 +0000 | [diff] [blame] | 366 | return _VSTD::__launder(__p); |
Marshall Clow | df5a11f | 2017-11-22 19:49:03 +0000 | [diff] [blame] | 367 | } |
| 368 | #endif |
| 369 | |
Richard Smith | 1f1c147 | 2014-06-04 19:54:15 +0000 | [diff] [blame] | 370 | _LIBCPP_END_NAMESPACE_STD |
| 371 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 372 | #endif // _LIBCPP_NEW |