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 |
| 45 | throw std::bad_alloc(); |
| 46 | } |
| 47 | return p; |
| 48 | } |
| 49 | |
| 50 | __attribute__((__weak__, __visibility__("default"))) |
| 51 | void* |
| 52 | operator new(size_t size, const std::nothrow_t&) throw() |
| 53 | { |
| 54 | void* p = 0; |
| 55 | try |
| 56 | { |
| 57 | p = ::operator new(size); |
| 58 | } |
| 59 | catch (...) |
| 60 | { |
| 61 | } |
| 62 | return p; |
| 63 | } |
| 64 | |
| 65 | __attribute__((__weak__, __visibility__("default"))) |
| 66 | void* |
| 67 | operator new[](size_t size) throw (std::bad_alloc) |
| 68 | { |
| 69 | return ::operator new(size); |
| 70 | } |
| 71 | |
| 72 | __attribute__((__weak__, __visibility__("default"))) |
| 73 | void* |
| 74 | operator new[](size_t size, const std::nothrow_t& nothrow) throw() |
| 75 | { |
| 76 | void* p = 0; |
| 77 | try |
| 78 | { |
| 79 | p = ::operator new[](size); |
| 80 | } |
| 81 | catch (...) |
| 82 | { |
| 83 | } |
| 84 | return p; |
| 85 | } |
| 86 | |
| 87 | __attribute__((__weak__, __visibility__("default"))) |
| 88 | void |
| 89 | operator delete(void* ptr) throw () |
| 90 | { |
| 91 | if (ptr) |
| 92 | ::free(ptr); |
| 93 | } |
| 94 | |
| 95 | __attribute__((__weak__, __visibility__("default"))) |
| 96 | void |
| 97 | operator delete(void* ptr, const std::nothrow_t&) throw () |
| 98 | { |
| 99 | ::operator delete(ptr); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | __attribute__((__weak__, __visibility__("default"))) |
| 104 | void |
| 105 | operator delete[] (void* ptr) throw () |
| 106 | { |
| 107 | ::operator delete (ptr); |
| 108 | } |
| 109 | |
| 110 | __attribute__((__weak__, __visibility__("default"))) |
| 111 | void |
| 112 | operator delete[] (void* ptr, const std::nothrow_t&) throw () |
| 113 | { |
| 114 | ::operator delete[](ptr); |
| 115 | } |
| 116 | |
| 117 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 118 | namespace std |
| 119 | { |
| 120 | |
Nick Kledzik | 09163cc | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 121 | const nothrow_t nothrow = {}; |
| 122 | |
| 123 | new_handler |
| 124 | set_new_handler(new_handler handler) throw() |
| 125 | { |
| 126 | new_handler r = __new_handler; |
| 127 | __new_handler = handler; |
| 128 | return r; |
| 129 | } |
| 130 | |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 131 | bad_alloc::bad_alloc() throw() |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | bad_alloc::~bad_alloc() throw() |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | const char* |
| 140 | bad_alloc::what() const throw() |
| 141 | { |
| 142 | return "std::bad_alloc"; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | bad_array_new_length::bad_array_new_length() throw() |
| 147 | { |
| 148 | } |
| 149 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 150 | bad_array_new_length::~bad_array_new_length() throw() |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | const char* |
| 155 | bad_array_new_length::what() const throw() |
| 156 | { |
| 157 | return "bad_array_new_length"; |
| 158 | } |
| 159 | |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 160 | |
| 161 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 162 | void |
| 163 | __throw_bad_alloc() |
| 164 | { |
| 165 | throw bad_alloc(); |
| 166 | } |
| 167 | |
| 168 | } // std |