xref: /llvm-project-15.0.7/libcxx/src/new.cpp (revision 2230bf99)
1 //===--------------------------- new.cpp ----------------------------------===//
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 //===----------------------------------------------------------------------===//
8 
9 #include <stdlib.h>
10 
11 #include "new"
12 #include "include/atomic_support.h"
13 
14 #if defined(_LIBCPP_ABI_MICROSOFT)
15 #   if !defined(_LIBCPP_ABI_VCRUNTIME)
16 #       include "support/runtime/new_handler_fallback.ipp"
17 #   endif
18 #elif defined(LIBCXX_BUILDING_LIBCXXABI)
19 #   include <cxxabi.h>
20 #elif defined(LIBCXXRT)
21 #   include <cxxabi.h>
22 #   include "support/runtime/new_handler_fallback.ipp"
23 #elif defined(__GLIBCXX__)
24     // nothing to do
25 #else
26 #   include "support/runtime/new_handler_fallback.ipp"
27 #endif
28 
29 namespace std
30 {
31 
32 #ifndef __GLIBCXX__
33 const nothrow_t nothrow{};
34 #endif
35 
36 #ifndef LIBSTDCXX
37 
38 void
39 __throw_bad_alloc()
40 {
41 #ifndef _LIBCPP_NO_EXCEPTIONS
42     throw bad_alloc();
43 #else
44     _VSTD::abort();
45 #endif
46 }
47 
48 #endif // !LIBSTDCXX
49 
50 }  // std
51 
52 #if !defined(__GLIBCXX__) &&                                                   \
53     !defined(_LIBCPP_ABI_VCRUNTIME) &&      \
54     !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
55 
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.
59 
60 _LIBCPP_WEAK
61 void *
62 operator new(std::size_t size) _THROW_BAD_ALLOC
63 {
64     if (size == 0)
65         size = 1;
66     void* p;
67     while ((p = ::malloc(size)) == nullptr)
68     {
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();
72         if (nh)
73             nh();
74         else
75 #ifndef _LIBCPP_NO_EXCEPTIONS
76             throw std::bad_alloc();
77 #else
78             break;
79 #endif
80     }
81     return p;
82 }
83 
84 _LIBCPP_WEAK
85 void*
86 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
87 {
88     void* p = nullptr;
89 #ifndef _LIBCPP_NO_EXCEPTIONS
90     try
91     {
92 #endif  // _LIBCPP_NO_EXCEPTIONS
93         p = ::operator new(size);
94 #ifndef _LIBCPP_NO_EXCEPTIONS
95     }
96     catch (...)
97     {
98     }
99 #endif  // _LIBCPP_NO_EXCEPTIONS
100     return p;
101 }
102 
103 _LIBCPP_WEAK
104 void*
105 operator new[](size_t size) _THROW_BAD_ALLOC
106 {
107     return ::operator new(size);
108 }
109 
110 _LIBCPP_WEAK
111 void*
112 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
113 {
114     void* p = nullptr;
115 #ifndef _LIBCPP_NO_EXCEPTIONS
116     try
117     {
118 #endif  // _LIBCPP_NO_EXCEPTIONS
119         p = ::operator new[](size);
120 #ifndef _LIBCPP_NO_EXCEPTIONS
121     }
122     catch (...)
123     {
124     }
125 #endif  // _LIBCPP_NO_EXCEPTIONS
126     return p;
127 }
128 
129 _LIBCPP_WEAK
130 void
131 operator delete(void* ptr) _NOEXCEPT
132 {
133     if (ptr)
134         ::free(ptr);
135 }
136 
137 _LIBCPP_WEAK
138 void
139 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
140 {
141     ::operator delete(ptr);
142 }
143 
144 _LIBCPP_WEAK
145 void
146 operator delete(void* ptr, size_t) _NOEXCEPT
147 {
148     ::operator delete(ptr);
149 }
150 
151 _LIBCPP_WEAK
152 void
153 operator delete[] (void* ptr) _NOEXCEPT
154 {
155     ::operator delete(ptr);
156 }
157 
158 _LIBCPP_WEAK
159 void
160 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
161 {
162     ::operator delete[](ptr);
163 }
164 
165 _LIBCPP_WEAK
166 void
167 operator delete[] (void* ptr, size_t) _NOEXCEPT
168 {
169     ::operator delete[](ptr);
170 }
171 
172 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
173 
174 _LIBCPP_WEAK
175 void *
176 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
177 {
178     if (size == 0)
179         size = 1;
180     if (static_cast<size_t>(alignment) < sizeof(void*))
181       alignment = std::align_val_t(sizeof(void*));
182 
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.
186     //
187     // If allocation fails and there is no new_handler, we throw bad_alloc
188     // (or return nullptr if exceptions are disabled).
189     void* p;
190     while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr)
191     {
192         std::new_handler nh = std::get_new_handler();
193         if (nh)
194             nh();
195         else {
196 #ifndef _LIBCPP_NO_EXCEPTIONS
197             throw std::bad_alloc();
198 #else
199             break;
200 #endif
201         }
202     }
203     return p;
204 }
205 
206 _LIBCPP_WEAK
207 void*
208 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
209 {
210     void* p = nullptr;
211 #ifndef _LIBCPP_NO_EXCEPTIONS
212     try
213     {
214 #endif  // _LIBCPP_NO_EXCEPTIONS
215         p = ::operator new(size, alignment);
216 #ifndef _LIBCPP_NO_EXCEPTIONS
217     }
218     catch (...)
219     {
220     }
221 #endif  // _LIBCPP_NO_EXCEPTIONS
222     return p;
223 }
224 
225 _LIBCPP_WEAK
226 void*
227 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
228 {
229     return ::operator new(size, alignment);
230 }
231 
232 _LIBCPP_WEAK
233 void*
234 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
235 {
236     void* p = nullptr;
237 #ifndef _LIBCPP_NO_EXCEPTIONS
238     try
239     {
240 #endif  // _LIBCPP_NO_EXCEPTIONS
241         p = ::operator new[](size, alignment);
242 #ifndef _LIBCPP_NO_EXCEPTIONS
243     }
244     catch (...)
245     {
246     }
247 #endif  // _LIBCPP_NO_EXCEPTIONS
248     return p;
249 }
250 
251 _LIBCPP_WEAK
252 void
253 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
254 {
255     if (ptr) {
256         std::__libcpp_aligned_free(ptr);
257     }
258 }
259 
260 _LIBCPP_WEAK
261 void
262 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
263 {
264     ::operator delete(ptr, alignment);
265 }
266 
267 _LIBCPP_WEAK
268 void
269 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
270 {
271     ::operator delete(ptr, alignment);
272 }
273 
274 _LIBCPP_WEAK
275 void
276 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
277 {
278     ::operator delete(ptr, alignment);
279 }
280 
281 _LIBCPP_WEAK
282 void
283 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
284 {
285     ::operator delete[](ptr, alignment);
286 }
287 
288 _LIBCPP_WEAK
289 void
290 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
291 {
292     ::operator delete[](ptr, alignment);
293 }
294 
295 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
296 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
297