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 #define _LIBCPP_BUILDING_NEW 11 12 #include <stdlib.h> 13 14 #include "new" 15 16 #if defined(__APPLE__) && !defined(LIBCXXRT) 17 #include <cxxabi.h> 18 19 #ifndef _LIBCPPABI_VERSION 20 // On Darwin, there are two STL shared libraries and a lower level ABI 21 // shared library. The global holding the current new handler is 22 // in the ABI library and named __cxa_new_handler. 23 #define __new_handler __cxxabiapple::__cxa_new_handler 24 #endif 25 #else // __APPLE__ 26 #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI) 27 #include <cxxabi.h> 28 #endif // defined(LIBCXX_BUILDING_LIBCXXABI) 29 #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) 30 static std::new_handler __new_handler; 31 #endif // _LIBCPPABI_VERSION 32 #endif 33 34 #ifndef __GLIBCXX__ 35 36 // Implement all new and delete operators as weak definitions 37 // in this shared library, so that they can be overridden by programs 38 // that define non-weak copies of the functions. 39 40 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 41 void * 42 operator new(std::size_t size) _THROW_BAD_ALLOC 43 { 44 if (size == 0) 45 size = 1; 46 void* p; 47 while ((p = ::malloc(size)) == 0) 48 { 49 // If malloc fails and there is a new_handler, 50 // call it to try free up memory. 51 std::new_handler nh = std::get_new_handler(); 52 if (nh) 53 nh(); 54 else 55 #ifndef _LIBCPP_NO_EXCEPTIONS 56 throw std::bad_alloc(); 57 #else 58 break; 59 #endif 60 } 61 return p; 62 } 63 64 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 65 void * 66 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC 67 { 68 if (size == 0) 69 size = 1; 70 if (static_cast<size_t>(alignment) < sizeof(void*)) 71 alignment = std::align_val_t(sizeof(void*)); 72 void* p; 73 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0) 74 { 75 // If posix_memalign fails and there is a new_handler, 76 // call it to try free up memory. 77 std::new_handler nh = std::get_new_handler(); 78 if (nh) 79 nh(); 80 else { 81 #ifndef _LIBCPP_NO_EXCEPTIONS 82 throw std::bad_alloc(); 83 #else 84 p = nullptr; // posix_memalign doesn't initialize 'p' on failure 85 break; 86 #endif 87 } 88 } 89 return p; 90 } 91 92 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 93 void* 94 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT 95 { 96 void* p = 0; 97 #ifndef _LIBCPP_NO_EXCEPTIONS 98 try 99 { 100 #endif // _LIBCPP_NO_EXCEPTIONS 101 p = ::operator new(size); 102 #ifndef _LIBCPP_NO_EXCEPTIONS 103 } 104 catch (...) 105 { 106 } 107 #endif // _LIBCPP_NO_EXCEPTIONS 108 return p; 109 } 110 111 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 112 void* 113 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 114 { 115 void* p = 0; 116 #ifndef _LIBCPP_NO_EXCEPTIONS 117 try 118 { 119 #endif // _LIBCPP_NO_EXCEPTIONS 120 p = ::operator new(size, alignment); 121 #ifndef _LIBCPP_NO_EXCEPTIONS 122 } 123 catch (...) 124 { 125 } 126 #endif // _LIBCPP_NO_EXCEPTIONS 127 return p; 128 } 129 130 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 131 void* 132 operator new[](size_t size) _THROW_BAD_ALLOC 133 { 134 return ::operator new(size); 135 } 136 137 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 138 void* 139 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC 140 { 141 return ::operator new(size, alignment); 142 } 143 144 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 145 void* 146 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT 147 { 148 void* p = 0; 149 #ifndef _LIBCPP_NO_EXCEPTIONS 150 try 151 { 152 #endif // _LIBCPP_NO_EXCEPTIONS 153 p = ::operator new[](size); 154 #ifndef _LIBCPP_NO_EXCEPTIONS 155 } 156 catch (...) 157 { 158 } 159 #endif // _LIBCPP_NO_EXCEPTIONS 160 return p; 161 } 162 163 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 164 void* 165 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 166 { 167 void* p = 0; 168 #ifndef _LIBCPP_NO_EXCEPTIONS 169 try 170 { 171 #endif // _LIBCPP_NO_EXCEPTIONS 172 p = ::operator new[](size, alignment); 173 #ifndef _LIBCPP_NO_EXCEPTIONS 174 } 175 catch (...) 176 { 177 } 178 #endif // _LIBCPP_NO_EXCEPTIONS 179 return p; 180 } 181 182 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 183 void 184 operator delete(void* ptr) _NOEXCEPT 185 { 186 if (ptr) 187 ::free(ptr); 188 } 189 190 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 191 void 192 operator delete(void* ptr, std::align_val_t) _NOEXCEPT 193 { 194 if (ptr) 195 ::free(ptr); 196 } 197 198 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 199 void 200 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT 201 { 202 ::operator delete(ptr); 203 } 204 205 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 206 void 207 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 208 { 209 ::operator delete(ptr, alignment); 210 } 211 212 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 213 void 214 operator delete(void* ptr, size_t) _NOEXCEPT 215 { 216 ::operator delete(ptr); 217 } 218 219 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 220 void 221 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT 222 { 223 ::operator delete(ptr, alignment); 224 } 225 226 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 227 void 228 operator delete[] (void* ptr) _NOEXCEPT 229 { 230 ::operator delete(ptr); 231 } 232 233 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 234 void 235 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT 236 { 237 ::operator delete(ptr, alignment); 238 } 239 240 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 241 void 242 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT 243 { 244 ::operator delete[](ptr); 245 } 246 247 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 248 void 249 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 250 { 251 ::operator delete[](ptr, alignment); 252 } 253 254 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 255 void 256 operator delete[] (void* ptr, size_t) _NOEXCEPT 257 { 258 ::operator delete[](ptr); 259 } 260 261 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 262 void 263 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT 264 { 265 ::operator delete[](ptr, alignment); 266 } 267 268 #endif // !__GLIBCXX__ 269 270 namespace std 271 { 272 273 #ifndef __GLIBCXX__ 274 const nothrow_t nothrow = {}; 275 #endif 276 277 #ifndef _LIBCPPABI_VERSION 278 279 #ifndef __GLIBCXX__ 280 281 new_handler 282 set_new_handler(new_handler handler) _NOEXCEPT 283 { 284 return __sync_lock_test_and_set(&__new_handler, handler); 285 } 286 287 new_handler 288 get_new_handler() _NOEXCEPT 289 { 290 return __sync_fetch_and_add(&__new_handler, nullptr); 291 } 292 293 #endif // !__GLIBCXX__ 294 295 #ifndef LIBCXXRT 296 297 bad_alloc::bad_alloc() _NOEXCEPT 298 { 299 } 300 301 #ifndef __GLIBCXX__ 302 303 bad_alloc::~bad_alloc() _NOEXCEPT 304 { 305 } 306 307 const char* 308 bad_alloc::what() const _NOEXCEPT 309 { 310 return "std::bad_alloc"; 311 } 312 313 #endif // !__GLIBCXX__ 314 315 bad_array_new_length::bad_array_new_length() _NOEXCEPT 316 { 317 } 318 319 bad_array_new_length::~bad_array_new_length() _NOEXCEPT 320 { 321 } 322 323 const char* 324 bad_array_new_length::what() const _NOEXCEPT 325 { 326 return "bad_array_new_length"; 327 } 328 329 #endif //LIBCXXRT 330 331 const char* 332 bad_array_length::what() const _NOEXCEPT 333 { 334 return "bad_array_length"; 335 } 336 337 bad_array_length::bad_array_length() _NOEXCEPT 338 { 339 } 340 341 bad_array_length::~bad_array_length() _NOEXCEPT 342 { 343 } 344 345 #endif // _LIBCPPABI_VERSION 346 347 #ifndef LIBSTDCXX 348 349 void 350 __throw_bad_alloc() 351 { 352 #ifndef _LIBCPP_NO_EXCEPTIONS 353 throw bad_alloc(); 354 #else 355 _VSTD::abort(); 356 #endif 357 } 358 359 #endif // !LIBSTDCXX 360 361 } // std 362