]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - src/new.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / src / new.cpp
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(__APPLE__) && !defined(LIBCXXRT)
17     #include <cxxabi.h>
18
19     #ifndef _LIBCPPABI_VERSION
20         // On Darwin, there are two STL shared libraries and a lower level ABI
21         // shared library.  The global holding the current new handler is
22         // in the ABI library and named __cxa_new_handler.
23         #define __new_handler __cxxabiapple::__cxa_new_handler
24     #endif
25 #else  // __APPLE__
26     #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
27         #include <cxxabi.h>
28     #endif  // defined(LIBCXX_BUILDING_LIBCXXABI)
29     #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
30         static std::new_handler __new_handler;
31     #endif  // _LIBCPPABI_VERSION
32 #endif
33
34 #ifndef __GLIBCXX__
35
36 // Implement all new and delete operators as weak definitions
37 // in this shared library, so that they can be overridden by programs
38 // that define non-weak copies of the functions.
39
40 _LIBCPP_WEAK
41 void *
42 operator new(std::size_t size) _THROW_BAD_ALLOC
43 {
44     if (size == 0)
45         size = 1;
46     void* p;
47     while ((p = ::malloc(size)) == 0)
48     {
49         // If malloc fails and there is a new_handler,
50         // call it to try free up memory.
51         std::new_handler nh = std::get_new_handler();
52         if (nh)
53             nh();
54         else
55 #ifndef _LIBCPP_NO_EXCEPTIONS
56             throw std::bad_alloc();
57 #else
58             break;
59 #endif
60     }
61     return p;
62 }
63
64 _LIBCPP_WEAK
65 void *
66 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
67 {
68     if (size == 0)
69         size = 1;
70     if (static_cast<size_t>(alignment) < sizeof(void*))
71       alignment = std::align_val_t(sizeof(void*));
72     void* p;
73 #if defined(_WIN32)
74     while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
75 #else
76     while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
77 #endif
78     {
79         // If posix_memalign fails and there is a new_handler,
80         // call it to try free up memory.
81         std::new_handler nh = std::get_new_handler();
82         if (nh)
83             nh();
84         else {
85 #ifndef _LIBCPP_NO_EXCEPTIONS
86             throw std::bad_alloc();
87 #else
88             p = nullptr; // posix_memalign doesn't initialize 'p' on failure
89             break;
90 #endif
91         }
92     }
93     return p;
94 }
95
96 _LIBCPP_WEAK
97 void*
98 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
99 {
100     void* p = 0;
101 #ifndef _LIBCPP_NO_EXCEPTIONS
102     try
103     {
104 #endif  // _LIBCPP_NO_EXCEPTIONS
105         p = ::operator new(size);
106 #ifndef _LIBCPP_NO_EXCEPTIONS
107     }
108     catch (...)
109     {
110     }
111 #endif  // _LIBCPP_NO_EXCEPTIONS
112     return p;
113 }
114
115 _LIBCPP_WEAK
116 void*
117 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
118 {
119     void* p = 0;
120 #ifndef _LIBCPP_NO_EXCEPTIONS
121     try
122     {
123 #endif  // _LIBCPP_NO_EXCEPTIONS
124         p = ::operator new(size, alignment);
125 #ifndef _LIBCPP_NO_EXCEPTIONS
126     }
127     catch (...)
128     {
129     }
130 #endif  // _LIBCPP_NO_EXCEPTIONS
131     return p;
132 }
133
134 _LIBCPP_WEAK
135 void*
136 operator new[](size_t size) _THROW_BAD_ALLOC
137 {
138     return ::operator new(size);
139 }
140
141 _LIBCPP_WEAK
142 void*
143 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
144 {
145     return ::operator new(size, alignment);
146 }
147
148 _LIBCPP_WEAK
149 void*
150 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
151 {
152     void* p = 0;
153 #ifndef _LIBCPP_NO_EXCEPTIONS
154     try
155     {
156 #endif  // _LIBCPP_NO_EXCEPTIONS
157         p = ::operator new[](size);
158 #ifndef _LIBCPP_NO_EXCEPTIONS
159     }
160     catch (...)
161     {
162     }
163 #endif  // _LIBCPP_NO_EXCEPTIONS
164     return p;
165 }
166
167 _LIBCPP_WEAK
168 void*
169 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
170 {
171     void* p = 0;
172 #ifndef _LIBCPP_NO_EXCEPTIONS
173     try
174     {
175 #endif  // _LIBCPP_NO_EXCEPTIONS
176         p = ::operator new[](size, alignment);
177 #ifndef _LIBCPP_NO_EXCEPTIONS
178     }
179     catch (...)
180     {
181     }
182 #endif  // _LIBCPP_NO_EXCEPTIONS
183     return p;
184 }
185
186 _LIBCPP_WEAK
187 void
188 operator delete(void* ptr) _NOEXCEPT
189 {
190     if (ptr)
191         ::free(ptr);
192 }
193
194 _LIBCPP_WEAK
195 void
196 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
197 {
198     if (ptr)
199         ::free(ptr);
200 }
201
202 _LIBCPP_WEAK
203 void
204 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
205 {
206     ::operator delete(ptr);
207 }
208
209 _LIBCPP_WEAK
210 void
211 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
212 {
213     ::operator delete(ptr, alignment);
214 }
215
216 _LIBCPP_WEAK
217 void
218 operator delete(void* ptr, size_t) _NOEXCEPT
219 {
220     ::operator delete(ptr);
221 }
222
223 _LIBCPP_WEAK
224 void
225 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
226 {
227     ::operator delete(ptr, alignment);
228 }
229
230 _LIBCPP_WEAK
231 void
232 operator delete[] (void* ptr) _NOEXCEPT
233 {
234     ::operator delete(ptr);
235 }
236
237 _LIBCPP_WEAK
238 void
239 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
240 {
241     ::operator delete(ptr, alignment);
242 }
243
244 _LIBCPP_WEAK
245 void
246 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
247 {
248     ::operator delete[](ptr);
249 }
250
251 _LIBCPP_WEAK
252 void
253 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
254 {
255     ::operator delete[](ptr, alignment);
256 }
257
258 _LIBCPP_WEAK
259 void
260 operator delete[] (void* ptr, size_t) _NOEXCEPT
261 {
262     ::operator delete[](ptr);
263 }
264
265 _LIBCPP_WEAK
266 void
267 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
268 {
269     ::operator delete[](ptr, alignment);
270 }
271
272 #endif // !__GLIBCXX__
273
274 namespace std
275 {
276
277 #ifndef __GLIBCXX__
278 const nothrow_t nothrow = {};
279 #endif
280
281 #ifndef _LIBCPPABI_VERSION
282
283 #ifndef __GLIBCXX__
284
285 new_handler
286 set_new_handler(new_handler handler) _NOEXCEPT
287 {
288     return __sync_lock_test_and_set(&__new_handler, handler);
289 }
290
291 new_handler
292 get_new_handler() _NOEXCEPT
293 {
294     return __sync_fetch_and_add(&__new_handler, nullptr);
295 }
296
297 #endif // !__GLIBCXX__
298
299 #ifndef LIBCXXRT
300
301 bad_alloc::bad_alloc() _NOEXCEPT
302 {
303 }
304
305 #ifndef __GLIBCXX__
306
307 bad_alloc::~bad_alloc() _NOEXCEPT
308 {
309 }
310
311 const char*
312 bad_alloc::what() const _NOEXCEPT
313 {
314     return "std::bad_alloc";
315 }
316
317 #endif // !__GLIBCXX__
318
319 bad_array_new_length::bad_array_new_length() _NOEXCEPT
320 {
321 }
322
323 #ifndef __GLIBCXX__
324
325 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
326 {
327 }
328
329 const char*
330 bad_array_new_length::what() const _NOEXCEPT
331 {
332     return "bad_array_new_length";
333 }
334
335 #endif // !__GLIBCXX__
336
337 #endif //LIBCXXRT
338
339 bad_array_length::bad_array_length() _NOEXCEPT
340 {
341 }
342
343 #ifndef __GLIBCXX__
344
345 bad_array_length::~bad_array_length() _NOEXCEPT
346 {
347 }
348
349 const char*
350 bad_array_length::what() const _NOEXCEPT
351 {
352     return "bad_array_length";
353 }
354
355 #endif // !__GLIBCXX__
356
357 #endif // _LIBCPPABI_VERSION
358
359 #ifndef LIBSTDCXX
360
361 void
362 __throw_bad_alloc()
363 {
364 #ifndef _LIBCPP_NO_EXCEPTIONS
365     throw bad_alloc();
366 #else
367     _VSTD::abort();
368 #endif
369 }
370
371 #endif // !LIBSTDCXX
372
373 }  // std