blob: 5a4686d259a230e2f58b8e72f7f207e15c67ec2b [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 Fiselierec3a1672017-02-10 08:57:35 +000016#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
Eric Fiselier0fbd44d2017-02-10 09:16:29 +000026# 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
Eric Fiselier88d11712017-02-10 07:43:08 +000031#endif
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000032
Eric Fiselierec3a1672017-02-10 08:57:35 +000033namespace std
34{
35
Peter Collingbournedc009952013-10-06 22:13:16 +000036#ifndef __GLIBCXX__
Eric Fiselierec3a1672017-02-10 08:57:35 +000037const nothrow_t nothrow = {};
38#endif
39
40#ifndef LIBSTDCXX
41
42void
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)
Peter Collingbournedc009952013-10-06 22:13:16 +000057
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000058// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000059// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000060// that define non-weak copies of the functions.
61
Shoaib Meenai2d71db42016-11-16 22:18:10 +000062_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000063void *
Eric Fiselier162922f2016-10-14 06:46:30 +000064operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000065{
66 if (size == 0)
67 size = 1;
68 void* p;
69 while ((p = ::malloc(size)) == 0)
70 {
Howard Hinnant34468d42010-08-22 13:53:14 +000071 // If malloc fails and there is a new_handler,
72 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000073 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000074 if (nh)
75 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000076 else
Howard Hinnant72f73582010-08-11 17:04:31 +000077#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000078 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000079#else
80 break;
81#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000082 }
83 return p;
84}
85
Shoaib Meenai2d71db42016-11-16 22:18:10 +000086_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000087void*
88operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
89{
90 void* p = 0;
91#ifndef _LIBCPP_NO_EXCEPTIONS
92 try
93 {
94#endif // _LIBCPP_NO_EXCEPTIONS
95 p = ::operator new(size);
96#ifndef _LIBCPP_NO_EXCEPTIONS
97 }
98 catch (...)
99 {
100 }
101#endif // _LIBCPP_NO_EXCEPTIONS
102 return p;
103}
104
105_LIBCPP_WEAK
106void*
107operator new[](size_t size) _THROW_BAD_ALLOC
108{
109 return ::operator new(size);
110}
111
112_LIBCPP_WEAK
113void*
114operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
115{
116 void* p = 0;
117#ifndef _LIBCPP_NO_EXCEPTIONS
118 try
119 {
120#endif // _LIBCPP_NO_EXCEPTIONS
121 p = ::operator new[](size);
122#ifndef _LIBCPP_NO_EXCEPTIONS
123 }
124 catch (...)
125 {
126 }
127#endif // _LIBCPP_NO_EXCEPTIONS
128 return p;
129}
130
131_LIBCPP_WEAK
132void
133operator delete(void* ptr) _NOEXCEPT
134{
135 if (ptr)
136 ::free(ptr);
137}
138
139_LIBCPP_WEAK
140void
141operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
142{
143 ::operator delete(ptr);
144}
145
146_LIBCPP_WEAK
147void
148operator delete(void* ptr, size_t) _NOEXCEPT
149{
150 ::operator delete(ptr);
151}
152
153_LIBCPP_WEAK
154void
155operator delete[] (void* ptr) _NOEXCEPT
156{
157 ::operator delete(ptr);
158}
159
160_LIBCPP_WEAK
161void
162operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
163{
164 ::operator delete[](ptr);
165}
166
167_LIBCPP_WEAK
168void
169operator delete[] (void* ptr, size_t) _NOEXCEPT
170{
171 ::operator delete[](ptr);
172}
173
Eric Fiselier44832fb2017-01-20 01:47:26 +0000174#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
175
Eric Fiselier850652f2017-01-20 01:13:49 +0000176_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000177void *
178operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
179{
180 if (size == 0)
181 size = 1;
182 if (static_cast<size_t>(alignment) < sizeof(void*))
183 alignment = std::align_val_t(sizeof(void*));
184 void* p;
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000185#if defined(_LIBCPP_MSVCRT)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000186 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
187#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000188 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000189#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000190 {
191 // If posix_memalign fails and there is a new_handler,
192 // call it to try free up memory.
193 std::new_handler nh = std::get_new_handler();
194 if (nh)
195 nh();
196 else {
197#ifndef _LIBCPP_NO_EXCEPTIONS
198 throw std::bad_alloc();
199#else
200 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
201 break;
202#endif
203 }
204 }
205 return p;
206}
207
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000208_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000209void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000210operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
211{
212 void* p = 0;
213#ifndef _LIBCPP_NO_EXCEPTIONS
214 try
215 {
216#endif // _LIBCPP_NO_EXCEPTIONS
217 p = ::operator new(size, alignment);
218#ifndef _LIBCPP_NO_EXCEPTIONS
219 }
220 catch (...)
221 {
222 }
223#endif // _LIBCPP_NO_EXCEPTIONS
224 return p;
225}
226
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000227_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000228void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000229operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
230{
231 return ::operator new(size, alignment);
232}
233
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000234_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000235void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000236operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
237{
238 void* p = 0;
239#ifndef _LIBCPP_NO_EXCEPTIONS
240 try
241 {
242#endif // _LIBCPP_NO_EXCEPTIONS
243 p = ::operator new[](size, alignment);
244#ifndef _LIBCPP_NO_EXCEPTIONS
245 }
246 catch (...)
247 {
248 }
249#endif // _LIBCPP_NO_EXCEPTIONS
250 return p;
251}
252
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000253_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000254void
Eric Fiselier162922f2016-10-14 06:46:30 +0000255operator delete(void* ptr, std::align_val_t) _NOEXCEPT
256{
257 if (ptr)
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000258#if defined(_LIBCPP_MSVCRT)
259 ::_aligned_free(ptr);
260#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000261 ::free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000262#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000263}
264
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000265_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000266void
Eric Fiselier162922f2016-10-14 06:46:30 +0000267operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
268{
269 ::operator delete(ptr, alignment);
270}
271
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000272_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000273void
Eric Fiselier162922f2016-10-14 06:46:30 +0000274operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
275{
276 ::operator delete(ptr, alignment);
277}
278
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000279_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000280void
Eric Fiselier162922f2016-10-14 06:46:30 +0000281operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
282{
283 ::operator delete(ptr, alignment);
284}
285
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000286_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000287void
Eric Fiselier162922f2016-10-14 06:46:30 +0000288operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
289{
290 ::operator delete[](ptr, alignment);
291}
292
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000293_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000294void
Eric Fiselier162922f2016-10-14 06:46:30 +0000295operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
296{
297 ::operator delete[](ptr, alignment);
298}
299
Eric Fiselierec3a1672017-02-10 08:57:35 +0000300#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
301#endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT