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