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 //===----------------------------------------------------------------------===//
15 #define __has_include(inc) 0
21 #ifndef _LIBCPPABI_VERSION
22 // On Darwin, there are two STL shared libraries and a lower level ABI
23 // shared libray. The global holding the current new handler is
24 // in the ABI library and named __cxa_new_handler.
25 #define __new_handler __cxxabiapple::__cxa_new_handler
28 #if defined(LIBCXXRT) || __has_include(<cxxabi.h>)
30 #endif // __has_include(<cxxabi.h>)
31 #ifndef _LIBCPPABI_VERSION
32 static std::new_handler __new_handler;
33 #endif // _LIBCPPABI_VERSION
36 // Implement all new and delete operators as weak definitions
37 // in this shared library, so that they can be overriden by programs
38 // that define non-weak copies of the functions.
40 __attribute__((__weak__, __visibility__("default")))
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 __attribute__((__weak__, __visibility__("default")))
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 __attribute__((__weak__, __visibility__("default")))
88 operator new[](size_t size)
89 #if !__has_feature(cxx_noexcept)
93 return ::operator new(size);
96 __attribute__((__weak__, __visibility__("default")))
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 __attribute__((__weak__, __visibility__("default")))
117 operator delete(void* ptr) _NOEXCEPT
123 __attribute__((__weak__, __visibility__("default")))
125 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
127 ::operator delete(ptr);
130 __attribute__((__weak__, __visibility__("default")))
132 operator delete[] (void* ptr) _NOEXCEPT
134 ::operator delete (ptr);
137 __attribute__((__weak__, __visibility__("default")))
139 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
141 ::operator delete[](ptr);
147 const nothrow_t nothrow = {};
149 #ifndef _LIBCPPABI_VERSION
152 set_new_handler(new_handler handler) _NOEXCEPT
154 return __sync_lock_test_and_set(&__new_handler, handler);
158 get_new_handler() _NOEXCEPT
160 return __sync_fetch_and_add(&__new_handler, (new_handler)0);
165 bad_alloc::bad_alloc() _NOEXCEPT
169 bad_alloc::~bad_alloc() _NOEXCEPT
174 bad_alloc::what() const _NOEXCEPT
176 return "std::bad_alloc";
181 bad_array_new_length::bad_array_new_length() _NOEXCEPT
185 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
190 bad_array_new_length::what() const _NOEXCEPT
192 return "bad_array_new_length";
200 #ifndef _LIBCPP_NO_EXCEPTIONS