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