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 // On Darwin, there are two STL shared libraries and a lower level ABI 17 // shared libray. The global holding the current new handler is 18 // in the ABI library and named __cxa_new_handler. 19 #define __new_handler __cxxabiapple::__cxa_new_handler 20 #else // __APPLE__ 21 static std::new_handler __new_handler; 22 #endif 23 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 28 __attribute__((__weak__, __visibility__("default"))) 29 void * 30 operator new(std::size_t size) throw (std::bad_alloc) 31 { 32 if (size == 0) 33 size = 1; 34 void* p; 35 while ((p = ::malloc(size)) == 0) 36 { 37 // If malloc fails and there is a new_handler, 38 // call it to try free up memory. 39 std::new_handler nh = std::get_new_handler(); 40 if (nh) 41 nh(); 42 else 43 #ifndef _LIBCPP_NO_EXCEPTIONS 44 throw std::bad_alloc(); 45 #else 46 break; 47 #endif 48 } 49 return p; 50 } 51 52 __attribute__((__weak__, __visibility__("default"))) 53 void* 54 operator new(size_t size, const std::nothrow_t&) throw() 55 { 56 void* p = 0; 57 #ifndef _LIBCPP_NO_EXCEPTIONS 58 try 59 { 60 #endif // _LIBCPP_NO_EXCEPTIONS 61 p = ::operator new(size); 62 #ifndef _LIBCPP_NO_EXCEPTIONS 63 } 64 catch (...) 65 { 66 } 67 #endif // _LIBCPP_NO_EXCEPTIONS 68 return p; 69 } 70 71 __attribute__((__weak__, __visibility__("default"))) 72 void* 73 operator new[](size_t size) throw (std::bad_alloc) 74 { 75 return ::operator new(size); 76 } 77 78 __attribute__((__weak__, __visibility__("default"))) 79 void* 80 operator new[](size_t size, const std::nothrow_t& nothrow) throw() 81 { 82 void* p = 0; 83 #ifndef _LIBCPP_NO_EXCEPTIONS 84 try 85 { 86 #endif // _LIBCPP_NO_EXCEPTIONS 87 p = ::operator new[](size); 88 #ifndef _LIBCPP_NO_EXCEPTIONS 89 } 90 catch (...) 91 { 92 } 93 #endif // _LIBCPP_NO_EXCEPTIONS 94 return p; 95 } 96 97 __attribute__((__weak__, __visibility__("default"))) 98 void 99 operator delete(void* ptr) throw () 100 { 101 if (ptr) 102 ::free(ptr); 103 } 104 105 __attribute__((__weak__, __visibility__("default"))) 106 void 107 operator delete(void* ptr, const std::nothrow_t&) throw () 108 { 109 ::operator delete(ptr); 110 } 111 112 __attribute__((__weak__, __visibility__("default"))) 113 void 114 operator delete[] (void* ptr) throw () 115 { 116 ::operator delete (ptr); 117 } 118 119 __attribute__((__weak__, __visibility__("default"))) 120 void 121 operator delete[] (void* ptr, const std::nothrow_t&) throw () 122 { 123 ::operator delete[](ptr); 124 } 125 126 namespace std 127 { 128 129 const nothrow_t nothrow = {}; 130 131 new_handler 132 set_new_handler(new_handler handler) throw() 133 { 134 return __sync_lock_test_and_set(&__new_handler, handler); 135 } 136 137 new_handler 138 get_new_handler() throw() 139 { 140 return __sync_fetch_and_add(&__new_handler, (new_handler)0); 141 } 142 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 bad_array_new_length::bad_array_new_length() throw() 158 { 159 } 160 161 bad_array_new_length::~bad_array_new_length() throw() 162 { 163 } 164 165 const char* 166 bad_array_new_length::what() const throw() 167 { 168 return "bad_array_new_length"; 169 } 170 171 void 172 __throw_bad_alloc() 173 { 174 #ifndef _LIBCPP_NO_EXCEPTIONS 175 throw bad_alloc(); 176 #endif 177 } 178 179 } // std 180