xref: /llvm-project-15.0.7/libcxx/src/new.cpp (revision dce4218d)
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 
40*dce4218dSShoaib Meenai _LIBCPP_WEAK
41111e0cbeSNick Kledzik void *
420ae4f23fSEric 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 
64*dce4218dSShoaib Meenai _LIBCPP_WEAK
65111e0cbeSNick Kledzik void *
660ae4f23fSEric Fiselier operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
670ae4f23fSEric Fiselier {
680ae4f23fSEric Fiselier     if (size == 0)
690ae4f23fSEric Fiselier         size = 1;
700ae4f23fSEric Fiselier     if (static_cast<size_t>(alignment) < sizeof(void*))
710ae4f23fSEric Fiselier       alignment = std::align_val_t(sizeof(void*));
720ae4f23fSEric Fiselier     void* p;
730ae4f23fSEric Fiselier     while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
740ae4f23fSEric Fiselier     {
750ae4f23fSEric Fiselier         // If posix_memalign fails and there is a new_handler,
760ae4f23fSEric Fiselier         // call it to try free up memory.
770ae4f23fSEric Fiselier         std::new_handler nh = std::get_new_handler();
780ae4f23fSEric Fiselier         if (nh)
790ae4f23fSEric Fiselier             nh();
800ae4f23fSEric Fiselier         else {
810ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS
820ae4f23fSEric Fiselier             throw std::bad_alloc();
830ae4f23fSEric Fiselier #else
840ae4f23fSEric Fiselier             p = nullptr; // posix_memalign doesn't initialize 'p' on failure
850ae4f23fSEric Fiselier             break;
860ae4f23fSEric Fiselier #endif
870ae4f23fSEric Fiselier         }
880ae4f23fSEric Fiselier     }
890ae4f23fSEric Fiselier     return p;
900ae4f23fSEric Fiselier }
910ae4f23fSEric Fiselier 
92*dce4218dSShoaib Meenai _LIBCPP_WEAK
930ae4f23fSEric 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 
111*dce4218dSShoaib Meenai _LIBCPP_WEAK
112111e0cbeSNick Kledzik void*
1130ae4f23fSEric Fiselier operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
1140ae4f23fSEric Fiselier {
1150ae4f23fSEric Fiselier     void* p = 0;
1160ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS
1170ae4f23fSEric Fiselier     try
1180ae4f23fSEric Fiselier     {
1190ae4f23fSEric Fiselier #endif  // _LIBCPP_NO_EXCEPTIONS
1200ae4f23fSEric Fiselier         p = ::operator new(size, alignment);
1210ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS
1220ae4f23fSEric Fiselier     }
1230ae4f23fSEric Fiselier     catch (...)
1240ae4f23fSEric Fiselier     {
1250ae4f23fSEric Fiselier     }
1260ae4f23fSEric Fiselier #endif  // _LIBCPP_NO_EXCEPTIONS
1270ae4f23fSEric Fiselier     return p;
1280ae4f23fSEric Fiselier }
1290ae4f23fSEric Fiselier 
130*dce4218dSShoaib Meenai _LIBCPP_WEAK
1310ae4f23fSEric Fiselier void*
1320ae4f23fSEric Fiselier operator new[](size_t size) _THROW_BAD_ALLOC
133111e0cbeSNick Kledzik {
134111e0cbeSNick Kledzik     return ::operator new(size);
135111e0cbeSNick Kledzik }
136111e0cbeSNick Kledzik 
137*dce4218dSShoaib Meenai _LIBCPP_WEAK
138111e0cbeSNick Kledzik void*
1390ae4f23fSEric Fiselier operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
1400ae4f23fSEric Fiselier {
1410ae4f23fSEric Fiselier     return ::operator new(size, alignment);
1420ae4f23fSEric Fiselier }
1430ae4f23fSEric Fiselier 
144*dce4218dSShoaib Meenai _LIBCPP_WEAK
1450ae4f23fSEric 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 
163*dce4218dSShoaib Meenai _LIBCPP_WEAK
1640ae4f23fSEric Fiselier void*
1650ae4f23fSEric Fiselier operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
1660ae4f23fSEric Fiselier {
1670ae4f23fSEric Fiselier     void* p = 0;
1680ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS
1690ae4f23fSEric Fiselier     try
1700ae4f23fSEric Fiselier     {
1710ae4f23fSEric Fiselier #endif  // _LIBCPP_NO_EXCEPTIONS
1720ae4f23fSEric Fiselier         p = ::operator new[](size, alignment);
1730ae4f23fSEric Fiselier #ifndef _LIBCPP_NO_EXCEPTIONS
1740ae4f23fSEric Fiselier     }
1750ae4f23fSEric Fiselier     catch (...)
1760ae4f23fSEric Fiselier     {
1770ae4f23fSEric Fiselier     }
1780ae4f23fSEric Fiselier #endif  // _LIBCPP_NO_EXCEPTIONS
1790ae4f23fSEric Fiselier     return p;
1800ae4f23fSEric Fiselier }
1810ae4f23fSEric Fiselier 
182*dce4218dSShoaib Meenai _LIBCPP_WEAK
183111e0cbeSNick Kledzik void
184fafca58cSHoward Hinnant operator delete(void* ptr) _NOEXCEPT
185111e0cbeSNick Kledzik {
186111e0cbeSNick Kledzik     if (ptr)
187111e0cbeSNick Kledzik         ::free(ptr);
188111e0cbeSNick Kledzik }
189111e0cbeSNick Kledzik 
190*dce4218dSShoaib Meenai _LIBCPP_WEAK
191111e0cbeSNick Kledzik void
1920ae4f23fSEric Fiselier operator delete(void* ptr, std::align_val_t) _NOEXCEPT
1930ae4f23fSEric Fiselier {
1940ae4f23fSEric Fiselier     if (ptr)
1950ae4f23fSEric Fiselier         ::free(ptr);
1960ae4f23fSEric Fiselier }
1970ae4f23fSEric Fiselier 
198*dce4218dSShoaib Meenai _LIBCPP_WEAK
1990ae4f23fSEric Fiselier void
200fafca58cSHoward Hinnant operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
201111e0cbeSNick Kledzik {
202111e0cbeSNick Kledzik     ::operator delete(ptr);
203111e0cbeSNick Kledzik }
204111e0cbeSNick Kledzik 
205*dce4218dSShoaib Meenai _LIBCPP_WEAK
206111e0cbeSNick Kledzik void
2070ae4f23fSEric Fiselier operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
2080ae4f23fSEric Fiselier {
2090ae4f23fSEric Fiselier     ::operator delete(ptr, alignment);
2100ae4f23fSEric Fiselier }
2110ae4f23fSEric Fiselier 
212*dce4218dSShoaib Meenai _LIBCPP_WEAK
2130ae4f23fSEric Fiselier void
214e4864e0bSLarisse Voufo operator delete(void* ptr, size_t) _NOEXCEPT
215d36bd700SLarisse Voufo {
216d36bd700SLarisse Voufo     ::operator delete(ptr);
217d36bd700SLarisse Voufo }
218d36bd700SLarisse Voufo 
219*dce4218dSShoaib Meenai _LIBCPP_WEAK
220d36bd700SLarisse Voufo void
2210ae4f23fSEric Fiselier operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
2220ae4f23fSEric Fiselier {
2230ae4f23fSEric Fiselier     ::operator delete(ptr, alignment);
2240ae4f23fSEric Fiselier }
2250ae4f23fSEric Fiselier 
226*dce4218dSShoaib Meenai _LIBCPP_WEAK
2270ae4f23fSEric Fiselier void
228fafca58cSHoward Hinnant operator delete[] (void* ptr) _NOEXCEPT
229111e0cbeSNick Kledzik {
230111e0cbeSNick Kledzik     ::operator delete(ptr);
231111e0cbeSNick Kledzik }
232111e0cbeSNick Kledzik 
233*dce4218dSShoaib Meenai _LIBCPP_WEAK
234111e0cbeSNick Kledzik void
2350ae4f23fSEric Fiselier operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
2360ae4f23fSEric Fiselier {
2370ae4f23fSEric Fiselier     ::operator delete(ptr, alignment);
2380ae4f23fSEric Fiselier }
2390ae4f23fSEric Fiselier 
240*dce4218dSShoaib Meenai _LIBCPP_WEAK
2410ae4f23fSEric Fiselier void
242fafca58cSHoward Hinnant operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
243111e0cbeSNick Kledzik {
244111e0cbeSNick Kledzik     ::operator delete[](ptr);
245111e0cbeSNick Kledzik }
246111e0cbeSNick Kledzik 
247*dce4218dSShoaib Meenai _LIBCPP_WEAK
248d36bd700SLarisse Voufo void
2490ae4f23fSEric Fiselier operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
2500ae4f23fSEric Fiselier {
2510ae4f23fSEric Fiselier     ::operator delete[](ptr, alignment);
2520ae4f23fSEric Fiselier }
2530ae4f23fSEric Fiselier 
254*dce4218dSShoaib Meenai _LIBCPP_WEAK
2550ae4f23fSEric Fiselier void
256e4864e0bSLarisse Voufo operator delete[] (void* ptr, size_t) _NOEXCEPT
257d36bd700SLarisse Voufo {
258d36bd700SLarisse Voufo     ::operator delete[](ptr);
259d36bd700SLarisse Voufo }
260d36bd700SLarisse Voufo 
261*dce4218dSShoaib Meenai _LIBCPP_WEAK
2620ae4f23fSEric Fiselier void
2630ae4f23fSEric Fiselier operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
2640ae4f23fSEric Fiselier {
2650ae4f23fSEric Fiselier     ::operator delete[](ptr, alignment);
2660ae4f23fSEric Fiselier }
2670ae4f23fSEric 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