1 //===--------------------------- new.cpp ----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 #include "include/atomic_support.h"
14 #if defined(_LIBCPP_ABI_MICROSOFT)
15 # if !defined(_LIBCPP_ABI_VCRUNTIME)
16 # include "support/runtime/new_handler_fallback.ipp"
18 #elif defined(LIBCXX_BUILDING_LIBCXXABI)
20 #elif defined(LIBCXXRT)
22 # include "support/runtime/new_handler_fallback.ipp"
23 #elif defined(__GLIBCXX__)
26 # include "support/runtime/new_handler_fallback.ipp"
33 const nothrow_t nothrow{};
41 #ifndef _LIBCPP_NO_EXCEPTIONS
52 #if !defined(__GLIBCXX__) && \
53 !defined(_LIBCPP_ABI_VCRUNTIME) && \
54 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
56 // Implement all new and delete operators as weak definitions
57 // in this shared library, so that they can be overridden by programs
58 // that define non-weak copies of the functions.
62 operator new(std::size_t size) _THROW_BAD_ALLOC
67 while ((p = ::malloc(size)) == 0)
69 // If malloc fails and there is a new_handler,
70 // call it to try free up memory.
71 std::new_handler nh = std::get_new_handler();
75 #ifndef _LIBCPP_NO_EXCEPTIONS
76 throw std::bad_alloc();
86 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
89 #ifndef _LIBCPP_NO_EXCEPTIONS
92 #endif // _LIBCPP_NO_EXCEPTIONS
93 p = ::operator new(size);
94 #ifndef _LIBCPP_NO_EXCEPTIONS
99 #endif // _LIBCPP_NO_EXCEPTIONS
105 operator new[](size_t size) _THROW_BAD_ALLOC
107 return ::operator new(size);
112 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
115 #ifndef _LIBCPP_NO_EXCEPTIONS
118 #endif // _LIBCPP_NO_EXCEPTIONS
119 p = ::operator new[](size);
120 #ifndef _LIBCPP_NO_EXCEPTIONS
125 #endif // _LIBCPP_NO_EXCEPTIONS
131 operator delete(void* ptr) _NOEXCEPT
138 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
140 ::operator delete(ptr);
145 operator delete(void* ptr, size_t) _NOEXCEPT
147 ::operator delete(ptr);
152 operator delete[] (void* ptr) _NOEXCEPT
154 ::operator delete(ptr);
159 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
161 ::operator delete[](ptr);
166 operator delete[] (void* ptr, size_t) _NOEXCEPT
168 ::operator delete[](ptr);
171 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
175 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
179 if (static_cast<size_t>(alignment) < sizeof(void*))
180 alignment = std::align_val_t(sizeof(void*));
182 #if defined(_LIBCPP_MSVCRT_LIKE)
183 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
185 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
188 // If posix_memalign fails and there is a new_handler,
189 // call it to try free up memory.
190 std::new_handler nh = std::get_new_handler();
194 #ifndef _LIBCPP_NO_EXCEPTIONS
195 throw std::bad_alloc();
197 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
207 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
210 #ifndef _LIBCPP_NO_EXCEPTIONS
213 #endif // _LIBCPP_NO_EXCEPTIONS
214 p = ::operator new(size, alignment);
215 #ifndef _LIBCPP_NO_EXCEPTIONS
220 #endif // _LIBCPP_NO_EXCEPTIONS
226 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
228 return ::operator new(size, alignment);
233 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
236 #ifndef _LIBCPP_NO_EXCEPTIONS
239 #endif // _LIBCPP_NO_EXCEPTIONS
240 p = ::operator new[](size, alignment);
241 #ifndef _LIBCPP_NO_EXCEPTIONS
246 #endif // _LIBCPP_NO_EXCEPTIONS
252 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
254 #if defined(_LIBCPP_MSVCRT_LIKE)
255 ::_aligned_free(ptr);
263 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
265 ::operator delete(ptr, alignment);
270 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
272 ::operator delete(ptr, alignment);
277 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
279 ::operator delete(ptr, alignment);
284 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
286 ::operator delete[](ptr, alignment);
291 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
293 ::operator delete[](ptr, alignment);
296 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
297 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS