xref: /llvm-project-15.0.7/libcxx/src/new.cpp (revision f05b42e9)
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 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__) &&                                                   \
57     !defined(_LIBCPP_ABI_VCRUNTIME) &&      \
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     ::free(ptr);
138 }
139 
140 _LIBCPP_WEAK
141 void
142 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
143 {
144     ::operator delete(ptr);
145 }
146 
147 _LIBCPP_WEAK
148 void
149 operator delete(void* ptr, size_t) _NOEXCEPT
150 {
151     ::operator delete(ptr);
152 }
153 
154 _LIBCPP_WEAK
155 void
156 operator delete[] (void* ptr) _NOEXCEPT
157 {
158     ::operator delete(ptr);
159 }
160 
161 _LIBCPP_WEAK
162 void
163 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
164 {
165     ::operator delete[](ptr);
166 }
167 
168 _LIBCPP_WEAK
169 void
170 operator delete[] (void* ptr, size_t) _NOEXCEPT
171 {
172     ::operator delete[](ptr);
173 }
174 
175 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
176 
177 _LIBCPP_WEAK
178 void *
179 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
180 {
181     if (size == 0)
182         size = 1;
183     if (static_cast<size_t>(alignment) < sizeof(void*))
184       alignment = std::align_val_t(sizeof(void*));
185     void* p;
186 #if defined(_LIBCPP_MSVCRT_LIKE)
187     while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
188 #else
189     while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
190 #endif
191     {
192         // If posix_memalign fails and there is a new_handler,
193         // call it to try free up memory.
194         std::new_handler nh = std::get_new_handler();
195         if (nh)
196             nh();
197         else {
198 #ifndef _LIBCPP_NO_EXCEPTIONS
199             throw std::bad_alloc();
200 #else
201             p = nullptr; // posix_memalign doesn't initialize 'p' on failure
202             break;
203 #endif
204         }
205     }
206     return p;
207 }
208 
209 _LIBCPP_WEAK
210 void*
211 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
212 {
213     void* p = 0;
214 #ifndef _LIBCPP_NO_EXCEPTIONS
215     try
216     {
217 #endif  // _LIBCPP_NO_EXCEPTIONS
218         p = ::operator new(size, alignment);
219 #ifndef _LIBCPP_NO_EXCEPTIONS
220     }
221     catch (...)
222     {
223     }
224 #endif  // _LIBCPP_NO_EXCEPTIONS
225     return p;
226 }
227 
228 _LIBCPP_WEAK
229 void*
230 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
231 {
232     return ::operator new(size, alignment);
233 }
234 
235 _LIBCPP_WEAK
236 void*
237 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
238 {
239     void* p = 0;
240 #ifndef _LIBCPP_NO_EXCEPTIONS
241     try
242     {
243 #endif  // _LIBCPP_NO_EXCEPTIONS
244         p = ::operator new[](size, alignment);
245 #ifndef _LIBCPP_NO_EXCEPTIONS
246     }
247     catch (...)
248     {
249     }
250 #endif  // _LIBCPP_NO_EXCEPTIONS
251     return p;
252 }
253 
254 _LIBCPP_WEAK
255 void
256 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
257 {
258 #if defined(_LIBCPP_MSVCRT_LIKE)
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_LIBRARY_ALIGNED_ALLOCATION
301 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
302