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 <__memory/aligned_alloc.h>
13 #if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_VCRUNTIME)
15 // The code below is copied as-is into libc++abi's libcxxabi/src/stdlib_new_delete.cpp
16 // file. The version in this file is the canonical one.
18 // ------------------ BEGIN COPY ------------------
19 // Implement all new and delete operators as weak definitions
20 // in this shared library, so that they can be overridden by programs
21 // that define non-weak copies of the functions.
23 static void* operator_new_impl(std::size_t size
) noexcept
{
27 while ((p
= std::malloc(size
)) == nullptr) {
28 // If malloc fails and there is a new_handler,
29 // call it to try free up memory.
30 std::new_handler nh
= std::get_new_handler();
39 _LIBCPP_WEAK
void* operator new(std::size_t size
) _THROW_BAD_ALLOC
{
40 void* p
= operator_new_impl(size
);
41 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
43 throw std::bad_alloc();
48 _LIBCPP_WEAK
void* operator new(size_t size
, const std::nothrow_t
&) noexcept
{
50 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
52 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
53 p
= ::operator new(size
);
54 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
57 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
61 _LIBCPP_WEAK
void* operator new[](size_t size
) _THROW_BAD_ALLOC
{ return ::operator new(size
); }
63 _LIBCPP_WEAK
void* operator new[](size_t size
, const std::nothrow_t
&) noexcept
{
65 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
67 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
68 p
= ::operator new[](size
);
69 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
72 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
76 _LIBCPP_WEAK
void operator delete(void* ptr
) noexcept
{ std::free(ptr
); }
78 _LIBCPP_WEAK
void operator delete(void* ptr
, const std::nothrow_t
&) noexcept
{ ::operator delete(ptr
); }
80 _LIBCPP_WEAK
void operator delete(void* ptr
, size_t) noexcept
{ ::operator delete(ptr
); }
82 _LIBCPP_WEAK
void operator delete[](void* ptr
) noexcept
{ ::operator delete(ptr
); }
84 _LIBCPP_WEAK
void operator delete[](void* ptr
, const std::nothrow_t
&) noexcept
{ ::operator delete[](ptr
); }
86 _LIBCPP_WEAK
void operator delete[](void* ptr
, size_t) noexcept
{ ::operator delete[](ptr
); }
88 # if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
90 static void* operator_new_aligned_impl(std::size_t size
, std::align_val_t alignment
) noexcept
{
93 if (static_cast<size_t>(alignment
) < sizeof(void*))
94 alignment
= std::align_val_t(sizeof(void*));
96 // Try allocating memory. If allocation fails and there is a new_handler,
97 // call it to try free up memory, and try again until it succeeds, or until
98 // the new_handler decides to terminate.
100 while ((p
= std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment
), size
)) == nullptr) {
101 std::new_handler nh
= std::get_new_handler();
110 _LIBCPP_WEAK
void* operator new(std::size_t size
, std::align_val_t alignment
) _THROW_BAD_ALLOC
{
111 void* p
= operator_new_aligned_impl(size
, alignment
);
112 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
114 throw std::bad_alloc();
119 _LIBCPP_WEAK
void* operator new(size_t size
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
{
121 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
123 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
124 p
= ::operator new(size
, alignment
);
125 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
128 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
132 _LIBCPP_WEAK
void* operator new[](size_t size
, std::align_val_t alignment
) _THROW_BAD_ALLOC
{
133 return ::operator new(size
, alignment
);
136 _LIBCPP_WEAK
void* operator new[](size_t size
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
{
138 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
140 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
141 p
= ::operator new[](size
, alignment
);
142 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
145 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
149 _LIBCPP_WEAK
void operator delete(void* ptr
, std::align_val_t
) noexcept
{ std::__libcpp_aligned_free(ptr
); }
151 _LIBCPP_WEAK
void operator delete(void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
{
152 ::operator delete(ptr
, alignment
);
155 _LIBCPP_WEAK
void operator delete(void* ptr
, size_t, std::align_val_t alignment
) noexcept
{
156 ::operator delete(ptr
, alignment
);
159 _LIBCPP_WEAK
void operator delete[](void* ptr
, std::align_val_t alignment
) noexcept
{
160 ::operator delete(ptr
, alignment
);
163 _LIBCPP_WEAK
void operator delete[](void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
{
164 ::operator delete[](ptr
, alignment
);
167 _LIBCPP_WEAK
void operator delete[](void* ptr
, size_t, std::align_val_t alignment
) noexcept
{
168 ::operator delete[](ptr
, alignment
);
171 # endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
172 // ------------------ END COPY ------------------
174 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME