1 //===----------------------------------------------------------------------===//
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
)) == nullptr)
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 // Try allocating memory. If allocation fails and there is a new_handler,
183 // call it to try free up memory, and try again until it succeeds, or until
184 // the new_handler decides to terminate.
186 // If allocation fails and there is no new_handler, we throw bad_alloc
187 // (or return nullptr if exceptions are disabled).
189 while ((p
= std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment
), size
)) == nullptr)
191 std::new_handler nh
= std::get_new_handler();
195 #ifndef _LIBCPP_NO_EXCEPTIONS
196 throw std::bad_alloc();
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 std::__libcpp_aligned_free(ptr
);
259 operator delete(void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
261 ::operator delete(ptr
, alignment
);
266 operator delete(void* ptr
, size_t, std::align_val_t alignment
) noexcept
268 ::operator delete(ptr
, alignment
);
273 operator delete[] (void* ptr
, std::align_val_t alignment
) noexcept
275 ::operator delete(ptr
, alignment
);
280 operator delete[] (void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
282 ::operator delete[](ptr
, alignment
);
287 operator delete[] (void* ptr
, size_t, std::align_val_t alignment
) noexcept
289 ::operator delete[](ptr
, alignment
);
292 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
293 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS