xref: /llvm-project-15.0.7/libcxx/src/new.cpp (revision ed98e8d0)
1 //===--------------------------- new.cpp ----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #define _LIBCPP_BUILDING_NEW
11 
12 #include <stdlib.h>
13 
14 #include "new"
15 
16 #if defined(_LIBCPP_ABI_MICROSOFT)
17 // nothing todo
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 todo
25 #else
26 # if defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
27 #   include <cxxabi.h> // FIXME: remove this once buildit is gone.
28 # else
29 #   include "support/runtime/new_handler_fallback.ipp"
30 # endif
31 #endif
32 
33 namespace std
34 {
35 
36 #ifndef __GLIBCXX__
37 const nothrow_t nothrow = {};
38 #endif
39 
40 #ifndef LIBSTDCXX
41 
42 void
43 __throw_bad_alloc()
44 {
45 #ifndef _LIBCPP_NO_EXCEPTIONS
46     throw bad_alloc();
47 #else
48     _VSTD::abort();
49 #endif
50 }
51 
52 #endif // !LIBSTDCXX
53 
54 }  // std
55 
56 #if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_MICROSOFT)
57 
58 // Implement all new and delete operators as weak definitions
59 // in this shared library, so that they can be overridden by programs
60 // that define non-weak copies of the functions.
61 
62 _LIBCPP_WEAK
63 void *
64 operator new(std::size_t size) _THROW_BAD_ALLOC
65 {
66     if (size == 0)
67         size = 1;
68     void* p;
69     while ((p = ::malloc(size)) == 0)
70     {
71         // If malloc fails and there is a new_handler,
72         // call it to try free up memory.
73         std::new_handler nh = std::get_new_handler();
74         if (nh)
75             nh();
76         else
77 #ifndef _LIBCPP_NO_EXCEPTIONS
78             throw std::bad_alloc();
79 #else
80             break;
81 #endif
82     }
83     return p;
84 }
85 
86 _LIBCPP_WEAK
87 void*
88 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
89 {
90     void* p = 0;
91 #ifndef _LIBCPP_NO_EXCEPTIONS
92     try
93     {
94 #endif  // _LIBCPP_NO_EXCEPTIONS
95         p = ::operator new(size);
96 #ifndef _LIBCPP_NO_EXCEPTIONS
97     }
98     catch (...)
99     {
100     }
101 #endif  // _LIBCPP_NO_EXCEPTIONS
102     return p;
103 }
104 
105 _LIBCPP_WEAK
106 void*
107 operator new[](size_t size) _THROW_BAD_ALLOC
108 {
109     return ::operator new(size);
110 }
111 
112 _LIBCPP_WEAK
113 void*
114 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
115 {
116     void* p = 0;
117 #ifndef _LIBCPP_NO_EXCEPTIONS
118     try
119     {
120 #endif  // _LIBCPP_NO_EXCEPTIONS
121         p = ::operator new[](size);
122 #ifndef _LIBCPP_NO_EXCEPTIONS
123     }
124     catch (...)
125     {
126     }
127 #endif  // _LIBCPP_NO_EXCEPTIONS
128     return p;
129 }
130 
131 _LIBCPP_WEAK
132 void
133 operator delete(void* ptr) _NOEXCEPT
134 {
135     if (ptr)
136         ::free(ptr);
137 }
138 
139 _LIBCPP_WEAK
140 void
141 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
142 {
143     ::operator delete(ptr);
144 }
145 
146 _LIBCPP_WEAK
147 void
148 operator delete(void* ptr, size_t) _NOEXCEPT
149 {
150     ::operator delete(ptr);
151 }
152 
153 _LIBCPP_WEAK
154 void
155 operator delete[] (void* ptr) _NOEXCEPT
156 {
157     ::operator delete(ptr);
158 }
159 
160 _LIBCPP_WEAK
161 void
162 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
163 {
164     ::operator delete[](ptr);
165 }
166 
167 _LIBCPP_WEAK
168 void
169 operator delete[] (void* ptr, size_t) _NOEXCEPT
170 {
171     ::operator delete[](ptr);
172 }
173 
174 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
175 
176 _LIBCPP_WEAK
177 void *
178 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
179 {
180     if (size == 0)
181         size = 1;
182     if (static_cast<size_t>(alignment) < sizeof(void*))
183       alignment = std::align_val_t(sizeof(void*));
184     void* p;
185 #if defined(_LIBCPP_MSVCRT)
186     while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
187 #else
188     while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
189 #endif
190     {
191         // If posix_memalign fails and there is a new_handler,
192         // call it to try free up memory.
193         std::new_handler nh = std::get_new_handler();
194         if (nh)
195             nh();
196         else {
197 #ifndef _LIBCPP_NO_EXCEPTIONS
198             throw std::bad_alloc();
199 #else
200             p = nullptr; // posix_memalign doesn't initialize 'p' on failure
201             break;
202 #endif
203         }
204     }
205     return p;
206 }
207 
208 _LIBCPP_WEAK
209 void*
210 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
211 {
212     void* p = 0;
213 #ifndef _LIBCPP_NO_EXCEPTIONS
214     try
215     {
216 #endif  // _LIBCPP_NO_EXCEPTIONS
217         p = ::operator new(size, alignment);
218 #ifndef _LIBCPP_NO_EXCEPTIONS
219     }
220     catch (...)
221     {
222     }
223 #endif  // _LIBCPP_NO_EXCEPTIONS
224     return p;
225 }
226 
227 _LIBCPP_WEAK
228 void*
229 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
230 {
231     return ::operator new(size, alignment);
232 }
233 
234 _LIBCPP_WEAK
235 void*
236 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
237 {
238     void* p = 0;
239 #ifndef _LIBCPP_NO_EXCEPTIONS
240     try
241     {
242 #endif  // _LIBCPP_NO_EXCEPTIONS
243         p = ::operator new[](size, alignment);
244 #ifndef _LIBCPP_NO_EXCEPTIONS
245     }
246     catch (...)
247     {
248     }
249 #endif  // _LIBCPP_NO_EXCEPTIONS
250     return p;
251 }
252 
253 _LIBCPP_WEAK
254 void
255 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
256 {
257     if (ptr)
258 #if defined(_LIBCPP_MSVCRT)
259         ::_aligned_free(ptr);
260 #else
261         ::free(ptr);
262 #endif
263 }
264 
265 _LIBCPP_WEAK
266 void
267 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
268 {
269     ::operator delete(ptr, alignment);
270 }
271 
272 _LIBCPP_WEAK
273 void
274 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
275 {
276     ::operator delete(ptr, alignment);
277 }
278 
279 _LIBCPP_WEAK
280 void
281 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
282 {
283     ::operator delete(ptr, alignment);
284 }
285 
286 _LIBCPP_WEAK
287 void
288 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
289 {
290     ::operator delete[](ptr, alignment);
291 }
292 
293 _LIBCPP_WEAK
294 void
295 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
296 {
297     ::operator delete[](ptr, alignment);
298 }
299 
300 #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
301 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT
302