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 "include/overridable_function.h"
10 #include <__memory/aligned_alloc.h>
15 #if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_VCRUNTIME)
17 // The code below is copied as-is into libc++abi's libcxxabi/src/stdlib_new_delete.cpp
18 // file. The version in this file is the canonical one.
20 inline void __throw_bad_alloc_shim() { std::__throw_bad_alloc(); }
22 # define _LIBCPP_ASSERT_SHIM(expr, str) _LIBCPP_ASSERT(expr, str)
24 // ------------------ BEGIN COPY ------------------
25 // Implement all new and delete operators as weak definitions
26 // in this shared library, so that they can be overridden by programs
27 // that define non-weak copies of the functions.
29 static void* operator_new_impl(std::size_t size
) {
33 while ((p
= std::malloc(size
)) == nullptr) {
34 // If malloc fails and there is a new_handler,
35 // call it to try free up memory.
36 std::new_handler nh
= std::get_new_handler();
45 _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK
void* operator new(std::size_t size
) _THROW_BAD_ALLOC
{
46 void* p
= operator_new_impl(size
);
48 __throw_bad_alloc_shim();
52 _LIBCPP_WEAK
void* operator new(size_t size
, const std::nothrow_t
&) noexcept
{
53 # ifdef _LIBCPP_HAS_NO_EXCEPTIONS
54 # if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
56 !std::__is_function_overridden(static_cast<void* (*)(std::size_t)>(&operator new)),
57 "libc++ was configured with exceptions disabled and `operator new(size_t)` has been overridden, "
58 "but `operator new(size_t, nothrow_t)` has not been overridden. This is problematic because "
59 "`operator new(size_t, nothrow_t)` must call `operator new(size_t)`, which will terminate in case "
60 "it fails to allocate, making it impossible for `operator new(size_t, nothrow_t)` to fulfill its "
61 "contract (since it should return nullptr upon failure). Please make sure you override "
62 "`operator new(size_t, nothrow_t)` as well.");
65 return operator_new_impl(size
);
69 p
= ::operator new(size
);
76 _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK
void* operator new[](size_t size
) _THROW_BAD_ALLOC
{
77 return ::operator new(size
);
80 _LIBCPP_WEAK
void* operator new[](size_t size
, const std::nothrow_t
&) noexcept
{
81 # ifdef _LIBCPP_HAS_NO_EXCEPTIONS
82 # if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
84 !std::__is_function_overridden(static_cast<void* (*)(std::size_t)>(&operator new[])),
85 "libc++ was configured with exceptions disabled and `operator new[](size_t)` has been overridden, "
86 "but `operator new[](size_t, nothrow_t)` has not been overridden. This is problematic because "
87 "`operator new[](size_t, nothrow_t)` must call `operator new[](size_t)`, which will terminate in case "
88 "it fails to allocate, making it impossible for `operator new[](size_t, nothrow_t)` to fulfill its "
89 "contract (since it should return nullptr upon failure). Please make sure you override "
90 "`operator new[](size_t, nothrow_t)` as well.");
93 return operator_new_impl(size
);
97 p
= ::operator new[](size
);
104 _LIBCPP_WEAK
void operator delete(void* ptr
) noexcept
{ std::free(ptr
); }
106 _LIBCPP_WEAK
void operator delete(void* ptr
, const std::nothrow_t
&) noexcept
{ ::operator delete(ptr
); }
108 _LIBCPP_WEAK
void operator delete(void* ptr
, size_t) noexcept
{ ::operator delete(ptr
); }
110 _LIBCPP_WEAK
void operator delete[](void* ptr
) noexcept
{ ::operator delete(ptr
); }
112 _LIBCPP_WEAK
void operator delete[](void* ptr
, const std::nothrow_t
&) noexcept
{ ::operator delete[](ptr
); }
114 _LIBCPP_WEAK
void operator delete[](void* ptr
, size_t) noexcept
{ ::operator delete[](ptr
); }
116 # if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
118 static void* operator_new_aligned_impl(std::size_t size
, std::align_val_t alignment
) {
121 if (static_cast<size_t>(alignment
) < sizeof(void*))
122 alignment
= std::align_val_t(sizeof(void*));
124 // Try allocating memory. If allocation fails and there is a new_handler,
125 // call it to try free up memory, and try again until it succeeds, or until
126 // the new_handler decides to terminate.
128 while ((p
= std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment
), size
)) == nullptr) {
129 std::new_handler nh
= std::get_new_handler();
138 _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK
void*
139 operator new(std::size_t size
, std::align_val_t alignment
) _THROW_BAD_ALLOC
{
140 void* p
= operator_new_aligned_impl(size
, alignment
);
142 __throw_bad_alloc_shim();
146 _LIBCPP_WEAK
void* operator new(size_t size
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
{
147 # ifdef _LIBCPP_HAS_NO_EXCEPTIONS
148 # if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
150 !std::__is_function_overridden(static_cast<void* (*)(std::size_t, std::align_val_t
)>(&operator new)),
151 "libc++ was configured with exceptions disabled and `operator new(size_t, align_val_t)` has been overridden, "
152 "but `operator new(size_t, align_val_t, nothrow_t)` has not been overridden. This is problematic because "
153 "`operator new(size_t, align_val_t, nothrow_t)` must call `operator new(size_t, align_val_t)`, which will "
154 "terminate in case it fails to allocate, making it impossible for `operator new(size_t, align_val_t, nothrow_t)` "
155 "to fulfill its contract (since it should return nullptr upon failure). Please make sure you override "
156 "`operator new(size_t, align_val_t, nothrow_t)` as well.");
159 return operator_new_aligned_impl(size
, alignment
);
163 p
= ::operator new(size
, alignment
);
170 _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK
void*
171 operator new[](size_t size
, std::align_val_t alignment
) _THROW_BAD_ALLOC
{
172 return ::operator new(size
, alignment
);
175 _LIBCPP_WEAK
void* operator new[](size_t size
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
{
176 # ifdef _LIBCPP_HAS_NO_EXCEPTIONS
177 # if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
179 !std::__is_function_overridden(static_cast<void* (*)(std::size_t, std::align_val_t
)>(&operator new[])),
180 "libc++ was configured with exceptions disabled and `operator new[](size_t, align_val_t)` has been overridden, "
181 "but `operator new[](size_t, align_val_t, nothrow_t)` has not been overridden. This is problematic because "
182 "`operator new[](size_t, align_val_t, nothrow_t)` must call `operator new[](size_t, align_val_t)`, which will "
183 "terminate in case it fails to allocate, making it impossible for `operator new[](size_t, align_val_t, "
184 "nothrow_t)` to fulfill its contract (since it should return nullptr upon failure). Please make sure you "
186 "`operator new[](size_t, align_val_t, nothrow_t)` as well.");
189 return operator_new_aligned_impl(size
, alignment
);
193 p
= ::operator new[](size
, alignment
);
200 _LIBCPP_WEAK
void operator delete(void* ptr
, std::align_val_t
) noexcept
{ std::__libcpp_aligned_free(ptr
); }
202 _LIBCPP_WEAK
void operator delete(void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
{
203 ::operator delete(ptr
, alignment
);
206 _LIBCPP_WEAK
void operator delete(void* ptr
, size_t, std::align_val_t alignment
) noexcept
{
207 ::operator delete(ptr
, alignment
);
210 _LIBCPP_WEAK
void operator delete[](void* ptr
, std::align_val_t alignment
) noexcept
{
211 ::operator delete(ptr
, alignment
);
214 _LIBCPP_WEAK
void operator delete[](void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) noexcept
{
215 ::operator delete[](ptr
, alignment
);
218 _LIBCPP_WEAK
void operator delete[](void* ptr
, size_t, std::align_val_t alignment
) noexcept
{
219 ::operator delete[](ptr
, alignment
);
222 # endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
223 // ------------------ END COPY ------------------
225 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME