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