Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===--------------------------- new.cpp ----------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 10 | #define _LIBCPP_BUILDING_NEW |
| 11 | |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 13 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | #include "new" |
| 15 | |
Howard Hinnant | 8b5bb3c | 2012-07-26 17:42:39 +0000 | [diff] [blame] | 16 | #ifndef __has_include |
| 17 | #define __has_include(inc) 0 |
| 18 | #endif |
| 19 | |
Eric Fiselier | 3be7f19 | 2014-11-01 00:41:42 +0000 | [diff] [blame] | 20 | #if defined(__APPLE__) && !defined(LIBCXXRT) |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 21 | #include <cxxabi.h> |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 22 | |
| 23 | #ifndef _LIBCPPABI_VERSION |
| 24 | // On Darwin, there are two STL shared libraries and a lower level ABI |
Marshall Clow | a46a0ad | 2013-11-11 23:27:19 +0000 | [diff] [blame] | 25 | // shared library. The global holding the current new handler is |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 26 | // in the ABI library and named __cxa_new_handler. |
| 27 | #define __new_handler __cxxabiapple::__cxa_new_handler |
| 28 | #endif |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 29 | #else // __APPLE__ |
Howard Hinnant | 8b5bb3c | 2012-07-26 17:42:39 +0000 | [diff] [blame] | 30 | #if defined(LIBCXXRT) || __has_include(<cxxabi.h>) |
| 31 | #include <cxxabi.h> |
| 32 | #endif // __has_include(<cxxabi.h>) |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 33 | #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) |
Howard Hinnant | 8b5bb3c | 2012-07-26 17:42:39 +0000 | [diff] [blame] | 34 | static std::new_handler __new_handler; |
| 35 | #endif // _LIBCPPABI_VERSION |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 38 | #ifndef __GLIBCXX__ |
| 39 | |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 40 | // Implement all new and delete operators as weak definitions |
| 41 | // in this shared library, so that they can be overriden by programs |
| 42 | // that define non-weak copies of the functions. |
| 43 | |
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 44 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 45 | void * |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 46 | operator new(std::size_t size) |
Howard Hinnant | 912012e | 2011-07-14 01:34:46 +0000 | [diff] [blame] | 47 | #if !__has_feature(cxx_noexcept) |
| 48 | throw(std::bad_alloc) |
| 49 | #endif |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 50 | { |
| 51 | if (size == 0) |
| 52 | size = 1; |
| 53 | void* p; |
| 54 | while ((p = ::malloc(size)) == 0) |
| 55 | { |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 56 | // If malloc fails and there is a new_handler, |
| 57 | // call it to try free up memory. |
Howard Hinnant | d42c4be | 2010-12-04 19:56:43 +0000 | [diff] [blame] | 58 | std::new_handler nh = std::get_new_handler(); |
Howard Hinnant | 1f970ad | 2010-12-04 19:54:11 +0000 | [diff] [blame] | 59 | if (nh) |
| 60 | nh(); |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 61 | else |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 62 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 63 | throw std::bad_alloc(); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 64 | #else |
| 65 | break; |
| 66 | #endif |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 67 | } |
| 68 | return p; |
| 69 | } |
| 70 | |
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 71 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 72 | void* |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 73 | operator new(size_t size, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 74 | { |
| 75 | void* p = 0; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 76 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 77 | try |
| 78 | { |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 79 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 80 | p = ::operator new(size); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 81 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 82 | } |
| 83 | catch (...) |
| 84 | { |
| 85 | } |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 86 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 87 | return p; |
| 88 | } |
| 89 | |
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 90 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 91 | void* |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 92 | operator new[](size_t size) |
Howard Hinnant | 912012e | 2011-07-14 01:34:46 +0000 | [diff] [blame] | 93 | #if !__has_feature(cxx_noexcept) |
| 94 | throw(std::bad_alloc) |
| 95 | #endif |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 96 | { |
| 97 | return ::operator new(size); |
| 98 | } |
| 99 | |
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 100 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 101 | void* |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 102 | operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 103 | { |
| 104 | void* p = 0; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 105 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 106 | try |
| 107 | { |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 108 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 109 | p = ::operator new[](size); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 110 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 111 | } |
| 112 | catch (...) |
| 113 | { |
| 114 | } |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 115 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 116 | return p; |
| 117 | } |
| 118 | |
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 119 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 120 | void |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 121 | operator delete(void* ptr) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 122 | { |
| 123 | if (ptr) |
| 124 | ::free(ptr); |
| 125 | } |
| 126 | |
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 127 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 128 | void |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 129 | operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 130 | { |
| 131 | ::operator delete(ptr); |
| 132 | } |
| 133 | |
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 134 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 135 | void |
Larisse Voufo | 74f95a0 | 2015-02-20 06:13:05 +0000 | [diff] [blame] | 136 | operator delete(void* ptr, size_t) _NOEXCEPT |
Larisse Voufo | 19efe01 | 2015-02-15 05:18:55 +0000 | [diff] [blame] | 137 | { |
| 138 | ::operator delete(ptr); |
| 139 | } |
| 140 | |
| 141 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
| 142 | void |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 143 | operator delete[] (void* ptr) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 144 | { |
Larisse Voufo | 19efe01 | 2015-02-15 05:18:55 +0000 | [diff] [blame] | 145 | ::operator delete(ptr); |
| 146 | } |
| 147 | |
| 148 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
| 149 | void |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 150 | operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 151 | { |
| 152 | ::operator delete[](ptr); |
| 153 | } |
| 154 | |
Larisse Voufo | 19efe01 | 2015-02-15 05:18:55 +0000 | [diff] [blame] | 155 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
| 156 | void |
Larisse Voufo | 74f95a0 | 2015-02-20 06:13:05 +0000 | [diff] [blame] | 157 | operator delete[] (void* ptr, size_t) _NOEXCEPT |
Larisse Voufo | 19efe01 | 2015-02-15 05:18:55 +0000 | [diff] [blame] | 158 | { |
| 159 | ::operator delete[](ptr); |
| 160 | } |
| 161 | |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 162 | #endif // !__GLIBCXX__ |
| 163 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 164 | namespace std |
| 165 | { |
| 166 | |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 167 | #ifndef __GLIBCXX__ |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 168 | const nothrow_t nothrow = {}; |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 169 | #endif |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 170 | |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 171 | #ifndef _LIBCPPABI_VERSION |
| 172 | |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 173 | #ifndef __GLIBCXX__ |
| 174 | |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 175 | new_handler |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 176 | set_new_handler(new_handler handler) _NOEXCEPT |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 177 | { |
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 178 | return __sync_lock_test_and_set(&__new_handler, handler); |
| 179 | } |
| 180 | |
| 181 | new_handler |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 182 | get_new_handler() _NOEXCEPT |
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 183 | { |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 184 | return __sync_fetch_and_add(&__new_handler, nullptr); |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 187 | #endif // !__GLIBCXX__ |
| 188 | |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 189 | #ifndef LIBCXXRT |
| 190 | |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 191 | bad_alloc::bad_alloc() _NOEXCEPT |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 192 | { |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 195 | #ifndef __GLIBCXX__ |
| 196 | |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 197 | bad_alloc::~bad_alloc() _NOEXCEPT |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 198 | { |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 201 | const char* |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 202 | bad_alloc::what() const _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 203 | { |
| 204 | return "std::bad_alloc"; |
| 205 | } |
| 206 | |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 207 | #endif // !__GLIBCXX__ |
| 208 | |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 209 | bad_array_new_length::bad_array_new_length() _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 210 | { |
| 211 | } |
| 212 | |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 213 | bad_array_new_length::~bad_array_new_length() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | { |
| 215 | } |
| 216 | |
Eric Fiselier | f53135f | 2014-11-01 00:11:25 +0000 | [diff] [blame] | 217 | const char* |
| 218 | bad_array_new_length::what() const _NOEXCEPT |
| 219 | { |
| 220 | return "bad_array_new_length"; |
| 221 | } |
| 222 | |
Eric Fiselier | 3fdac97 | 2014-10-29 23:14:53 +0000 | [diff] [blame] | 223 | #endif //LIBCXXRT |
| 224 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 225 | const char* |
Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 226 | bad_array_length::what() const _NOEXCEPT |
| 227 | { |
| 228 | return "bad_array_length"; |
| 229 | } |
| 230 | |
| 231 | bad_array_length::bad_array_length() _NOEXCEPT |
| 232 | { |
| 233 | } |
| 234 | |
| 235 | bad_array_length::~bad_array_length() _NOEXCEPT |
| 236 | { |
| 237 | } |
| 238 | |
Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 239 | #endif // _LIBCPPABI_VERSION |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 240 | |
Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 +0000 | [diff] [blame] | 241 | #ifndef LIBSTDCXX |
| 242 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 243 | void |
| 244 | __throw_bad_alloc() |
| 245 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 246 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 247 | throw bad_alloc(); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 248 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 +0000 | [diff] [blame] | 251 | #endif // !LIBSTDCXX |
| 252 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 253 | } // std |