1 //===--------------------------- new.cpp ----------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include <stdlib.h> 11 12 #include "new" 13 14 #if __APPLE__ 15 #include <cxxabi.h> 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 23 #else // __APPLE__ 24 static std::new_handler __new_handler; 25 #endif 26 27 #ifndef _LIBCPPABI_VERSION 28 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 33 __attribute__((__weak__, __visibility__("default"))) 34 void * 35 operator new(std::size_t size) 36 #if !__has_feature(cxx_noexcept) 37 throw(std::bad_alloc) 38 #endif 39 { 40 if (size == 0) 41 size = 1; 42 void* p; 43 while ((p = ::malloc(size)) == 0) 44 { 45 // If malloc fails and there is a new_handler, 46 // call it to try free up memory. 47 std::new_handler nh = std::get_new_handler(); 48 if (nh) 49 nh(); 50 else 51 #ifndef _LIBCPP_NO_EXCEPTIONS 52 throw std::bad_alloc(); 53 #else 54 break; 55 #endif 56 } 57 return p; 58 } 59 60 __attribute__((__weak__, __visibility__("default"))) 61 void* 62 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT 63 { 64 void* p = 0; 65 #ifndef _LIBCPP_NO_EXCEPTIONS 66 try 67 { 68 #endif // _LIBCPP_NO_EXCEPTIONS 69 p = ::operator new(size); 70 #ifndef _LIBCPP_NO_EXCEPTIONS 71 } 72 catch (...) 73 { 74 } 75 #endif // _LIBCPP_NO_EXCEPTIONS 76 return p; 77 } 78 79 __attribute__((__weak__, __visibility__("default"))) 80 void* 81 operator new[](size_t size) 82 #if !__has_feature(cxx_noexcept) 83 throw(std::bad_alloc) 84 #endif 85 { 86 return ::operator new(size); 87 } 88 89 __attribute__((__weak__, __visibility__("default"))) 90 void* 91 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT 92 { 93 void* p = 0; 94 #ifndef _LIBCPP_NO_EXCEPTIONS 95 try 96 { 97 #endif // _LIBCPP_NO_EXCEPTIONS 98 p = ::operator new[](size); 99 #ifndef _LIBCPP_NO_EXCEPTIONS 100 } 101 catch (...) 102 { 103 } 104 #endif // _LIBCPP_NO_EXCEPTIONS 105 return p; 106 } 107 108 __attribute__((__weak__, __visibility__("default"))) 109 void 110 operator delete(void* ptr) _NOEXCEPT 111 { 112 if (ptr) 113 ::free(ptr); 114 } 115 116 __attribute__((__weak__, __visibility__("default"))) 117 void 118 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT 119 { 120 ::operator delete(ptr); 121 } 122 123 __attribute__((__weak__, __visibility__("default"))) 124 void 125 operator delete[] (void* ptr) _NOEXCEPT 126 { 127 ::operator delete (ptr); 128 } 129 130 __attribute__((__weak__, __visibility__("default"))) 131 void 132 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT 133 { 134 ::operator delete[](ptr); 135 } 136 137 #endif 138 139 namespace std 140 { 141 142 const nothrow_t nothrow = {}; 143 144 #ifndef _LIBCPPABI_VERSION 145 146 new_handler 147 set_new_handler(new_handler handler) _NOEXCEPT 148 { 149 return __sync_lock_test_and_set(&__new_handler, handler); 150 } 151 152 new_handler 153 get_new_handler() _NOEXCEPT 154 { 155 return __sync_fetch_and_add(&__new_handler, (new_handler)0); 156 } 157 158 bad_alloc::bad_alloc() _NOEXCEPT 159 { 160 } 161 162 bad_alloc::~bad_alloc() _NOEXCEPT 163 { 164 } 165 166 const char* 167 bad_alloc::what() const _NOEXCEPT 168 { 169 return "std::bad_alloc"; 170 } 171 172 bad_array_new_length::bad_array_new_length() _NOEXCEPT 173 { 174 } 175 176 bad_array_new_length::~bad_array_new_length() _NOEXCEPT 177 { 178 } 179 180 const char* 181 bad_array_new_length::what() const _NOEXCEPT 182 { 183 return "bad_array_new_length"; 184 } 185 186 #endif 187 188 void 189 __throw_bad_alloc() 190 { 191 #ifndef _LIBCPP_NO_EXCEPTIONS 192 throw bad_alloc(); 193 #endif 194 } 195 196 } // std 197