Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 1 | //===------------------------ cxa_new_delete.cpp --------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | // |
| 9 | // This file implements the new and delete operators. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Marshall Clow | 6c3f675 | 2013-09-11 01:42:02 +0000 | [diff] [blame] | 12 | #define _LIBCPP_BUILDING_NEW |
| 13 | |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 14 | #include <new> |
| 15 | #include <cstdlib> |
| 16 | |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 17 | #if !defined(_THROW_BAD_ALLOC) || !defined(_NOEXCEPT) |
| 18 | #error _THROW_BAD_ALLOC and _NOEXCEPT libc++ macros must already be defined \ |
| 19 | by libc++. |
| 20 | #endif |
| 21 | |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 22 | /* |
| 23 | [new.delete.single] |
| 24 | |
| 25 | * Executes a loop: Within the loop, the function first attempts to allocate |
| 26 | the requested storage. Whether the attempt involves a call to the Standard C |
| 27 | library function malloc is unspecified. |
| 28 | |
| 29 | * Returns a pointer to the allocated storage if the attempt is successful. |
| 30 | Otherwise, if the current new_handler (18.6.2.5) is a null pointer value, |
| 31 | throws bad_alloc. |
| 32 | |
| 33 | * Otherwise, the function calls the current new_handler function (18.6.2.3). |
| 34 | If the called function returns, the loop repeats. |
| 35 | |
| 36 | * The loop terminates when an attempt to allocate the requested storage is |
| 37 | successful or when a called new_handler function does not return. |
| 38 | */ |
| 39 | __attribute__((__weak__, __visibility__("default"))) |
| 40 | void * |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 41 | operator new(std::size_t size) _THROW_BAD_ALLOC |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 42 | { |
| 43 | if (size == 0) |
| 44 | size = 1; |
| 45 | void* p; |
| 46 | while ((p = std::malloc(size)) == 0) |
| 47 | { |
| 48 | std::new_handler nh = std::get_new_handler(); |
| 49 | if (nh) |
| 50 | nh(); |
| 51 | else |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 52 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 53 | throw std::bad_alloc(); |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 54 | #else |
| 55 | break; |
| 56 | #endif |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 57 | } |
| 58 | return p; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | Note: The relationships among these operators is both carefully considered |
| 63 | and standard in C++11. Please do not change them without fully understanding |
| 64 | the consequences of doing so. Reference: |
| 65 | http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2158.html |
| 66 | */ |
| 67 | /* |
| 68 | [new.delete.single] |
| 69 | |
| 70 | Calls operator new(size). If the call returns normally, returns the result of |
| 71 | that call. Otherwise, returns a null pointer. |
| 72 | */ |
| 73 | __attribute__((__weak__, __visibility__("default"))) |
| 74 | void* |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 75 | operator new(size_t size, const std::nothrow_t&) _NOEXCEPT |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 76 | { |
| 77 | void* p = 0; |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 78 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 79 | try |
| 80 | { |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 81 | #endif |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 82 | p = ::operator new(size); |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 83 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 84 | } |
| 85 | catch (...) |
| 86 | { |
| 87 | } |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 88 | #endif |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 89 | return p; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | [new.delete.array] |
| 94 | |
| 95 | Returns operator new(size). |
| 96 | */ |
| 97 | __attribute__((__weak__, __visibility__("default"))) |
| 98 | void* |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 99 | operator new[](size_t size) _THROW_BAD_ALLOC |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 100 | { |
| 101 | return ::operator new(size); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | [new.delete.array] |
| 106 | |
| 107 | Calls operator new[](size). If the call returns normally, returns the result |
| 108 | of that call. Otherwise, returns a null pointer. |
| 109 | */ |
| 110 | __attribute__((__weak__, __visibility__("default"))) |
| 111 | void* |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 112 | operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 113 | { |
| 114 | void* p = 0; |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 115 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 116 | try |
| 117 | { |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 118 | #endif |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 119 | p = ::operator new[](size); |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 120 | #ifndef _LIBCXXABI_NO_EXCEPTIONS |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 121 | } |
| 122 | catch (...) |
| 123 | { |
| 124 | } |
Asiri Rathnayake | 4174e8b | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 125 | #endif |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 126 | return p; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | [new.delete.single] |
| 131 | |
| 132 | If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the |
| 133 | earlier call to operator new. |
| 134 | */ |
| 135 | __attribute__((__weak__, __visibility__("default"))) |
| 136 | void |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 137 | operator delete(void* ptr) _NOEXCEPT |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 138 | { |
| 139 | if (ptr) |
| 140 | std::free(ptr); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | [new.delete.single] |
| 145 | |
| 146 | calls operator delete(ptr) |
| 147 | */ |
| 148 | __attribute__((__weak__, __visibility__("default"))) |
| 149 | void |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 150 | operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 151 | { |
| 152 | ::operator delete(ptr); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | [new.delete.array] |
| 157 | |
| 158 | Calls operator delete(ptr) |
| 159 | */ |
| 160 | __attribute__((__weak__, __visibility__("default"))) |
| 161 | void |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 162 | operator delete[] (void* ptr) _NOEXCEPT |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 163 | { |
| 164 | ::operator delete(ptr); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | [new.delete.array] |
| 169 | |
| 170 | calls operator delete[](ptr) |
| 171 | */ |
| 172 | __attribute__((__weak__, __visibility__("default"))) |
| 173 | void |
Eric Fiselier | 81b06b3 | 2017-01-03 00:16:18 +0000 | [diff] [blame] | 174 | operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT |
Howard Hinnant | 4a6e103 | 2011-12-20 20:38:05 +0000 | [diff] [blame] | 175 | { |
| 176 | ::operator delete[](ptr); |
| 177 | } |
Howard Hinnant | 35b2c2a | 2012-01-24 21:35:18 +0000 | [diff] [blame] | 178 | |
| 179 | namespace std |
| 180 | { |
| 181 | |
| 182 | // bad_alloc |
| 183 | |
| 184 | bad_alloc::bad_alloc() _NOEXCEPT |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | bad_alloc::~bad_alloc() _NOEXCEPT |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | const char* |
| 193 | bad_alloc::what() const _NOEXCEPT |
| 194 | { |
| 195 | return "std::bad_alloc"; |
| 196 | } |
| 197 | |
| 198 | // bad_array_new_length |
| 199 | |
| 200 | bad_array_new_length::bad_array_new_length() _NOEXCEPT |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | bad_array_new_length::~bad_array_new_length() _NOEXCEPT |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | const char* |
| 209 | bad_array_new_length::what() const _NOEXCEPT |
| 210 | { |
| 211 | return "bad_array_new_length"; |
| 212 | } |
| 213 | |
Marshall Clow | 6c3f675 | 2013-09-11 01:42:02 +0000 | [diff] [blame] | 214 | // bad_array_length |
| 215 | |
Howard Hinnant | 38a0eeb | 2013-11-07 17:16:37 +0000 | [diff] [blame] | 216 | #ifndef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED |
| 217 | |
| 218 | class _LIBCPP_EXCEPTION_ABI bad_array_length |
| 219 | : public bad_alloc |
| 220 | { |
| 221 | public: |
| 222 | bad_array_length() _NOEXCEPT; |
| 223 | virtual ~bad_array_length() _NOEXCEPT; |
| 224 | virtual const char* what() const _NOEXCEPT; |
| 225 | }; |
| 226 | |
| 227 | #endif // _LIBCPP_BAD_ARRAY_LENGTH_DEFINED |
| 228 | |
Marshall Clow | 6c3f675 | 2013-09-11 01:42:02 +0000 | [diff] [blame] | 229 | bad_array_length::bad_array_length() _NOEXCEPT |
| 230 | { |
| 231 | } |
| 232 | |
| 233 | bad_array_length::~bad_array_length() _NOEXCEPT |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | const char* |
| 238 | bad_array_length::what() const _NOEXCEPT |
| 239 | { |
| 240 | return "bad_array_length"; |
| 241 | } |
| 242 | |
Howard Hinnant | 35b2c2a | 2012-01-24 21:35:18 +0000 | [diff] [blame] | 243 | } // std |