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 //===----------------------------------------------------------------------===//
9 #include "__cxxabi_config.h"
10 #include <__memory/aligned_alloc.h>
14 // Perform a few sanity checks on libc++ and libc++abi macros to ensure that
15 // the code below can be an exact copy of the code in libcxx/src/new.cpp.
16 #if !defined(_THROW_BAD_ALLOC)
17 # error The _THROW_BAD_ALLOC macro should be already defined by libc++
21 # error The _LIBCPP_WEAK macro should be already defined by libc++
24 #if defined(_LIBCXXABI_NO_EXCEPTIONS) != defined(_LIBCPP_HAS_NO_EXCEPTIONS)
25 # error libc++ and libc++abi seem to disagree on whether exceptions are enabled
28 // ------------------ BEGIN COPY ------------------
29 // Implement all new and delete operators as weak definitions
30 // in this shared library, so that they can be overridden by programs
31 // that define non-weak copies of the functions.
35 operator new(std::size_t size
) _THROW_BAD_ALLOC
40 while ((p
= std::malloc(size
)) == nullptr)
42 // If malloc fails and there is a new_handler,
43 // call it to try free up memory.
44 std::new_handler nh
= std::get_new_handler();
48 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
49 throw std::bad_alloc();
59 operator new(size_t size
, const std::nothrow_t
&) noexcept
62 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
65 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
66 p
= ::operator new(size
);
67 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
72 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
78 operator new[](size_t size
) _THROW_BAD_ALLOC
80 return ::operator new(size
);
85 operator new[](size_t size
, const std::nothrow_t
&) noexcept
88 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
91 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
92 p
= ::operator new[](size
);
93 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
98 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
104 operator delete(void* ptr
) noexcept
111 operator delete(void* ptr
, const std::nothrow_t
&) noexcept
113 ::operator delete(ptr
);
118 operator delete(void* ptr
, size_t) noexcept
120 ::operator delete(ptr
);
125 operator delete[] (void* ptr
) noexcept
127 ::operator delete(ptr
);
132 operator delete[] (void* ptr
, const std::nothrow_t
&) noexcept
134 ::operator delete[](ptr
);
139 operator delete[] (void* ptr
, size_t) noexcept
141 ::operator delete[](ptr
);
144 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
148 operator new(std::size_t size
, std::align_val_t alignment
) _THROW_BAD_ALLOC
152 if (static_cast<size_t>(alignment
) < sizeof(void*))
153 alignment
= std::align_val_t(sizeof(void*));
155 // Try allocating memory. If allocation fails and there is a new_handler,
156 // call it to try free up memory, and try again until it succeeds, or until
157 // the new_handler decides to terminate.
159 // If allocation fails and there is no new_handler, we throw bad_alloc
160 // (or return nullptr if exceptions are disabled).
162 while ((p
= std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment
), size
)) == nullptr)
164 std::new_handler nh
= std::get_new_handler();
168 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
169 throw std::bad_alloc();
180 operator new(size_t size
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
183 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
186 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
187 p
= ::operator new(size
, alignment
);
188 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
193 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
199 operator new[](size_t size
, std::align_val_t alignment
) _THROW_BAD_ALLOC
201 return ::operator new(size
, alignment
);
206 operator new[](size_t size
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
209 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
212 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
213 p
= ::operator new[](size
, alignment
);
214 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
219 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
225 operator delete(void* ptr
, std::align_val_t
) noexcept
227 std::__libcpp_aligned_free(ptr
);
232 operator delete(void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
234 ::operator delete(ptr
, alignment
);
239 operator delete(void* ptr
, size_t, std::align_val_t alignment
) noexcept
241 ::operator delete(ptr
, alignment
);
246 operator delete[] (void* ptr
, std::align_val_t alignment
) noexcept
248 ::operator delete(ptr
, alignment
);
253 operator delete[] (void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
255 ::operator delete[](ptr
, alignment
);
260 operator delete[] (void* ptr
, size_t, std::align_val_t alignment
) noexcept
262 ::operator delete[](ptr
, alignment
);
265 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
266 // ------------------ END COPY ------------------