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