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
17 #define __has_include(inc) 0
20 #if defined(__APPLE__) && !defined(LIBCXXRT)
23 #ifndef _LIBCPPABI_VERSION
24 // On Darwin, there are two STL shared libraries and a lower level ABI
25 // shared library. The global holding the current new handler is
26 // in the ABI library and named __cxa_new_handler.
27 #define __new_handler __cxxabiapple::__cxa_new_handler
30 #if defined(LIBCXXRT) || __has_include(<cxxabi.h>)
32 #endif // __has_include(<cxxabi.h>)
33 #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
34 static std::new_handler __new_handler;
35 #endif // _LIBCPPABI_VERSION
40 // Implement all new and delete operators as weak definitions
41 // in this shared library, so that they can be overriden by programs
42 // that define non-weak copies of the functions.
44 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
46 operator new(std::size_t size)
47 #if !__has_feature(cxx_noexcept)
54 while ((p = ::malloc(size)) == 0)
56 // If malloc fails and there is a new_handler,
57 // call it to try free up memory.
58 std::new_handler nh = std::get_new_handler();
62 #ifndef _LIBCPP_NO_EXCEPTIONS
63 throw std::bad_alloc();
71 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
73 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
76 #ifndef _LIBCPP_NO_EXCEPTIONS
79 #endif // _LIBCPP_NO_EXCEPTIONS
80 p = ::operator new(size);
81 #ifndef _LIBCPP_NO_EXCEPTIONS
86 #endif // _LIBCPP_NO_EXCEPTIONS
90 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
92 operator new[](size_t size)
93 #if !__has_feature(cxx_noexcept)
97 return ::operator new(size);
100 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
102 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
105 #ifndef _LIBCPP_NO_EXCEPTIONS
108 #endif // _LIBCPP_NO_EXCEPTIONS
109 p = ::operator new[](size);
110 #ifndef _LIBCPP_NO_EXCEPTIONS
115 #endif // _LIBCPP_NO_EXCEPTIONS
119 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
121 operator delete(void* ptr) _NOEXCEPT
127 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
129 operator delete(void* ptr, size_t) _NOEXCEPT
131 ::operator delete(ptr);
134 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
136 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
138 ::operator delete(ptr);
141 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
143 operator delete(void* ptr, size_t, const std::nothrow_t&) _NOEXCEPT
145 ::operator delete(ptr);
148 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
150 operator delete[] (void* ptr) _NOEXCEPT
152 ::operator delete(ptr);
155 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
157 operator delete[] (void* ptr, size_t) _NOEXCEPT
159 ::operator delete[](ptr);
162 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
164 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
166 ::operator delete[](ptr);
169 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
171 operator delete[] (void* ptr, size_t, const std::nothrow_t&) _NOEXCEPT
173 ::operator delete[](ptr);
176 #endif // !__GLIBCXX__
182 const nothrow_t nothrow = {};
185 #ifndef _LIBCPPABI_VERSION
190 set_new_handler(new_handler handler) _NOEXCEPT
192 return __sync_lock_test_and_set(&__new_handler, handler);
196 get_new_handler() _NOEXCEPT
198 return __sync_fetch_and_add(&__new_handler, nullptr);
201 #endif // !__GLIBCXX__
205 bad_alloc::bad_alloc() _NOEXCEPT
211 bad_alloc::~bad_alloc() _NOEXCEPT
216 bad_alloc::what() const _NOEXCEPT
218 return "std::bad_alloc";
221 #endif // !__GLIBCXX__
223 bad_array_new_length::bad_array_new_length() _NOEXCEPT
227 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
232 bad_array_new_length::what() const _NOEXCEPT
234 return "bad_array_new_length";
240 bad_array_length::what() const _NOEXCEPT
242 return "bad_array_length";
245 bad_array_length::bad_array_length() _NOEXCEPT
249 bad_array_length::~bad_array_length() _NOEXCEPT
253 #endif // _LIBCPPABI_VERSION
260 #ifndef _LIBCPP_NO_EXCEPTIONS