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(size_t size, const std::nothrow_t&) _NOEXCEPT
71 #ifndef _LIBCPP_NO_EXCEPTIONS
74 #endif // _LIBCPP_NO_EXCEPTIONS
75 p = ::operator new(size);
76 #ifndef _LIBCPP_NO_EXCEPTIONS
81 #endif // _LIBCPP_NO_EXCEPTIONS
87 operator new[](size_t size) _THROW_BAD_ALLOC
89 return ::operator new(size);
94 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
97 #ifndef _LIBCPP_NO_EXCEPTIONS
100 #endif // _LIBCPP_NO_EXCEPTIONS
101 p = ::operator new[](size);
102 #ifndef _LIBCPP_NO_EXCEPTIONS
107 #endif // _LIBCPP_NO_EXCEPTIONS
113 operator delete(void* ptr) _NOEXCEPT
121 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
123 ::operator delete(ptr);
128 operator delete(void* ptr, size_t) _NOEXCEPT
130 ::operator delete(ptr);
135 operator delete[] (void* ptr) _NOEXCEPT
137 ::operator delete(ptr);
142 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
144 ::operator delete[](ptr);
149 operator delete[] (void* ptr, size_t) _NOEXCEPT
151 ::operator delete[](ptr);
154 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
158 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
162 if (static_cast<size_t>(alignment) < sizeof(void*))
163 alignment = std::align_val_t(sizeof(void*));
165 #if defined(_LIBCPP_MSVCRT)
166 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
168 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
171 // If posix_memalign fails and there is a new_handler,
172 // call it to try free up memory.
173 std::new_handler nh = std::get_new_handler();
177 #ifndef _LIBCPP_NO_EXCEPTIONS
178 throw std::bad_alloc();
180 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
190 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
193 #ifndef _LIBCPP_NO_EXCEPTIONS
196 #endif // _LIBCPP_NO_EXCEPTIONS
197 p = ::operator new(size, alignment);
198 #ifndef _LIBCPP_NO_EXCEPTIONS
203 #endif // _LIBCPP_NO_EXCEPTIONS
209 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
211 return ::operator new(size, alignment);
216 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
219 #ifndef _LIBCPP_NO_EXCEPTIONS
222 #endif // _LIBCPP_NO_EXCEPTIONS
223 p = ::operator new[](size, alignment);
224 #ifndef _LIBCPP_NO_EXCEPTIONS
229 #endif // _LIBCPP_NO_EXCEPTIONS
235 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
238 #if defined(_LIBCPP_MSVCRT)
239 ::_aligned_free(ptr);
247 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
249 ::operator delete(ptr, alignment);
254 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
256 ::operator delete(ptr, alignment);
261 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
263 ::operator delete(ptr, alignment);
268 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
270 ::operator delete[](ptr, alignment);
275 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
277 ::operator delete[](ptr, alignment);
280 #endif // !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
282 #endif // !__GLIBCXX__
288 const nothrow_t nothrow = {};
291 #ifndef _LIBCPPABI_VERSION
296 set_new_handler(new_handler handler) _NOEXCEPT
298 return __sync_lock_test_and_set(&__new_handler, handler);
302 get_new_handler() _NOEXCEPT
304 return __sync_fetch_and_add(&__new_handler, nullptr);
307 #endif // !__GLIBCXX__
311 bad_alloc::bad_alloc() _NOEXCEPT
317 bad_alloc::~bad_alloc() _NOEXCEPT
322 bad_alloc::what() const _NOEXCEPT
324 return "std::bad_alloc";
327 #endif // !__GLIBCXX__
329 bad_array_new_length::bad_array_new_length() _NOEXCEPT
335 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
340 bad_array_new_length::what() const _NOEXCEPT
342 return "bad_array_new_length";
345 #endif // !__GLIBCXX__
349 bad_array_length::bad_array_length() _NOEXCEPT
355 bad_array_length::~bad_array_length() _NOEXCEPT
360 bad_array_length::what() const _NOEXCEPT
362 return "bad_array_length";
365 #endif // !__GLIBCXX__
367 #endif // _LIBCPPABI_VERSION
374 #ifndef _LIBCPP_NO_EXCEPTIONS