[NFC][RemoveDIs] Prefer iterators over inst-pointers in InstCombine
[llvm-project.git] / libcxx / src / new.cpp
blobc435c5ffc809c6144b773b2739c6fa21734f505e
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include <__memory/aligned_alloc.h>
10 #include <cstdlib>
11 #include <new>
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 _LIBCPP_WEAK
24 void *
25 operator new(std::size_t size) _THROW_BAD_ALLOC
27 if (size == 0)
28 size = 1;
29 void* p;
30 while ((p = std::malloc(size)) == nullptr)
32 // If malloc fails and there is a new_handler,
33 // call it to try free up memory.
34 std::new_handler nh = std::get_new_handler();
35 if (nh)
36 nh();
37 else
38 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
39 throw std::bad_alloc();
40 #else
41 break;
42 #endif
44 return p;
47 _LIBCPP_WEAK
48 void*
49 operator new(size_t size, const std::nothrow_t&) noexcept
51 void* p = nullptr;
52 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
53 try
55 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
56 p = ::operator new(size);
57 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
59 catch (...)
62 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
63 return p;
66 _LIBCPP_WEAK
67 void*
68 operator new[](size_t size) _THROW_BAD_ALLOC
70 return ::operator new(size);
73 _LIBCPP_WEAK
74 void*
75 operator new[](size_t size, const std::nothrow_t&) noexcept
77 void* p = nullptr;
78 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
79 try
81 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
82 p = ::operator new[](size);
83 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
85 catch (...)
88 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
89 return p;
92 _LIBCPP_WEAK
93 void
94 operator delete(void* ptr) noexcept
96 std::free(ptr);
99 _LIBCPP_WEAK
100 void
101 operator delete(void* ptr, const std::nothrow_t&) noexcept
103 ::operator delete(ptr);
106 _LIBCPP_WEAK
107 void
108 operator delete(void* ptr, size_t) noexcept
110 ::operator delete(ptr);
113 _LIBCPP_WEAK
114 void
115 operator delete[] (void* ptr) noexcept
117 ::operator delete(ptr);
120 _LIBCPP_WEAK
121 void
122 operator delete[] (void* ptr, const std::nothrow_t&) noexcept
124 ::operator delete[](ptr);
127 _LIBCPP_WEAK
128 void
129 operator delete[] (void* ptr, size_t) noexcept
131 ::operator delete[](ptr);
134 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
136 _LIBCPP_WEAK
137 void *
138 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
140 if (size == 0)
141 size = 1;
142 if (static_cast<size_t>(alignment) < sizeof(void*))
143 alignment = std::align_val_t(sizeof(void*));
145 // Try allocating memory. If allocation fails and there is a new_handler,
146 // call it to try free up memory, and try again until it succeeds, or until
147 // the new_handler decides to terminate.
149 // If allocation fails and there is no new_handler, we throw bad_alloc
150 // (or return nullptr if exceptions are disabled).
151 void* p;
152 while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr)
154 std::new_handler nh = std::get_new_handler();
155 if (nh)
156 nh();
157 else {
158 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
159 throw std::bad_alloc();
160 #else
161 break;
162 #endif
165 return p;
168 _LIBCPP_WEAK
169 void*
170 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
172 void* p = nullptr;
173 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
176 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
177 p = ::operator new(size, alignment);
178 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
180 catch (...)
183 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
184 return p;
187 _LIBCPP_WEAK
188 void*
189 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
191 return ::operator new(size, alignment);
194 _LIBCPP_WEAK
195 void*
196 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
198 void* p = nullptr;
199 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
202 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
203 p = ::operator new[](size, alignment);
204 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
206 catch (...)
209 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
210 return p;
213 _LIBCPP_WEAK
214 void
215 operator delete(void* ptr, std::align_val_t) noexcept
217 std::__libcpp_aligned_free(ptr);
220 _LIBCPP_WEAK
221 void
222 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
224 ::operator delete(ptr, alignment);
227 _LIBCPP_WEAK
228 void
229 operator delete(void* ptr, size_t, std::align_val_t alignment) noexcept
231 ::operator delete(ptr, alignment);
234 _LIBCPP_WEAK
235 void
236 operator delete[] (void* ptr, std::align_val_t alignment) noexcept
238 ::operator delete(ptr, alignment);
241 _LIBCPP_WEAK
242 void
243 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
245 ::operator delete[](ptr, alignment);
248 _LIBCPP_WEAK
249 void
250 operator delete[] (void* ptr, size_t, std::align_val_t alignment) noexcept
252 ::operator delete[](ptr, alignment);
255 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
256 // ------------------ END COPY ------------------
258 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME