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(_LIBCPP_ABI_MICROSOFT)
18 #elif defined(LIBCXX_BUILDING_LIBCXXABI)
20 #elif defined(LIBCXXRT)
22 #include "support/runtime/new_handler_fallback.ipp"
23 #elif defined(__GLIBCXX__)
26 # if defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
27 # include <cxxabi.h> // FIXME: remove this once buildit is gone.
29 # include "support/runtime/new_handler_fallback.ipp"
37 const nothrow_t nothrow = {};
45 #ifndef _LIBCPP_NO_EXCEPTIONS
56 #if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_MICROSOFT) && \
57 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
59 // Implement all new and delete operators as weak definitions
60 // in this shared library, so that they can be overridden by programs
61 // that define non-weak copies of the functions.
65 operator new(std::size_t size) _THROW_BAD_ALLOC
70 while ((p = ::malloc(size)) == 0)
72 // If malloc fails and there is a new_handler,
73 // call it to try free up memory.
74 std::new_handler nh = std::get_new_handler();
78 #ifndef _LIBCPP_NO_EXCEPTIONS
79 throw std::bad_alloc();
89 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
92 #ifndef _LIBCPP_NO_EXCEPTIONS
95 #endif // _LIBCPP_NO_EXCEPTIONS
96 p = ::operator new(size);
97 #ifndef _LIBCPP_NO_EXCEPTIONS
102 #endif // _LIBCPP_NO_EXCEPTIONS
108 operator new[](size_t size) _THROW_BAD_ALLOC
110 return ::operator new(size);
115 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
118 #ifndef _LIBCPP_NO_EXCEPTIONS
121 #endif // _LIBCPP_NO_EXCEPTIONS
122 p = ::operator new[](size);
123 #ifndef _LIBCPP_NO_EXCEPTIONS
128 #endif // _LIBCPP_NO_EXCEPTIONS
134 operator delete(void* ptr) _NOEXCEPT
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);
156 operator delete[] (void* ptr) _NOEXCEPT
158 ::operator delete(ptr);
163 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
165 ::operator delete[](ptr);
170 operator delete[] (void* ptr, size_t) _NOEXCEPT
172 ::operator delete[](ptr);
175 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
179 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
183 if (static_cast<size_t>(alignment) < sizeof(void*))
184 alignment = std::align_val_t(sizeof(void*));
186 #if defined(_LIBCPP_MSVCRT)
187 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
189 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
192 // If posix_memalign fails and there is a new_handler,
193 // call it to try free up memory.
194 std::new_handler nh = std::get_new_handler();
198 #ifndef _LIBCPP_NO_EXCEPTIONS
199 throw std::bad_alloc();
201 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
211 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
214 #ifndef _LIBCPP_NO_EXCEPTIONS
217 #endif // _LIBCPP_NO_EXCEPTIONS
218 p = ::operator new(size, alignment);
219 #ifndef _LIBCPP_NO_EXCEPTIONS
224 #endif // _LIBCPP_NO_EXCEPTIONS
230 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
232 return ::operator new(size, alignment);
237 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
240 #ifndef _LIBCPP_NO_EXCEPTIONS
243 #endif // _LIBCPP_NO_EXCEPTIONS
244 p = ::operator new[](size, alignment);
245 #ifndef _LIBCPP_NO_EXCEPTIONS
250 #endif // _LIBCPP_NO_EXCEPTIONS
256 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
259 #if defined(_LIBCPP_MSVCRT)
260 ::_aligned_free(ptr);
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);
282 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
284 ::operator delete(ptr, alignment);
289 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
291 ::operator delete[](ptr, alignment);
296 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
298 ::operator delete[](ptr, alignment);
301 #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
302 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS