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