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