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 //===----------------------------------------------------------------------===//
13 #include "include/atomic_support.h"
15 #if defined(_LIBCPP_ABI_MICROSOFT)
16 #if defined(_LIBCPP_NO_VCRUNTIME)
17 #include "support/runtime/new_handler_fallback.ipp"
19 #elif defined(LIBCXX_BUILDING_LIBCXXABI)
21 #elif defined(LIBCXXRT)
23 #include "support/runtime/new_handler_fallback.ipp"
24 #elif defined(__GLIBCXX__)
27 # if defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
28 # include <cxxabi.h> // FIXME: remove this once buildit is gone.
30 # include "support/runtime/new_handler_fallback.ipp"
38 const nothrow_t nothrow = {};
46 #ifndef _LIBCPP_NO_EXCEPTIONS
57 #if !defined(__GLIBCXX__) && \
58 (!defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)) && \
59 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
61 // Implement all new and delete operators as weak definitions
62 // in this shared library, so that they can be overridden by programs
63 // that define non-weak copies of the functions.
67 operator new(std::size_t size) _THROW_BAD_ALLOC
72 while ((p = ::malloc(size)) == 0)
74 // If malloc fails and there is a new_handler,
75 // call it to try free up memory.
76 std::new_handler nh = std::get_new_handler();
80 #ifndef _LIBCPP_NO_EXCEPTIONS
81 throw std::bad_alloc();
91 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
94 #ifndef _LIBCPP_NO_EXCEPTIONS
97 #endif // _LIBCPP_NO_EXCEPTIONS
98 p = ::operator new(size);
99 #ifndef _LIBCPP_NO_EXCEPTIONS
104 #endif // _LIBCPP_NO_EXCEPTIONS
110 operator new[](size_t size) _THROW_BAD_ALLOC
112 return ::operator new(size);
117 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
120 #ifndef _LIBCPP_NO_EXCEPTIONS
123 #endif // _LIBCPP_NO_EXCEPTIONS
124 p = ::operator new[](size);
125 #ifndef _LIBCPP_NO_EXCEPTIONS
130 #endif // _LIBCPP_NO_EXCEPTIONS
136 operator delete(void* ptr) _NOEXCEPT
144 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
146 ::operator delete(ptr);
151 operator delete(void* ptr, size_t) _NOEXCEPT
153 ::operator delete(ptr);
158 operator delete[] (void* ptr) _NOEXCEPT
160 ::operator delete(ptr);
165 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
167 ::operator delete[](ptr);
172 operator delete[] (void* ptr, size_t) _NOEXCEPT
174 ::operator delete[](ptr);
177 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
181 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
185 if (static_cast<size_t>(alignment) < sizeof(void*))
186 alignment = std::align_val_t(sizeof(void*));
188 #if defined(_LIBCPP_MSVCRT_LIKE)
189 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
191 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
194 // If posix_memalign fails and there is a new_handler,
195 // call it to try free up memory.
196 std::new_handler nh = std::get_new_handler();
200 #ifndef _LIBCPP_NO_EXCEPTIONS
201 throw std::bad_alloc();
203 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
213 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
216 #ifndef _LIBCPP_NO_EXCEPTIONS
219 #endif // _LIBCPP_NO_EXCEPTIONS
220 p = ::operator new(size, alignment);
221 #ifndef _LIBCPP_NO_EXCEPTIONS
226 #endif // _LIBCPP_NO_EXCEPTIONS
232 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
234 return ::operator new(size, alignment);
239 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
242 #ifndef _LIBCPP_NO_EXCEPTIONS
245 #endif // _LIBCPP_NO_EXCEPTIONS
246 p = ::operator new[](size, alignment);
247 #ifndef _LIBCPP_NO_EXCEPTIONS
252 #endif // _LIBCPP_NO_EXCEPTIONS
258 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
261 #if defined(_LIBCPP_MSVCRT_LIKE)
262 ::_aligned_free(ptr);
270 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
272 ::operator delete(ptr, alignment);
277 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
279 ::operator delete(ptr, alignment);
284 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
286 ::operator delete(ptr, alignment);
291 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
293 ::operator delete[](ptr, alignment);
298 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
300 ::operator delete[](ptr, alignment);
303 #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
304 #endif // !__GLIBCXX__ && (!_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME) && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS