Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===--------------------------- new.cpp ----------------------------------===// |
| 2 | // |
Howard Hinnant | c566dc3 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | ee11c31 | 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 | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Marshall Clow | 3bf7713 | 2013-09-11 01:38:42 +0000 | [diff] [blame^] | 10 | #define _LIBCPP_BUILDING_NEW |
| 11 | |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 13 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | #include "new" |
| 15 | |
Howard Hinnant | a4cabeb | 2012-07-26 17:42:39 +0000 | [diff] [blame] | 16 | #ifndef __has_include |
| 17 | #define __has_include(inc) 0 |
| 18 | #endif |
| 19 | |
Marshall Clow | dde4bfe | 2013-03-18 17:45:34 +0000 | [diff] [blame] | 20 | #ifdef __APPLE__ |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 21 | #include <cxxabi.h> |
Howard Hinnant | 4ad4eee | 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 |
| 25 | // shared libray. The global holding the current new handler is |
| 26 | // in the ABI library and named __cxa_new_handler. |
| 27 | #define __new_handler __cxxabiapple::__cxa_new_handler |
| 28 | #endif |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 29 | #else // __APPLE__ |
Howard Hinnant | a4cabeb | 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>) |
| 33 | #ifndef _LIBCPPABI_VERSION |
| 34 | static std::new_handler __new_handler; |
| 35 | #endif // _LIBCPPABI_VERSION |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 38 | // Implement all new and delete operators as weak definitions |
| 39 | // in this shared library, so that they can be overriden by programs |
| 40 | // that define non-weak copies of the functions. |
| 41 | |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 42 | __attribute__((__weak__, __visibility__("default"))) |
| 43 | void * |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 44 | operator new(std::size_t size) |
Howard Hinnant | fac2ab7 | 2011-07-14 01:34:46 +0000 | [diff] [blame] | 45 | #if !__has_feature(cxx_noexcept) |
| 46 | throw(std::bad_alloc) |
| 47 | #endif |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 48 | { |
| 49 | if (size == 0) |
| 50 | size = 1; |
| 51 | void* p; |
| 52 | while ((p = ::malloc(size)) == 0) |
| 53 | { |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 54 | // If malloc fails and there is a new_handler, |
| 55 | // call it to try free up memory. |
Howard Hinnant | 8c65b45 | 2010-12-04 19:56:43 +0000 | [diff] [blame] | 56 | std::new_handler nh = std::get_new_handler(); |
Howard Hinnant | f64dfce | 2010-12-04 19:54:11 +0000 | [diff] [blame] | 57 | if (nh) |
| 58 | nh(); |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 59 | else |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 60 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 61 | throw std::bad_alloc(); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 62 | #else |
| 63 | break; |
| 64 | #endif |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 65 | } |
| 66 | return p; |
| 67 | } |
| 68 | |
| 69 | __attribute__((__weak__, __visibility__("default"))) |
| 70 | void* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 71 | operator new(size_t size, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 72 | { |
| 73 | void* p = 0; |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 74 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 75 | try |
| 76 | { |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 77 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 78 | p = ::operator new(size); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 79 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 80 | } |
| 81 | catch (...) |
| 82 | { |
| 83 | } |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 84 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 85 | return p; |
| 86 | } |
| 87 | |
| 88 | __attribute__((__weak__, __visibility__("default"))) |
| 89 | void* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 90 | operator new[](size_t size) |
Howard Hinnant | fac2ab7 | 2011-07-14 01:34:46 +0000 | [diff] [blame] | 91 | #if !__has_feature(cxx_noexcept) |
| 92 | throw(std::bad_alloc) |
| 93 | #endif |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 94 | { |
| 95 | return ::operator new(size); |
| 96 | } |
| 97 | |
| 98 | __attribute__((__weak__, __visibility__("default"))) |
| 99 | void* |
Howard Hinnant | 28b2488 | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 100 | operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 101 | { |
| 102 | void* p = 0; |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 103 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 104 | try |
| 105 | { |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 106 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 107 | p = ::operator new[](size); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 108 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 109 | } |
| 110 | catch (...) |
| 111 | { |
| 112 | } |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 113 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 114 | return p; |
| 115 | } |
| 116 | |
| 117 | __attribute__((__weak__, __visibility__("default"))) |
| 118 | void |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 119 | operator delete(void* ptr) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 120 | { |
| 121 | if (ptr) |
| 122 | ::free(ptr); |
| 123 | } |
| 124 | |
| 125 | __attribute__((__weak__, __visibility__("default"))) |
| 126 | void |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 127 | operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 128 | { |
| 129 | ::operator delete(ptr); |
| 130 | } |
| 131 | |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 132 | __attribute__((__weak__, __visibility__("default"))) |
| 133 | void |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 134 | operator delete[] (void* ptr) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 135 | { |
| 136 | ::operator delete (ptr); |
| 137 | } |
| 138 | |
| 139 | __attribute__((__weak__, __visibility__("default"))) |
| 140 | void |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 141 | operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 142 | { |
| 143 | ::operator delete[](ptr); |
| 144 | } |
| 145 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 146 | namespace std |
| 147 | { |
| 148 | |
Nick Kledzik | 09163cc | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 149 | const nothrow_t nothrow = {}; |
| 150 | |
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 151 | #ifndef _LIBCPPABI_VERSION |
| 152 | |
Nick Kledzik | 09163cc | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 153 | new_handler |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 154 | set_new_handler(new_handler handler) _NOEXCEPT |
Nick Kledzik | 09163cc | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 155 | { |
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 156 | return __sync_lock_test_and_set(&__new_handler, handler); |
| 157 | } |
| 158 | |
| 159 | new_handler |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 160 | get_new_handler() _NOEXCEPT |
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 161 | { |
| 162 | return __sync_fetch_and_add(&__new_handler, (new_handler)0); |
Nick Kledzik | 09163cc | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 163 | } |
| 164 | |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 165 | #ifndef LIBCXXRT |
| 166 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 167 | bad_alloc::bad_alloc() _NOEXCEPT |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 168 | { |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 171 | bad_alloc::~bad_alloc() _NOEXCEPT |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 172 | { |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 175 | const char* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 176 | bad_alloc::what() const _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 177 | { |
| 178 | return "std::bad_alloc"; |
| 179 | } |
| 180 | |
David Chisnall | ba252b8 | 2012-03-14 14:11:13 +0000 | [diff] [blame] | 181 | #endif //LIBCXXRT |
| 182 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 183 | bad_array_new_length::bad_array_new_length() _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 184 | { |
| 185 | } |
| 186 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 187 | bad_array_new_length::~bad_array_new_length() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | { |
| 189 | } |
| 190 | |
| 191 | const char* |
Marshall Clow | 3bf7713 | 2013-09-11 01:38:42 +0000 | [diff] [blame^] | 192 | bad_array_length::what() const _NOEXCEPT |
| 193 | { |
| 194 | return "bad_array_length"; |
| 195 | } |
| 196 | |
| 197 | bad_array_length::bad_array_length() _NOEXCEPT |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | bad_array_length::~bad_array_length() _NOEXCEPT |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | const char* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 206 | bad_array_new_length::what() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 207 | { |
| 208 | return "bad_array_new_length"; |
| 209 | } |
| 210 | |
Marshall Clow | 3bf7713 | 2013-09-11 01:38:42 +0000 | [diff] [blame^] | 211 | #endif // _LIBCPPABI_VERSION |
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 212 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 213 | void |
| 214 | __throw_bad_alloc() |
| 215 | { |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 216 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 217 | throw bad_alloc(); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 218 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | } // std |