13e519524SHoward Hinnant //===--------------------------- new.cpp ----------------------------------===// 23e519524SHoward Hinnant // 35b08a8a4SHoward Hinnant // The LLVM Compiler Infrastructure 43e519524SHoward Hinnant // 5412dbebeSHoward Hinnant // This file is dual licensed under the MIT and the University of Illinois Open 6412dbebeSHoward Hinnant // Source Licenses. See LICENSE.TXT for details. 73e519524SHoward Hinnant // 83e519524SHoward Hinnant //===----------------------------------------------------------------------===// 93e519524SHoward Hinnant 106e8ebb2cSMarshall Clow #define _LIBCPP_BUILDING_NEW 116e8ebb2cSMarshall Clow 12111e0cbeSNick Kledzik #include <stdlib.h> 13111e0cbeSNick Kledzik 143e519524SHoward Hinnant #include "new" 153e519524SHoward Hinnant 1622b131ffSEric Fiselier #if defined(__APPLE__) && !defined(LIBCXXRT) 17128ba719SHoward Hinnant #include <cxxabi.h> 182c9c6a57SHoward Hinnant 192c9c6a57SHoward Hinnant #ifndef _LIBCPPABI_VERSION 20111e0cbeSNick Kledzik // On Darwin, there are two STL shared libraries and a lower level ABI 21c1cf9818SMarshall Clow // shared library. The global holding the current new handler is 22111e0cbeSNick Kledzik // in the ABI library and named __cxa_new_handler. 23111e0cbeSNick Kledzik #define __new_handler __cxxabiapple::__cxa_new_handler 242c9c6a57SHoward Hinnant #endif 25940e211cSHoward Hinnant #else // __APPLE__ 26929df471SBenjamin Kramer #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI) 27dce0696aSHoward Hinnant #include <cxxabi.h> 28929df471SBenjamin Kramer #endif // defined(LIBCXX_BUILDING_LIBCXXABI) 29926aa5f4SPeter Collingbourne #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) 30111e0cbeSNick Kledzik static std::new_handler __new_handler; 31dce0696aSHoward Hinnant #endif // _LIBCPPABI_VERSION 32111e0cbeSNick Kledzik #endif 33111e0cbeSNick Kledzik 34926aa5f4SPeter Collingbourne #ifndef __GLIBCXX__ 35926aa5f4SPeter Collingbourne 36111e0cbeSNick Kledzik // Implement all new and delete operators as weak definitions 3710fe4209SEric Fiselier // in this shared library, so that they can be overridden by programs 38111e0cbeSNick Kledzik // that define non-weak copies of the functions. 39111e0cbeSNick Kledzik 4032f9a776SHoward Hinnant _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 41111e0cbeSNick Kledzik void * 42*0ae4f23fSEric Fiselier operator new(std::size_t size) _THROW_BAD_ALLOC 43111e0cbeSNick Kledzik { 44111e0cbeSNick Kledzik if (size == 0) 45111e0cbeSNick Kledzik size = 1; 46111e0cbeSNick Kledzik void* p; 47111e0cbeSNick Kledzik while ((p = ::malloc(size)) == 0) 48111e0cbeSNick Kledzik { 49111e0cbeSNick Kledzik // If malloc fails and there is a new_handler, 50111e0cbeSNick Kledzik // call it to try free up memory. 5175357bcdSHoward Hinnant std::new_handler nh = std::get_new_handler(); 52816cb897SHoward Hinnant if (nh) 53816cb897SHoward Hinnant nh(); 54111e0cbeSNick Kledzik else 5554b409fdSHoward Hinnant #ifndef _LIBCPP_NO_EXCEPTIONS 56111e0cbeSNick Kledzik throw std::bad_alloc(); 5754b409fdSHoward Hinnant #else 5854b409fdSHoward Hinnant break; 5954b409fdSHoward Hinnant #endif 60111e0cbeSNick Kledzik } 61111e0cbeSNick Kledzik return p; 62111e0cbeSNick Kledzik } 63111e0cbeSNick Kledzik 6432f9a776SHoward Hinnant _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 65111e0cbeSNick Kledzik void * 66*0ae4f23fSEric Fiselier operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC 67*0ae4f23fSEric Fiselier { 68*0ae4f23fSEric Fiselier if (size == 0) 69*0ae4f23fSEric Fiselier size = 1; 70*0ae4f23fSEric Fiselier if (static_cast<size_t>(alignment) < sizeof(void*)) 71*0ae4f23fSEric Fiselier alignment = std::align_val_t(sizeof(void*)); 72*0ae4f23fSEric Fiselier void* p; 73*0ae4f23fSEric Fiselier while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0) 74*0ae4f23fSEric Fiselier { 75*0ae4f23fSEric Fiselier // If posix_memalign fails and there is a new_handler, 76*0ae4f23fSEric Fiselier // call it to try free up memory. 77*0ae4f23fSEric Fiselier std::new_handler nh = std::get_new_handler(); 78*0ae4f23fSEric Fiselier if (nh) 79*0ae4f23fSEric Fiselier nh(); 80*0ae4f23fSEric Fiselier else { 81*0ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS 82*0ae4f23fSEric Fiselier throw std::bad_alloc(); 83*0ae4f23fSEric Fiselier #else 84*0ae4f23fSEric Fiselier p = nullptr; // posix_memalign doesn't initialize 'p' on failure 85*0ae4f23fSEric Fiselier break; 86*0ae4f23fSEric Fiselier #endif 87*0ae4f23fSEric Fiselier } 88*0ae4f23fSEric Fiselier } 89*0ae4f23fSEric Fiselier return p; 90*0ae4f23fSEric Fiselier } 91*0ae4f23fSEric Fiselier 92*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 93*0ae4f23fSEric Fiselier void* 94fafca58cSHoward Hinnant operator new(size_t size, const std::nothrow_t&) _NOEXCEPT 95111e0cbeSNick Kledzik { 96111e0cbeSNick Kledzik void* p = 0; 9754b409fdSHoward Hinnant #ifndef _LIBCPP_NO_EXCEPTIONS 98111e0cbeSNick Kledzik try 99111e0cbeSNick Kledzik { 100940e211cSHoward Hinnant #endif // _LIBCPP_NO_EXCEPTIONS 101111e0cbeSNick Kledzik p = ::operator new(size); 10254b409fdSHoward Hinnant #ifndef _LIBCPP_NO_EXCEPTIONS 103111e0cbeSNick Kledzik } 104111e0cbeSNick Kledzik catch (...) 105111e0cbeSNick Kledzik { 106111e0cbeSNick Kledzik } 107940e211cSHoward Hinnant #endif // _LIBCPP_NO_EXCEPTIONS 108111e0cbeSNick Kledzik return p; 109111e0cbeSNick Kledzik } 110111e0cbeSNick Kledzik 11132f9a776SHoward Hinnant _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 112111e0cbeSNick Kledzik void* 113*0ae4f23fSEric Fiselier operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 114*0ae4f23fSEric Fiselier { 115*0ae4f23fSEric Fiselier void* p = 0; 116*0ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS 117*0ae4f23fSEric Fiselier try 118*0ae4f23fSEric Fiselier { 119*0ae4f23fSEric Fiselier #endif // _LIBCPP_NO_EXCEPTIONS 120*0ae4f23fSEric Fiselier p = ::operator new(size, alignment); 121*0ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS 122*0ae4f23fSEric Fiselier } 123*0ae4f23fSEric Fiselier catch (...) 124*0ae4f23fSEric Fiselier { 125*0ae4f23fSEric Fiselier } 126*0ae4f23fSEric Fiselier #endif // _LIBCPP_NO_EXCEPTIONS 127*0ae4f23fSEric Fiselier return p; 128*0ae4f23fSEric Fiselier } 129*0ae4f23fSEric Fiselier 130*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 131*0ae4f23fSEric Fiselier void* 132*0ae4f23fSEric Fiselier operator new[](size_t size) _THROW_BAD_ALLOC 133111e0cbeSNick Kledzik { 134111e0cbeSNick Kledzik return ::operator new(size); 135111e0cbeSNick Kledzik } 136111e0cbeSNick Kledzik 13732f9a776SHoward Hinnant _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 138111e0cbeSNick Kledzik void* 139*0ae4f23fSEric Fiselier operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC 140*0ae4f23fSEric Fiselier { 141*0ae4f23fSEric Fiselier return ::operator new(size, alignment); 142*0ae4f23fSEric Fiselier } 143*0ae4f23fSEric Fiselier 144*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 145*0ae4f23fSEric Fiselier void* 146c206366fSHoward Hinnant operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT 147111e0cbeSNick Kledzik { 148111e0cbeSNick Kledzik void* p = 0; 14954b409fdSHoward Hinnant #ifndef _LIBCPP_NO_EXCEPTIONS 150111e0cbeSNick Kledzik try 151111e0cbeSNick Kledzik { 152940e211cSHoward Hinnant #endif // _LIBCPP_NO_EXCEPTIONS 153111e0cbeSNick Kledzik p = ::operator new[](size); 15454b409fdSHoward Hinnant #ifndef _LIBCPP_NO_EXCEPTIONS 155111e0cbeSNick Kledzik } 156111e0cbeSNick Kledzik catch (...) 157111e0cbeSNick Kledzik { 158111e0cbeSNick Kledzik } 159940e211cSHoward Hinnant #endif // _LIBCPP_NO_EXCEPTIONS 160111e0cbeSNick Kledzik return p; 161111e0cbeSNick Kledzik } 162111e0cbeSNick Kledzik 16332f9a776SHoward Hinnant _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 164*0ae4f23fSEric Fiselier void* 165*0ae4f23fSEric Fiselier operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 166*0ae4f23fSEric Fiselier { 167*0ae4f23fSEric Fiselier void* p = 0; 168*0ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS 169*0ae4f23fSEric Fiselier try 170*0ae4f23fSEric Fiselier { 171*0ae4f23fSEric Fiselier #endif // _LIBCPP_NO_EXCEPTIONS 172*0ae4f23fSEric Fiselier p = ::operator new[](size, alignment); 173*0ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS 174*0ae4f23fSEric Fiselier } 175*0ae4f23fSEric Fiselier catch (...) 176*0ae4f23fSEric Fiselier { 177*0ae4f23fSEric Fiselier } 178*0ae4f23fSEric Fiselier #endif // _LIBCPP_NO_EXCEPTIONS 179*0ae4f23fSEric Fiselier return p; 180*0ae4f23fSEric Fiselier } 181*0ae4f23fSEric Fiselier 182*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 183111e0cbeSNick Kledzik void 184fafca58cSHoward Hinnant operator delete(void* ptr) _NOEXCEPT 185111e0cbeSNick Kledzik { 186111e0cbeSNick Kledzik if (ptr) 187111e0cbeSNick Kledzik ::free(ptr); 188111e0cbeSNick Kledzik } 189111e0cbeSNick Kledzik 19032f9a776SHoward Hinnant _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 191111e0cbeSNick Kledzik void 192*0ae4f23fSEric Fiselier operator delete(void* ptr, std::align_val_t) _NOEXCEPT 193*0ae4f23fSEric Fiselier { 194*0ae4f23fSEric Fiselier if (ptr) 195*0ae4f23fSEric Fiselier ::free(ptr); 196*0ae4f23fSEric Fiselier } 197*0ae4f23fSEric Fiselier 198*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 199*0ae4f23fSEric Fiselier void 200fafca58cSHoward Hinnant operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT 201111e0cbeSNick Kledzik { 202111e0cbeSNick Kledzik ::operator delete(ptr); 203111e0cbeSNick Kledzik } 204111e0cbeSNick Kledzik 20532f9a776SHoward Hinnant _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 206111e0cbeSNick Kledzik void 207*0ae4f23fSEric Fiselier operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 208*0ae4f23fSEric Fiselier { 209*0ae4f23fSEric Fiselier ::operator delete(ptr, alignment); 210*0ae4f23fSEric Fiselier } 211*0ae4f23fSEric Fiselier 212*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 213*0ae4f23fSEric Fiselier void 214e4864e0bSLarisse Voufo operator delete(void* ptr, size_t) _NOEXCEPT 215d36bd700SLarisse Voufo { 216d36bd700SLarisse Voufo ::operator delete(ptr); 217d36bd700SLarisse Voufo } 218d36bd700SLarisse Voufo 219d36bd700SLarisse Voufo _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 220d36bd700SLarisse Voufo void 221*0ae4f23fSEric Fiselier operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT 222*0ae4f23fSEric Fiselier { 223*0ae4f23fSEric Fiselier ::operator delete(ptr, alignment); 224*0ae4f23fSEric Fiselier } 225*0ae4f23fSEric Fiselier 226*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 227*0ae4f23fSEric Fiselier void 228fafca58cSHoward Hinnant operator delete[] (void* ptr) _NOEXCEPT 229111e0cbeSNick Kledzik { 230111e0cbeSNick Kledzik ::operator delete(ptr); 231111e0cbeSNick Kledzik } 232111e0cbeSNick Kledzik 23332f9a776SHoward Hinnant _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 234111e0cbeSNick Kledzik void 235*0ae4f23fSEric Fiselier operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT 236*0ae4f23fSEric Fiselier { 237*0ae4f23fSEric Fiselier ::operator delete(ptr, alignment); 238*0ae4f23fSEric Fiselier } 239*0ae4f23fSEric Fiselier 240*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 241*0ae4f23fSEric Fiselier void 242fafca58cSHoward Hinnant operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT 243111e0cbeSNick Kledzik { 244111e0cbeSNick Kledzik ::operator delete[](ptr); 245111e0cbeSNick Kledzik } 246111e0cbeSNick Kledzik 247d36bd700SLarisse Voufo _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 248d36bd700SLarisse Voufo void 249*0ae4f23fSEric Fiselier operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 250*0ae4f23fSEric Fiselier { 251*0ae4f23fSEric Fiselier ::operator delete[](ptr, alignment); 252*0ae4f23fSEric Fiselier } 253*0ae4f23fSEric Fiselier 254*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 255*0ae4f23fSEric Fiselier void 256e4864e0bSLarisse Voufo operator delete[] (void* ptr, size_t) _NOEXCEPT 257d36bd700SLarisse Voufo { 258d36bd700SLarisse Voufo ::operator delete[](ptr); 259d36bd700SLarisse Voufo } 260d36bd700SLarisse Voufo 261*0ae4f23fSEric Fiselier _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS 262*0ae4f23fSEric Fiselier void 263*0ae4f23fSEric Fiselier operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT 264*0ae4f23fSEric Fiselier { 265*0ae4f23fSEric Fiselier ::operator delete[](ptr, alignment); 266*0ae4f23fSEric Fiselier } 267*0ae4f23fSEric Fiselier 268926aa5f4SPeter Collingbourne #endif // !__GLIBCXX__ 269926aa5f4SPeter Collingbourne 2703e519524SHoward Hinnant namespace std 2713e519524SHoward Hinnant { 2723e519524SHoward Hinnant 273926aa5f4SPeter Collingbourne #ifndef __GLIBCXX__ 27462713dacSNick Kledzik const nothrow_t nothrow = {}; 275926aa5f4SPeter Collingbourne #endif 27662713dacSNick Kledzik 2772c9c6a57SHoward Hinnant #ifndef _LIBCPPABI_VERSION 2782c9c6a57SHoward Hinnant 279926aa5f4SPeter Collingbourne #ifndef __GLIBCXX__ 280926aa5f4SPeter Collingbourne 28162713dacSNick Kledzik new_handler 282fafca58cSHoward Hinnant set_new_handler(new_handler handler) _NOEXCEPT 28362713dacSNick Kledzik { 2841596c453SHoward Hinnant return __sync_lock_test_and_set(&__new_handler, handler); 2851596c453SHoward Hinnant } 2861596c453SHoward Hinnant 2871596c453SHoward Hinnant new_handler 288fafca58cSHoward Hinnant get_new_handler() _NOEXCEPT 2891596c453SHoward Hinnant { 290634b9dd7SJoerg Sonnenberger return __sync_fetch_and_add(&__new_handler, nullptr); 29162713dacSNick Kledzik } 29262713dacSNick Kledzik 293926aa5f4SPeter Collingbourne #endif // !__GLIBCXX__ 294926aa5f4SPeter Collingbourne 295e1da544eSDavid Chisnall #ifndef LIBCXXRT 296e1da544eSDavid Chisnall 297fafca58cSHoward Hinnant bad_alloc::bad_alloc() _NOEXCEPT 298111e0cbeSNick Kledzik { 299111e0cbeSNick Kledzik } 300111e0cbeSNick Kledzik 301926aa5f4SPeter Collingbourne #ifndef __GLIBCXX__ 302926aa5f4SPeter Collingbourne 303fafca58cSHoward Hinnant bad_alloc::~bad_alloc() _NOEXCEPT 304111e0cbeSNick Kledzik { 305111e0cbeSNick Kledzik } 306111e0cbeSNick Kledzik 307111e0cbeSNick Kledzik const char* 308fafca58cSHoward Hinnant bad_alloc::what() const _NOEXCEPT 309111e0cbeSNick Kledzik { 310111e0cbeSNick Kledzik return "std::bad_alloc"; 311111e0cbeSNick Kledzik } 312111e0cbeSNick Kledzik 313926aa5f4SPeter Collingbourne #endif // !__GLIBCXX__ 314926aa5f4SPeter Collingbourne 315fafca58cSHoward Hinnant bad_array_new_length::bad_array_new_length() _NOEXCEPT 316111e0cbeSNick Kledzik { 317111e0cbeSNick Kledzik } 318111e0cbeSNick Kledzik 319fafca58cSHoward Hinnant bad_array_new_length::~bad_array_new_length() _NOEXCEPT 3203e519524SHoward Hinnant { 3213e519524SHoward Hinnant } 3223e519524SHoward Hinnant 3231d558b84SEric Fiselier const char* 3241d558b84SEric Fiselier bad_array_new_length::what() const _NOEXCEPT 3251d558b84SEric Fiselier { 3261d558b84SEric Fiselier return "bad_array_new_length"; 3271d558b84SEric Fiselier } 3281d558b84SEric Fiselier 329fd465fe5SEric Fiselier #endif //LIBCXXRT 330fd465fe5SEric Fiselier 3313e519524SHoward Hinnant const char* 3326e8ebb2cSMarshall Clow bad_array_length::what() const _NOEXCEPT 3336e8ebb2cSMarshall Clow { 3346e8ebb2cSMarshall Clow return "bad_array_length"; 3356e8ebb2cSMarshall Clow } 3366e8ebb2cSMarshall Clow 3376e8ebb2cSMarshall Clow bad_array_length::bad_array_length() _NOEXCEPT 3386e8ebb2cSMarshall Clow { 3396e8ebb2cSMarshall Clow } 3406e8ebb2cSMarshall Clow 3416e8ebb2cSMarshall Clow bad_array_length::~bad_array_length() _NOEXCEPT 3426e8ebb2cSMarshall Clow { 3436e8ebb2cSMarshall Clow } 3446e8ebb2cSMarshall Clow 3456e8ebb2cSMarshall Clow #endif // _LIBCPPABI_VERSION 3462c9c6a57SHoward Hinnant 34726dd09e5SPeter Collingbourne #ifndef LIBSTDCXX 34826dd09e5SPeter Collingbourne 3493e519524SHoward Hinnant void 3503e519524SHoward Hinnant __throw_bad_alloc() 3513e519524SHoward Hinnant { 35254b409fdSHoward Hinnant #ifndef _LIBCPP_NO_EXCEPTIONS 3533e519524SHoward Hinnant throw bad_alloc(); 354d437fa5cSMarshall Clow #else 355d437fa5cSMarshall Clow _VSTD::abort(); 35654b409fdSHoward Hinnant #endif 3573e519524SHoward Hinnant } 3583e519524SHoward Hinnant 35926dd09e5SPeter Collingbourne #endif // !LIBSTDCXX 36026dd09e5SPeter Collingbourne 3613e519524SHoward Hinnant } // std 362