]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - src/new.cpp
Vendor import of libc++ trunk r302418:
[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(_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     !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
58
59 // Implement all new and delete operators as weak definitions
60 // in this shared library, so that they can be overridden by programs
61 // that define non-weak copies of the functions.
62
63 _LIBCPP_WEAK
64 void *
65 operator new(std::size_t size) _THROW_BAD_ALLOC
66 {
67     if (size == 0)
68         size = 1;
69     void* p;
70     while ((p = ::malloc(size)) == 0)
71     {
72         // If malloc fails and there is a new_handler,
73         // call it to try free up memory.
74         std::new_handler nh = std::get_new_handler();
75         if (nh)
76             nh();
77         else
78 #ifndef _LIBCPP_NO_EXCEPTIONS
79             throw std::bad_alloc();
80 #else
81             break;
82 #endif
83     }
84     return p;
85 }
86
87 _LIBCPP_WEAK
88 void*
89 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
90 {
91     void* p = 0;
92 #ifndef _LIBCPP_NO_EXCEPTIONS
93     try
94     {
95 #endif  // _LIBCPP_NO_EXCEPTIONS
96         p = ::operator new(size);
97 #ifndef _LIBCPP_NO_EXCEPTIONS
98     }
99     catch (...)
100     {
101     }
102 #endif  // _LIBCPP_NO_EXCEPTIONS
103     return p;
104 }
105
106 _LIBCPP_WEAK
107 void*
108 operator new[](size_t size) _THROW_BAD_ALLOC
109 {
110     return ::operator new(size);
111 }
112
113 _LIBCPP_WEAK
114 void*
115 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
116 {
117     void* p = 0;
118 #ifndef _LIBCPP_NO_EXCEPTIONS
119     try
120     {
121 #endif  // _LIBCPP_NO_EXCEPTIONS
122         p = ::operator new[](size);
123 #ifndef _LIBCPP_NO_EXCEPTIONS
124     }
125     catch (...)
126     {
127     }
128 #endif  // _LIBCPP_NO_EXCEPTIONS
129     return p;
130 }
131
132 _LIBCPP_WEAK
133 void
134 operator delete(void* ptr) _NOEXCEPT
135 {
136     if (ptr)
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_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)
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 (ptr)
259 #if defined(_LIBCPP_MSVCRT)
260         ::_aligned_free(ptr);
261 #else
262         ::free(ptr);
263 #endif
264 }
265
266 _LIBCPP_WEAK
267 void
268 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
269 {
270     ::operator delete(ptr, alignment);
271 }
272
273 _LIBCPP_WEAK
274 void
275 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
276 {
277     ::operator delete(ptr, alignment);
278 }
279
280 _LIBCPP_WEAK
281 void
282 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
283 {
284     ::operator delete(ptr, alignment);
285 }
286
287 _LIBCPP_WEAK
288 void
289 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
290 {
291     ::operator delete[](ptr, alignment);
292 }
293
294 _LIBCPP_WEAK
295 void
296 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
297 {
298     ::operator delete[](ptr, alignment);
299 }
300
301 #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
302 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS