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
201 #if defined(_LIBCPP_MSVCRT)
202 ::_aligned_free(ptr);
210 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
212 ::operator delete(ptr);
217 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
219 ::operator delete(ptr, alignment);
224 operator delete(void* ptr, size_t) _NOEXCEPT
226 ::operator delete(ptr);
231 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
233 ::operator delete(ptr, alignment);
238 operator delete[] (void* ptr) _NOEXCEPT
240 ::operator delete(ptr);
245 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
247 ::operator delete(ptr, alignment);
252 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
254 ::operator delete[](ptr);
259 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
261 ::operator delete[](ptr, alignment);
266 operator delete[] (void* ptr, size_t) _NOEXCEPT
268 ::operator delete[](ptr);
273 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
275 ::operator delete[](ptr, alignment);
278 #endif // !__GLIBCXX__
284 const nothrow_t nothrow = {};
287 #ifndef _LIBCPPABI_VERSION
292 set_new_handler(new_handler handler) _NOEXCEPT
294 return __sync_lock_test_and_set(&__new_handler, handler);
298 get_new_handler() _NOEXCEPT
300 return __sync_fetch_and_add(&__new_handler, nullptr);
303 #endif // !__GLIBCXX__
307 bad_alloc::bad_alloc() _NOEXCEPT
313 bad_alloc::~bad_alloc() _NOEXCEPT
318 bad_alloc::what() const _NOEXCEPT
320 return "std::bad_alloc";
323 #endif // !__GLIBCXX__
325 bad_array_new_length::bad_array_new_length() _NOEXCEPT
331 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
336 bad_array_new_length::what() const _NOEXCEPT
338 return "bad_array_new_length";
341 #endif // !__GLIBCXX__
345 bad_array_length::bad_array_length() _NOEXCEPT
351 bad_array_length::~bad_array_length() _NOEXCEPT
356 bad_array_length::what() const _NOEXCEPT
358 return "bad_array_length";
361 #endif // !__GLIBCXX__
363 #endif // _LIBCPPABI_VERSION
370 #ifndef _LIBCPP_NO_EXCEPTIONS