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
15 #include "include/atomic_support.h"
17 #if defined(_LIBCPP_ABI_MICROSOFT)
18 #if defined(_LIBCPP_NO_VCRUNTIME)
19 #include "support/runtime/new_handler_fallback.ipp"
21 #elif defined(LIBCXX_BUILDING_LIBCXXABI)
23 #elif defined(LIBCXXRT)
25 #include "support/runtime/new_handler_fallback.ipp"
26 #elif defined(__GLIBCXX__)
29 # if defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
30 # include <cxxabi.h> // FIXME: remove this once buildit is gone.
32 # include "support/runtime/new_handler_fallback.ipp"
40 const nothrow_t nothrow = {};
48 #ifndef _LIBCPP_NO_EXCEPTIONS
59 #if !defined(__GLIBCXX__) && \
60 (!defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)) && \
61 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
63 // Implement all new and delete operators as weak definitions
64 // in this shared library, so that they can be overridden by programs
65 // that define non-weak copies of the functions.
69 operator new(std::size_t size) _THROW_BAD_ALLOC
74 while ((p = ::malloc(size)) == 0)
76 // If malloc fails and there is a new_handler,
77 // call it to try free up memory.
78 std::new_handler nh = std::get_new_handler();
82 #ifndef _LIBCPP_NO_EXCEPTIONS
83 throw std::bad_alloc();
93 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
96 #ifndef _LIBCPP_NO_EXCEPTIONS
99 #endif // _LIBCPP_NO_EXCEPTIONS
100 p = ::operator new(size);
101 #ifndef _LIBCPP_NO_EXCEPTIONS
106 #endif // _LIBCPP_NO_EXCEPTIONS
112 operator new[](size_t size) _THROW_BAD_ALLOC
114 return ::operator new(size);
119 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
122 #ifndef _LIBCPP_NO_EXCEPTIONS
125 #endif // _LIBCPP_NO_EXCEPTIONS
126 p = ::operator new[](size);
127 #ifndef _LIBCPP_NO_EXCEPTIONS
132 #endif // _LIBCPP_NO_EXCEPTIONS
138 operator delete(void* ptr) _NOEXCEPT
146 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
148 ::operator delete(ptr);
153 operator delete(void* ptr, size_t) _NOEXCEPT
155 ::operator delete(ptr);
160 operator delete[] (void* ptr) _NOEXCEPT
162 ::operator delete(ptr);
167 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
169 ::operator delete[](ptr);
174 operator delete[] (void* ptr, size_t) _NOEXCEPT
176 ::operator delete[](ptr);
179 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
183 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
187 if (static_cast<size_t>(alignment) < sizeof(void*))
188 alignment = std::align_val_t(sizeof(void*));
190 #if defined(_LIBCPP_MSVCRT_LIKE)
191 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
193 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
196 // If posix_memalign fails and there is a new_handler,
197 // call it to try free up memory.
198 std::new_handler nh = std::get_new_handler();
202 #ifndef _LIBCPP_NO_EXCEPTIONS
203 throw std::bad_alloc();
205 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
215 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
218 #ifndef _LIBCPP_NO_EXCEPTIONS
221 #endif // _LIBCPP_NO_EXCEPTIONS
222 p = ::operator new(size, alignment);
223 #ifndef _LIBCPP_NO_EXCEPTIONS
228 #endif // _LIBCPP_NO_EXCEPTIONS
234 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
236 return ::operator new(size, alignment);
241 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
244 #ifndef _LIBCPP_NO_EXCEPTIONS
247 #endif // _LIBCPP_NO_EXCEPTIONS
248 p = ::operator new[](size, alignment);
249 #ifndef _LIBCPP_NO_EXCEPTIONS
254 #endif // _LIBCPP_NO_EXCEPTIONS
260 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
263 #if defined(_LIBCPP_MSVCRT_LIKE)
264 ::_aligned_free(ptr);
272 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
274 ::operator delete(ptr, alignment);
279 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
281 ::operator delete(ptr, alignment);
286 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
288 ::operator delete(ptr, alignment);
293 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
295 ::operator delete[](ptr, alignment);
300 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
302 ::operator delete[](ptr, alignment);
305 #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
306 #endif // !__GLIBCXX__ && (!_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME) && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS