blob: b1e8ee324d84e9d0c92265bcf7f4ea59a6e1aad1 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===--------------------------- new.cpp ----------------------------------===//
2//
Howard Hinnantc566dc32010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00004//
Howard Hinnantee11c312010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Marshall Clow3bf77132013-09-11 01:38:42 +000010#define _LIBCPP_BUILDING_NEW
11
Howard Hinnant155c2af2010-05-24 17:49:41 +000012#include <stdlib.h>
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000013
Howard Hinnantc51e1022010-05-11 19:42:16 +000014#include "new"
15
Eric Fiselier88d11712017-02-10 07:43:08 +000016#if defined(__APPLE__) && !defined(LIBCXXRT) && \
17 !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
18 #include <cxxabi.h>
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000019
Eric Fiselier88d11712017-02-10 07:43:08 +000020 #ifndef _LIBCPPABI_VERSION
21 // On Darwin, there are two STL shared libraries and a lower level ABI
22 // shared library. The global holding the current new handler is
23 // in the ABI library and named __cxa_new_handler.
24 #define __new_handler __cxxabiapple::__cxa_new_handler
25 #endif
26#else // __APPLE__
27 #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
28 #include <cxxabi.h>
29 #endif // defined(LIBCXX_BUILDING_LIBCXXABI)
30 #if defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) || \
31 (!defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__))
32 static std::new_handler __new_handler;
33 #endif // _LIBCPPABI_VERSION
34#endif
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000035
Peter Collingbournedc009952013-10-06 22:13:16 +000036#ifndef __GLIBCXX__
37
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000038// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000039// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000040// that define non-weak copies of the functions.
41
Shoaib Meenai2d71db42016-11-16 22:18:10 +000042_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000043void *
Eric Fiselier162922f2016-10-14 06:46:30 +000044operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000045{
46 if (size == 0)
47 size = 1;
48 void* p;
49 while ((p = ::malloc(size)) == 0)
50 {
Howard Hinnant34468d42010-08-22 13:53:14 +000051 // If malloc fails and there is a new_handler,
52 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000053 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000054 if (nh)
55 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000056 else
Howard Hinnant72f73582010-08-11 17:04:31 +000057#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000058 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000059#else
60 break;
61#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000062 }
63 return p;
64}
65
Shoaib Meenai2d71db42016-11-16 22:18:10 +000066_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000067void*
68operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
69{
70 void* p = 0;
71#ifndef _LIBCPP_NO_EXCEPTIONS
72 try
73 {
74#endif // _LIBCPP_NO_EXCEPTIONS
75 p = ::operator new(size);
76#ifndef _LIBCPP_NO_EXCEPTIONS
77 }
78 catch (...)
79 {
80 }
81#endif // _LIBCPP_NO_EXCEPTIONS
82 return p;
83}
84
85_LIBCPP_WEAK
86void*
87operator new[](size_t size) _THROW_BAD_ALLOC
88{
89 return ::operator new(size);
90}
91
92_LIBCPP_WEAK
93void*
94operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
95{
96 void* p = 0;
97#ifndef _LIBCPP_NO_EXCEPTIONS
98 try
99 {
100#endif // _LIBCPP_NO_EXCEPTIONS
101 p = ::operator new[](size);
102#ifndef _LIBCPP_NO_EXCEPTIONS
103 }
104 catch (...)
105 {
106 }
107#endif // _LIBCPP_NO_EXCEPTIONS
108 return p;
109}
110
111_LIBCPP_WEAK
112void
113operator delete(void* ptr) _NOEXCEPT
114{
115 if (ptr)
116 ::free(ptr);
117}
118
119_LIBCPP_WEAK
120void
121operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
122{
123 ::operator delete(ptr);
124}
125
126_LIBCPP_WEAK
127void
128operator delete(void* ptr, size_t) _NOEXCEPT
129{
130 ::operator delete(ptr);
131}
132
133_LIBCPP_WEAK
134void
135operator delete[] (void* ptr) _NOEXCEPT
136{
137 ::operator delete(ptr);
138}
139
140_LIBCPP_WEAK
141void
142operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
143{
144 ::operator delete[](ptr);
145}
146
147_LIBCPP_WEAK
148void
149operator delete[] (void* ptr, size_t) _NOEXCEPT
150{
151 ::operator delete[](ptr);
152}
153
Eric Fiselier44832fb2017-01-20 01:47:26 +0000154#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
155
Eric Fiselier850652f2017-01-20 01:13:49 +0000156_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000157void *
158operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
159{
160 if (size == 0)
161 size = 1;
162 if (static_cast<size_t>(alignment) < sizeof(void*))
163 alignment = std::align_val_t(sizeof(void*));
164 void* p;
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000165#if defined(_LIBCPP_MSVCRT)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000166 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
167#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000168 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000169#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000170 {
171 // If posix_memalign fails and there is a new_handler,
172 // call it to try free up memory.
173 std::new_handler nh = std::get_new_handler();
174 if (nh)
175 nh();
176 else {
177#ifndef _LIBCPP_NO_EXCEPTIONS
178 throw std::bad_alloc();
179#else
180 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
181 break;
182#endif
183 }
184 }
185 return p;
186}
187
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000188_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000189void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000190operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
191{
192 void* p = 0;
193#ifndef _LIBCPP_NO_EXCEPTIONS
194 try
195 {
196#endif // _LIBCPP_NO_EXCEPTIONS
197 p = ::operator new(size, alignment);
198#ifndef _LIBCPP_NO_EXCEPTIONS
199 }
200 catch (...)
201 {
202 }
203#endif // _LIBCPP_NO_EXCEPTIONS
204 return p;
205}
206
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000207_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000208void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000209operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
210{
211 return ::operator new(size, alignment);
212}
213
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000214_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000215void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000216operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
217{
218 void* p = 0;
219#ifndef _LIBCPP_NO_EXCEPTIONS
220 try
221 {
222#endif // _LIBCPP_NO_EXCEPTIONS
223 p = ::operator new[](size, alignment);
224#ifndef _LIBCPP_NO_EXCEPTIONS
225 }
226 catch (...)
227 {
228 }
229#endif // _LIBCPP_NO_EXCEPTIONS
230 return p;
231}
232
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000233_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000234void
Eric Fiselier162922f2016-10-14 06:46:30 +0000235operator delete(void* ptr, std::align_val_t) _NOEXCEPT
236{
237 if (ptr)
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000238#if defined(_LIBCPP_MSVCRT)
239 ::_aligned_free(ptr);
240#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000241 ::free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000242#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000243}
244
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000245_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000246void
Eric Fiselier162922f2016-10-14 06:46:30 +0000247operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
248{
249 ::operator delete(ptr, alignment);
250}
251
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000252_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000253void
Eric Fiselier162922f2016-10-14 06:46:30 +0000254operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
255{
256 ::operator delete(ptr, alignment);
257}
258
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000259_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000260void
Eric Fiselier162922f2016-10-14 06:46:30 +0000261operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
262{
263 ::operator delete(ptr, alignment);
264}
265
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000266_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000267void
Eric Fiselier162922f2016-10-14 06:46:30 +0000268operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
269{
270 ::operator delete[](ptr, alignment);
271}
272
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000273_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000274void
Eric Fiselier162922f2016-10-14 06:46:30 +0000275operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
276{
277 ::operator delete[](ptr, alignment);
278}
279
Eric Fiselier88d11712017-02-10 07:43:08 +0000280#endif // !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
281
282#endif // !__GLIBCXX__
283
284namespace std
285{
286
287#ifndef __GLIBCXX__
288const nothrow_t nothrow = {};
289#endif
290
291#ifndef _LIBCPPABI_VERSION
292
293#ifndef __GLIBCXX__
294
295new_handler
296set_new_handler(new_handler handler) _NOEXCEPT
297{
298 return __sync_lock_test_and_set(&__new_handler, handler);
299}
300
301new_handler
302get_new_handler() _NOEXCEPT
303{
304 return __sync_fetch_and_add(&__new_handler, nullptr);
305}
306
307#endif // !__GLIBCXX__
308
309#ifndef LIBCXXRT
310
311bad_alloc::bad_alloc() _NOEXCEPT
312{
313}
314
315#ifndef __GLIBCXX__
316
317bad_alloc::~bad_alloc() _NOEXCEPT
318{
319}
320
321const char*
322bad_alloc::what() const _NOEXCEPT
323{
324 return "std::bad_alloc";
325}
326
327#endif // !__GLIBCXX__
328
329bad_array_new_length::bad_array_new_length() _NOEXCEPT
330{
331}
332
333#ifndef __GLIBCXX__
334
335bad_array_new_length::~bad_array_new_length() _NOEXCEPT
336{
337}
338
339const char*
340bad_array_new_length::what() const _NOEXCEPT
341{
342 return "bad_array_new_length";
343}
344
345#endif // !__GLIBCXX__
346
347#endif //LIBCXXRT
348
349bad_array_length::bad_array_length() _NOEXCEPT
350{
351}
352
353#ifndef __GLIBCXX__
354
355bad_array_length::~bad_array_length() _NOEXCEPT
356{
357}
358
359const char*
360bad_array_length::what() const _NOEXCEPT
361{
362 return "bad_array_length";
363}
364
365#endif // !__GLIBCXX__
366
367#endif // _LIBCPPABI_VERSION
368
369#ifndef LIBSTDCXX
370
371void
372__throw_bad_alloc()
373{
374#ifndef _LIBCPP_NO_EXCEPTIONS
375 throw bad_alloc();
376#else
377 _VSTD::abort();
378#endif
379}
380
381#endif // !LIBSTDCXX
382
383} // std