1 //===--------------------------- new.cpp ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define _LIBCPP_BUILDING_NEW
16 #if defined(__APPLE__) && !defined(LIBCXXRT)
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
26 #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
28 #endif // defined(LIBCXX_BUILDING_LIBCXXABI)
29 #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
30 static std::new_handler __new_handler
;
31 #endif // _LIBCPPABI_VERSION
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.
40 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
42 operator new(std::size_t size
)
43 #if !__has_feature(cxx_noexcept)
50 while ((p
= ::malloc(size
)) == 0)
52 // If malloc fails and there is a new_handler,
53 // call it to try free up memory.
54 std::new_handler nh
= std::get_new_handler();
58 #ifndef _LIBCPP_NO_EXCEPTIONS
59 throw std::bad_alloc();
67 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
69 operator new(size_t size
, const std::nothrow_t
&) _NOEXCEPT
72 #ifndef _LIBCPP_NO_EXCEPTIONS
75 #endif // _LIBCPP_NO_EXCEPTIONS
76 p
= ::operator new(size
);
77 #ifndef _LIBCPP_NO_EXCEPTIONS
82 #endif // _LIBCPP_NO_EXCEPTIONS
86 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
88 operator new[](size_t size
)
89 #if !__has_feature(cxx_noexcept)
93 return ::operator new(size
);
96 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
98 operator new[](size_t size
, const std::nothrow_t
&) _NOEXCEPT
101 #ifndef _LIBCPP_NO_EXCEPTIONS
104 #endif // _LIBCPP_NO_EXCEPTIONS
105 p
= ::operator new[](size
);
106 #ifndef _LIBCPP_NO_EXCEPTIONS
111 #endif // _LIBCPP_NO_EXCEPTIONS
115 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
117 operator delete(void* ptr
) _NOEXCEPT
123 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
125 operator delete(void* ptr
, const std::nothrow_t
&) _NOEXCEPT
127 ::operator delete(ptr
);
130 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
132 operator delete(void* ptr
, size_t) _NOEXCEPT
134 ::operator delete(ptr
);
137 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
139 operator delete[] (void* ptr
) _NOEXCEPT
141 ::operator delete(ptr
);
144 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
146 operator delete[] (void* ptr
, const std::nothrow_t
&) _NOEXCEPT
148 ::operator delete[](ptr
);
151 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
153 operator delete[] (void* ptr
, size_t) _NOEXCEPT
155 ::operator delete[](ptr
);
158 #endif // !__GLIBCXX__
164 const nothrow_t nothrow
= {};
167 #ifndef _LIBCPPABI_VERSION
172 set_new_handler(new_handler handler
) _NOEXCEPT
174 return __sync_lock_test_and_set(&__new_handler
, handler
);
178 get_new_handler() _NOEXCEPT
180 return __sync_fetch_and_add(&__new_handler
, nullptr);
183 #endif // !__GLIBCXX__
187 bad_alloc::bad_alloc() _NOEXCEPT
193 bad_alloc::~bad_alloc() _NOEXCEPT
198 bad_alloc::what() const _NOEXCEPT
200 return "std::bad_alloc";
203 #endif // !__GLIBCXX__
205 bad_array_new_length::bad_array_new_length() _NOEXCEPT
209 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
214 bad_array_new_length::what() const _NOEXCEPT
216 return "bad_array_new_length";
222 bad_array_length::what() const _NOEXCEPT
224 return "bad_array_length";
227 bad_array_length::bad_array_length() _NOEXCEPT
231 bad_array_length::~bad_array_length() _NOEXCEPT
235 #endif // _LIBCPPABI_VERSION
242 #ifndef _LIBCPP_NO_EXCEPTIONS