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
)) == 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
139 operator delete(void* ptr
, const std::nothrow_t
&) _NOEXCEPT
141 ::operator delete(ptr
);
146 operator delete(void* ptr
, size_t) _NOEXCEPT
148 ::operator delete(ptr
);
153 operator delete[] (void* ptr
) _NOEXCEPT
155 ::operator delete(ptr
);
160 operator delete[] (void* ptr
, const std::nothrow_t
&) _NOEXCEPT
162 ::operator delete[](ptr
);
167 operator delete[] (void* ptr
, size_t) _NOEXCEPT
169 ::operator delete[](ptr
);
172 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
176 operator new(std::size_t size
, std::align_val_t alignment
) _THROW_BAD_ALLOC
180 if (static_cast<size_t>(alignment
) < sizeof(void*))
181 alignment
= std::align_val_t(sizeof(void*));
183 // Try allocating memory. If allocation fails and there is a new_handler,
184 // call it to try free up memory, and try again until it succeeds, or until
185 // the new_handler decides to terminate.
187 // If allocation fails and there is no new_handler, we throw bad_alloc
188 // (or return nullptr if exceptions are disabled).
190 while ((p
= std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment
), size
)) == nullptr)
192 std::new_handler nh
= std::get_new_handler();
196 #ifndef _LIBCPP_NO_EXCEPTIONS
197 throw std::bad_alloc();
208 operator new(size_t size
, std::align_val_t alignment
, const std::nothrow_t
&) _NOEXCEPT
211 #ifndef _LIBCPP_NO_EXCEPTIONS
214 #endif // _LIBCPP_NO_EXCEPTIONS
215 p
= ::operator new(size
, alignment
);
216 #ifndef _LIBCPP_NO_EXCEPTIONS
221 #endif // _LIBCPP_NO_EXCEPTIONS
227 operator new[](size_t size
, std::align_val_t alignment
) _THROW_BAD_ALLOC
229 return ::operator new(size
, alignment
);
234 operator new[](size_t size
, std::align_val_t alignment
, const std::nothrow_t
&) _NOEXCEPT
237 #ifndef _LIBCPP_NO_EXCEPTIONS
240 #endif // _LIBCPP_NO_EXCEPTIONS
241 p
= ::operator new[](size
, alignment
);
242 #ifndef _LIBCPP_NO_EXCEPTIONS
247 #endif // _LIBCPP_NO_EXCEPTIONS
253 operator delete(void* ptr
, std::align_val_t
) _NOEXCEPT
256 std::__libcpp_aligned_free(ptr
);
262 operator delete(void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) _NOEXCEPT
264 ::operator delete(ptr
, alignment
);
269 operator delete(void* ptr
, size_t, std::align_val_t alignment
) _NOEXCEPT
271 ::operator delete(ptr
, alignment
);
276 operator delete[] (void* ptr
, std::align_val_t alignment
) _NOEXCEPT
278 ::operator delete(ptr
, alignment
);
283 operator delete[] (void* ptr
, std::align_val_t alignment
, const std::nothrow_t
&) _NOEXCEPT
285 ::operator delete[](ptr
, alignment
);
290 operator delete[] (void* ptr
, size_t, std::align_val_t alignment
) _NOEXCEPT
292 ::operator delete[](ptr
, alignment
);
295 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
296 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS