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) |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 31 | { |
| 32 | if (size == 0) |
| 33 | size = 1; |
| 34 | void* p; |
| 35 | while ((p = ::malloc(size)) == 0) |
| 36 | { |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 37 | // If malloc fails and there is a new_handler, |
| 38 | // call it to try free up memory. |
Howard Hinnant | 8c65b45 | 2010-12-04 19:56:43 +0000 | [diff] [blame] | 39 | std::new_handler nh = std::get_new_handler(); |
Howard Hinnant | f64dfce | 2010-12-04 19:54:11 +0000 | [diff] [blame] | 40 | if (nh) |
| 41 | nh(); |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 42 | else |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 43 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 44 | throw std::bad_alloc(); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 45 | #else |
| 46 | break; |
| 47 | #endif |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 48 | } |
| 49 | return p; |
| 50 | } |
| 51 | |
| 52 | __attribute__((__weak__, __visibility__("default"))) |
| 53 | void* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 54 | operator new(size_t size, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 55 | { |
| 56 | void* p = 0; |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 57 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 58 | try |
| 59 | { |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 60 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 61 | p = ::operator new(size); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 62 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 63 | } |
| 64 | catch (...) |
| 65 | { |
| 66 | } |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 67 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 68 | return p; |
| 69 | } |
| 70 | |
| 71 | __attribute__((__weak__, __visibility__("default"))) |
| 72 | void* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 73 | operator new[](size_t size) |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 74 | { |
| 75 | return ::operator new(size); |
| 76 | } |
| 77 | |
| 78 | __attribute__((__weak__, __visibility__("default"))) |
| 79 | void* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 80 | operator new[](size_t size, const std::nothrow_t& nothrow) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 81 | { |
| 82 | void* p = 0; |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 83 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 84 | try |
| 85 | { |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 86 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 87 | p = ::operator new[](size); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 88 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 89 | } |
| 90 | catch (...) |
| 91 | { |
| 92 | } |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 93 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 94 | return p; |
| 95 | } |
| 96 | |
| 97 | __attribute__((__weak__, __visibility__("default"))) |
| 98 | void |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 99 | operator delete(void* ptr) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 100 | { |
| 101 | if (ptr) |
| 102 | ::free(ptr); |
| 103 | } |
| 104 | |
| 105 | __attribute__((__weak__, __visibility__("default"))) |
| 106 | void |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 107 | operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 108 | { |
| 109 | ::operator delete(ptr); |
| 110 | } |
| 111 | |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 112 | __attribute__((__weak__, __visibility__("default"))) |
| 113 | void |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 114 | operator delete[] (void* ptr) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 115 | { |
| 116 | ::operator delete (ptr); |
| 117 | } |
| 118 | |
| 119 | __attribute__((__weak__, __visibility__("default"))) |
| 120 | void |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 121 | operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 122 | { |
| 123 | ::operator delete[](ptr); |
| 124 | } |
| 125 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 126 | namespace std |
| 127 | { |
| 128 | |
Nick Kledzik | 09163cc | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 129 | const nothrow_t nothrow = {}; |
| 130 | |
| 131 | new_handler |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 132 | set_new_handler(new_handler handler) _NOEXCEPT |
Nick Kledzik | 09163cc | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 133 | { |
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 134 | return __sync_lock_test_and_set(&__new_handler, handler); |
| 135 | } |
| 136 | |
| 137 | new_handler |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 138 | get_new_handler() _NOEXCEPT |
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 139 | { |
| 140 | return __sync_fetch_and_add(&__new_handler, (new_handler)0); |
Nick Kledzik | 09163cc | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 143 | bad_alloc::bad_alloc() _NOEXCEPT |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 144 | { |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 147 | bad_alloc::~bad_alloc() _NOEXCEPT |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 148 | { |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 151 | const char* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 152 | bad_alloc::what() const _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 153 | { |
| 154 | return "std::bad_alloc"; |
| 155 | } |
| 156 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 157 | bad_array_new_length::bad_array_new_length() _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 158 | { |
| 159 | } |
| 160 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 161 | bad_array_new_length::~bad_array_new_length() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 162 | { |
| 163 | } |
| 164 | |
| 165 | const char* |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame^] | 166 | bad_array_new_length::what() const _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 167 | { |
| 168 | return "bad_array_new_length"; |
| 169 | } |
| 170 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 171 | void |
| 172 | __throw_bad_alloc() |
| 173 | { |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 174 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 175 | throw bad_alloc(); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 176 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | } // std |