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) && \
17 !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
20 #ifndef _LIBCPPABI_VERSION
21 // On Darwin, there are two STL shared libraries and a lower level ABI
22 // shared library. The global holding the current new handler is
23 // in the ABI library and named __cxa_new_handler.
24 #define __new_handler __cxxabiapple::__cxa_new_handler
27 #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
29 #endif // defined(LIBCXX_BUILDING_LIBCXXABI)
30 #if defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) || \
31 (!defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__))
32 static std::new_handler __new_handler;
33 #endif // _LIBCPPABI_VERSION
38 // Implement all new and delete operators as weak definitions
39 // in this shared library, so that they can be overridden by programs
40 // that define non-weak copies of the functions.
44 operator new(std::size_t size) _THROW_BAD_ALLOC
49 while ((p = ::malloc(size)) == 0)
51 // If malloc fails and there is a new_handler,
52 // call it to try free up memory.
53 std::new_handler nh = std::get_new_handler();
57 #ifndef _LIBCPP_NO_EXCEPTIONS
58 throw std::bad_alloc();
68 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
72 if (static_cast<size_t>(alignment) < sizeof(void*))
73 alignment = std::align_val_t(sizeof(void*));
75 #if defined(_LIBCPP_MSVCRT)
76 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
78 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
81 // If posix_memalign fails and there is a new_handler,
82 // call it to try free up memory.
83 std::new_handler nh = std::get_new_handler();
87 #ifndef _LIBCPP_NO_EXCEPTIONS
88 throw std::bad_alloc();
90 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
100 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
103 #ifndef _LIBCPP_NO_EXCEPTIONS
106 #endif // _LIBCPP_NO_EXCEPTIONS
107 p = ::operator new(size);
108 #ifndef _LIBCPP_NO_EXCEPTIONS
113 #endif // _LIBCPP_NO_EXCEPTIONS
119 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
122 #ifndef _LIBCPP_NO_EXCEPTIONS
125 #endif // _LIBCPP_NO_EXCEPTIONS
126 p = ::operator new(size, alignment);
127 #ifndef _LIBCPP_NO_EXCEPTIONS
132 #endif // _LIBCPP_NO_EXCEPTIONS
138 operator new[](size_t size) _THROW_BAD_ALLOC
140 return ::operator new(size);
145 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
147 return ::operator new(size, alignment);
152 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
155 #ifndef _LIBCPP_NO_EXCEPTIONS
158 #endif // _LIBCPP_NO_EXCEPTIONS
159 p = ::operator new[](size);
160 #ifndef _LIBCPP_NO_EXCEPTIONS
165 #endif // _LIBCPP_NO_EXCEPTIONS
171 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
174 #ifndef _LIBCPP_NO_EXCEPTIONS
177 #endif // _LIBCPP_NO_EXCEPTIONS
178 p = ::operator new[](size, alignment);
179 #ifndef _LIBCPP_NO_EXCEPTIONS
184 #endif // _LIBCPP_NO_EXCEPTIONS
190 operator delete(void* ptr) _NOEXCEPT
198 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
206 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
208 ::operator delete(ptr);
213 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
215 ::operator delete(ptr, alignment);
220 operator delete(void* ptr, size_t) _NOEXCEPT
222 ::operator delete(ptr);
227 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
229 ::operator delete(ptr, alignment);
234 operator delete[] (void* ptr) _NOEXCEPT
236 ::operator delete(ptr);
241 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
243 ::operator delete(ptr, alignment);
248 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
250 ::operator delete[](ptr);
255 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
257 ::operator delete[](ptr, alignment);
262 operator delete[] (void* ptr, size_t) _NOEXCEPT
264 ::operator delete[](ptr);
269 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
271 ::operator delete[](ptr, alignment);
274 #endif // !__GLIBCXX__
280 const nothrow_t nothrow = {};
283 #ifndef _LIBCPPABI_VERSION
288 set_new_handler(new_handler handler) _NOEXCEPT
290 return __sync_lock_test_and_set(&__new_handler, handler);
294 get_new_handler() _NOEXCEPT
296 return __sync_fetch_and_add(&__new_handler, nullptr);
299 #endif // !__GLIBCXX__
303 bad_alloc::bad_alloc() _NOEXCEPT
309 bad_alloc::~bad_alloc() _NOEXCEPT
314 bad_alloc::what() const _NOEXCEPT
316 return "std::bad_alloc";
319 #endif // !__GLIBCXX__
321 bad_array_new_length::bad_array_new_length() _NOEXCEPT
327 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
332 bad_array_new_length::what() const _NOEXCEPT
334 return "bad_array_new_length";
337 #endif // !__GLIBCXX__
341 bad_array_length::bad_array_length() _NOEXCEPT
347 bad_array_length::~bad_array_length() _NOEXCEPT
352 bad_array_length::what() const _NOEXCEPT
354 return "bad_array_length";
357 #endif // !__GLIBCXX__
359 #endif // _LIBCPPABI_VERSION
366 #ifndef _LIBCPP_NO_EXCEPTIONS