14684ddb6SLionel Sambuc //===--------------------------- new.cpp ----------------------------------===//
24684ddb6SLionel Sambuc //
34684ddb6SLionel Sambuc // The LLVM Compiler Infrastructure
44684ddb6SLionel Sambuc //
54684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
64684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
74684ddb6SLionel Sambuc //
84684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
94684ddb6SLionel Sambuc
104684ddb6SLionel Sambuc #define _LIBCPP_BUILDING_NEW
114684ddb6SLionel Sambuc
124684ddb6SLionel Sambuc #include <stdlib.h>
134684ddb6SLionel Sambuc
144684ddb6SLionel Sambuc #include "new"
154684ddb6SLionel Sambuc
164684ddb6SLionel Sambuc #ifndef __has_include
174684ddb6SLionel Sambuc #define __has_include(inc) 0
184684ddb6SLionel Sambuc #endif
194684ddb6SLionel Sambuc
20*0a6a1f1dSLionel Sambuc #if defined(__APPLE__) && !defined(LIBCXXRT)
214684ddb6SLionel Sambuc #include <cxxabi.h>
224684ddb6SLionel Sambuc
234684ddb6SLionel Sambuc #ifndef _LIBCPPABI_VERSION
244684ddb6SLionel Sambuc // On Darwin, there are two STL shared libraries and a lower level ABI
254684ddb6SLionel Sambuc // shared library. The global holding the current new handler is
264684ddb6SLionel Sambuc // in the ABI library and named __cxa_new_handler.
274684ddb6SLionel Sambuc #define __new_handler __cxxabiapple::__cxa_new_handler
284684ddb6SLionel Sambuc #endif
294684ddb6SLionel Sambuc #else // __APPLE__
304684ddb6SLionel Sambuc #if defined(LIBCXXRT) || __has_include(<cxxabi.h>)
314684ddb6SLionel Sambuc #include <cxxabi.h>
324684ddb6SLionel Sambuc #endif // __has_include(<cxxabi.h>)
334684ddb6SLionel Sambuc #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
344684ddb6SLionel Sambuc static std::new_handler __new_handler;
354684ddb6SLionel Sambuc #endif // _LIBCPPABI_VERSION
364684ddb6SLionel Sambuc #endif
374684ddb6SLionel Sambuc
384684ddb6SLionel Sambuc #ifndef __GLIBCXX__
394684ddb6SLionel Sambuc
404684ddb6SLionel Sambuc // Implement all new and delete operators as weak definitions
41*0a6a1f1dSLionel Sambuc // in this shared library, so that they can be overridden by programs
424684ddb6SLionel Sambuc // that define non-weak copies of the functions.
434684ddb6SLionel Sambuc
444684ddb6SLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
454684ddb6SLionel Sambuc void *
operator new(std::size_t size)464684ddb6SLionel Sambuc operator new(std::size_t size)
474684ddb6SLionel Sambuc #if !__has_feature(cxx_noexcept)
484684ddb6SLionel Sambuc throw(std::bad_alloc)
494684ddb6SLionel Sambuc #endif
504684ddb6SLionel Sambuc {
514684ddb6SLionel Sambuc if (size == 0)
524684ddb6SLionel Sambuc size = 1;
534684ddb6SLionel Sambuc void* p;
544684ddb6SLionel Sambuc while ((p = ::malloc(size)) == 0)
554684ddb6SLionel Sambuc {
564684ddb6SLionel Sambuc // If malloc fails and there is a new_handler,
574684ddb6SLionel Sambuc // call it to try free up memory.
584684ddb6SLionel Sambuc std::new_handler nh = std::get_new_handler();
594684ddb6SLionel Sambuc if (nh)
604684ddb6SLionel Sambuc nh();
614684ddb6SLionel Sambuc else
624684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
634684ddb6SLionel Sambuc throw std::bad_alloc();
644684ddb6SLionel Sambuc #else
654684ddb6SLionel Sambuc break;
664684ddb6SLionel Sambuc #endif
674684ddb6SLionel Sambuc }
684684ddb6SLionel Sambuc return p;
694684ddb6SLionel Sambuc }
704684ddb6SLionel Sambuc
714684ddb6SLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
724684ddb6SLionel Sambuc void*
operator new(size_t size,const std::nothrow_t &)734684ddb6SLionel Sambuc operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
744684ddb6SLionel Sambuc {
754684ddb6SLionel Sambuc void* p = 0;
764684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
774684ddb6SLionel Sambuc try
784684ddb6SLionel Sambuc {
794684ddb6SLionel Sambuc #endif // _LIBCPP_NO_EXCEPTIONS
804684ddb6SLionel Sambuc p = ::operator new(size);
814684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
824684ddb6SLionel Sambuc }
834684ddb6SLionel Sambuc catch (...)
844684ddb6SLionel Sambuc {
854684ddb6SLionel Sambuc }
864684ddb6SLionel Sambuc #endif // _LIBCPP_NO_EXCEPTIONS
874684ddb6SLionel Sambuc return p;
884684ddb6SLionel Sambuc }
894684ddb6SLionel Sambuc
904684ddb6SLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
914684ddb6SLionel Sambuc void*
operator new[](size_t size)924684ddb6SLionel Sambuc operator new[](size_t size)
934684ddb6SLionel Sambuc #if !__has_feature(cxx_noexcept)
944684ddb6SLionel Sambuc throw(std::bad_alloc)
954684ddb6SLionel Sambuc #endif
964684ddb6SLionel Sambuc {
974684ddb6SLionel Sambuc return ::operator new(size);
984684ddb6SLionel Sambuc }
994684ddb6SLionel Sambuc
1004684ddb6SLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
1014684ddb6SLionel Sambuc void*
operator new[](size_t size,const std::nothrow_t &)1024684ddb6SLionel Sambuc operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
1034684ddb6SLionel Sambuc {
1044684ddb6SLionel Sambuc void* p = 0;
1054684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
1064684ddb6SLionel Sambuc try
1074684ddb6SLionel Sambuc {
1084684ddb6SLionel Sambuc #endif // _LIBCPP_NO_EXCEPTIONS
1094684ddb6SLionel Sambuc p = ::operator new[](size);
1104684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
1114684ddb6SLionel Sambuc }
1124684ddb6SLionel Sambuc catch (...)
1134684ddb6SLionel Sambuc {
1144684ddb6SLionel Sambuc }
1154684ddb6SLionel Sambuc #endif // _LIBCPP_NO_EXCEPTIONS
1164684ddb6SLionel Sambuc return p;
1174684ddb6SLionel Sambuc }
1184684ddb6SLionel Sambuc
1194684ddb6SLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
1204684ddb6SLionel Sambuc void
operator delete(void * ptr)1214684ddb6SLionel Sambuc operator delete(void* ptr) _NOEXCEPT
1224684ddb6SLionel Sambuc {
1234684ddb6SLionel Sambuc if (ptr)
1244684ddb6SLionel Sambuc ::free(ptr);
1254684ddb6SLionel Sambuc }
1264684ddb6SLionel Sambuc
1274684ddb6SLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
1284684ddb6SLionel Sambuc void
operator delete(void * ptr,const std::nothrow_t &)1294684ddb6SLionel Sambuc operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
1304684ddb6SLionel Sambuc {
1314684ddb6SLionel Sambuc ::operator delete(ptr);
1324684ddb6SLionel Sambuc }
1334684ddb6SLionel Sambuc
1344684ddb6SLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
1354684ddb6SLionel Sambuc void
operator delete(void * ptr,size_t)136*0a6a1f1dSLionel Sambuc operator delete(void* ptr, size_t) _NOEXCEPT
137*0a6a1f1dSLionel Sambuc {
138*0a6a1f1dSLionel Sambuc ::operator delete(ptr);
139*0a6a1f1dSLionel Sambuc }
140*0a6a1f1dSLionel Sambuc
141*0a6a1f1dSLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
142*0a6a1f1dSLionel Sambuc void
operator delete[](void * ptr)1434684ddb6SLionel Sambuc operator delete[] (void* ptr) _NOEXCEPT
1444684ddb6SLionel Sambuc {
1454684ddb6SLionel Sambuc ::operator delete(ptr);
1464684ddb6SLionel Sambuc }
1474684ddb6SLionel Sambuc
1484684ddb6SLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
1494684ddb6SLionel Sambuc void
operator delete[](void * ptr,const std::nothrow_t &)1504684ddb6SLionel Sambuc operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
1514684ddb6SLionel Sambuc {
1524684ddb6SLionel Sambuc ::operator delete[](ptr);
1534684ddb6SLionel Sambuc }
1544684ddb6SLionel Sambuc
155*0a6a1f1dSLionel Sambuc _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
156*0a6a1f1dSLionel Sambuc void
operator delete[](void * ptr,size_t)157*0a6a1f1dSLionel Sambuc operator delete[] (void* ptr, size_t) _NOEXCEPT
158*0a6a1f1dSLionel Sambuc {
159*0a6a1f1dSLionel Sambuc ::operator delete[](ptr);
160*0a6a1f1dSLionel Sambuc }
161*0a6a1f1dSLionel Sambuc
1624684ddb6SLionel Sambuc #endif // !__GLIBCXX__
1634684ddb6SLionel Sambuc
1644684ddb6SLionel Sambuc namespace std
1654684ddb6SLionel Sambuc {
1664684ddb6SLionel Sambuc
1674684ddb6SLionel Sambuc #ifndef __GLIBCXX__
1684684ddb6SLionel Sambuc const nothrow_t nothrow = {};
1694684ddb6SLionel Sambuc #endif
1704684ddb6SLionel Sambuc
1714684ddb6SLionel Sambuc #ifndef _LIBCPPABI_VERSION
1724684ddb6SLionel Sambuc
1734684ddb6SLionel Sambuc #ifndef __GLIBCXX__
1744684ddb6SLionel Sambuc
1754684ddb6SLionel Sambuc new_handler
set_new_handler(new_handler handler)1764684ddb6SLionel Sambuc set_new_handler(new_handler handler) _NOEXCEPT
1774684ddb6SLionel Sambuc {
1784684ddb6SLionel Sambuc return __sync_lock_test_and_set(&__new_handler, handler);
1794684ddb6SLionel Sambuc }
1804684ddb6SLionel Sambuc
1814684ddb6SLionel Sambuc new_handler
get_new_handler()1824684ddb6SLionel Sambuc get_new_handler() _NOEXCEPT
1834684ddb6SLionel Sambuc {
184*0a6a1f1dSLionel Sambuc return __sync_fetch_and_add(&__new_handler, nullptr);
1854684ddb6SLionel Sambuc }
1864684ddb6SLionel Sambuc
1874684ddb6SLionel Sambuc #endif // !__GLIBCXX__
1884684ddb6SLionel Sambuc
1894684ddb6SLionel Sambuc #ifndef LIBCXXRT
1904684ddb6SLionel Sambuc
bad_alloc()1914684ddb6SLionel Sambuc bad_alloc::bad_alloc() _NOEXCEPT
1924684ddb6SLionel Sambuc {
1934684ddb6SLionel Sambuc }
1944684ddb6SLionel Sambuc
1954684ddb6SLionel Sambuc #ifndef __GLIBCXX__
1964684ddb6SLionel Sambuc
~bad_alloc()1974684ddb6SLionel Sambuc bad_alloc::~bad_alloc() _NOEXCEPT
1984684ddb6SLionel Sambuc {
1994684ddb6SLionel Sambuc }
2004684ddb6SLionel Sambuc
2014684ddb6SLionel Sambuc const char*
what() const2024684ddb6SLionel Sambuc bad_alloc::what() const _NOEXCEPT
2034684ddb6SLionel Sambuc {
2044684ddb6SLionel Sambuc return "std::bad_alloc";
2054684ddb6SLionel Sambuc }
2064684ddb6SLionel Sambuc
2074684ddb6SLionel Sambuc #endif // !__GLIBCXX__
2084684ddb6SLionel Sambuc
bad_array_new_length()2094684ddb6SLionel Sambuc bad_array_new_length::bad_array_new_length() _NOEXCEPT
2104684ddb6SLionel Sambuc {
2114684ddb6SLionel Sambuc }
2124684ddb6SLionel Sambuc
~bad_array_new_length()2134684ddb6SLionel Sambuc bad_array_new_length::~bad_array_new_length() _NOEXCEPT
2144684ddb6SLionel Sambuc {
2154684ddb6SLionel Sambuc }
2164684ddb6SLionel Sambuc
2174684ddb6SLionel Sambuc const char*
what() const218*0a6a1f1dSLionel Sambuc bad_array_new_length::what() const _NOEXCEPT
219*0a6a1f1dSLionel Sambuc {
220*0a6a1f1dSLionel Sambuc return "bad_array_new_length";
221*0a6a1f1dSLionel Sambuc }
222*0a6a1f1dSLionel Sambuc
223*0a6a1f1dSLionel Sambuc #endif //LIBCXXRT
224*0a6a1f1dSLionel Sambuc
225*0a6a1f1dSLionel Sambuc const char*
what() const2264684ddb6SLionel Sambuc bad_array_length::what() const _NOEXCEPT
2274684ddb6SLionel Sambuc {
2284684ddb6SLionel Sambuc return "bad_array_length";
2294684ddb6SLionel Sambuc }
2304684ddb6SLionel Sambuc
bad_array_length()2314684ddb6SLionel Sambuc bad_array_length::bad_array_length() _NOEXCEPT
2324684ddb6SLionel Sambuc {
2334684ddb6SLionel Sambuc }
2344684ddb6SLionel Sambuc
~bad_array_length()2354684ddb6SLionel Sambuc bad_array_length::~bad_array_length() _NOEXCEPT
2364684ddb6SLionel Sambuc {
2374684ddb6SLionel Sambuc }
2384684ddb6SLionel Sambuc
2394684ddb6SLionel Sambuc #endif // _LIBCPPABI_VERSION
2404684ddb6SLionel Sambuc
2414684ddb6SLionel Sambuc #ifndef LIBSTDCXX
2424684ddb6SLionel Sambuc
2434684ddb6SLionel Sambuc void
__throw_bad_alloc()2444684ddb6SLionel Sambuc __throw_bad_alloc()
2454684ddb6SLionel Sambuc {
2464684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
2474684ddb6SLionel Sambuc throw bad_alloc();
2484684ddb6SLionel Sambuc #endif
2494684ddb6SLionel Sambuc }
2504684ddb6SLionel Sambuc
2514684ddb6SLionel Sambuc #endif // !LIBSTDCXX
2524684ddb6SLionel Sambuc
2534684ddb6SLionel Sambuc } // std
254