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