blob: cb8e3ae4ce6113137ed9745652347de4ed71be73 [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
26#include "support/runtime/new_handler_fallback.ipp"
Eric Fiselier88d11712017-02-10 07:43:08 +000027#endif
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000028
Eric Fiselierec3a1672017-02-10 08:57:35 +000029namespace std
30{
31
Peter Collingbournedc009952013-10-06 22:13:16 +000032#ifndef __GLIBCXX__
Eric Fiselierec3a1672017-02-10 08:57:35 +000033const nothrow_t nothrow = {};
34#endif
35
36#ifndef LIBSTDCXX
37
38void
39__throw_bad_alloc()
40{
41#ifndef _LIBCPP_NO_EXCEPTIONS
42 throw bad_alloc();
43#else
44 _VSTD::abort();
45#endif
46}
47
48#endif // !LIBSTDCXX
49
50} // std
51
52#if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_MICROSOFT)
Peter Collingbournedc009952013-10-06 22:13:16 +000053
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000054// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000055// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000056// that define non-weak copies of the functions.
57
Shoaib Meenai2d71db42016-11-16 22:18:10 +000058_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000059void *
Eric Fiselier162922f2016-10-14 06:46:30 +000060operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000061{
62 if (size == 0)
63 size = 1;
64 void* p;
65 while ((p = ::malloc(size)) == 0)
66 {
Howard Hinnant34468d42010-08-22 13:53:14 +000067 // If malloc fails and there is a new_handler,
68 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000069 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000070 if (nh)
71 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000072 else
Howard Hinnant72f73582010-08-11 17:04:31 +000073#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000074 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000075#else
76 break;
77#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000078 }
79 return p;
80}
81
Shoaib Meenai2d71db42016-11-16 22:18:10 +000082_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000083void*
84operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
85{
86 void* p = 0;
87#ifndef _LIBCPP_NO_EXCEPTIONS
88 try
89 {
90#endif // _LIBCPP_NO_EXCEPTIONS
91 p = ::operator new(size);
92#ifndef _LIBCPP_NO_EXCEPTIONS
93 }
94 catch (...)
95 {
96 }
97#endif // _LIBCPP_NO_EXCEPTIONS
98 return p;
99}
100
101_LIBCPP_WEAK
102void*
103operator new[](size_t size) _THROW_BAD_ALLOC
104{
105 return ::operator new(size);
106}
107
108_LIBCPP_WEAK
109void*
110operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
111{
112 void* p = 0;
113#ifndef _LIBCPP_NO_EXCEPTIONS
114 try
115 {
116#endif // _LIBCPP_NO_EXCEPTIONS
117 p = ::operator new[](size);
118#ifndef _LIBCPP_NO_EXCEPTIONS
119 }
120 catch (...)
121 {
122 }
123#endif // _LIBCPP_NO_EXCEPTIONS
124 return p;
125}
126
127_LIBCPP_WEAK
128void
129operator delete(void* ptr) _NOEXCEPT
130{
131 if (ptr)
132 ::free(ptr);
133}
134
135_LIBCPP_WEAK
136void
137operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
138{
139 ::operator delete(ptr);
140}
141
142_LIBCPP_WEAK
143void
144operator delete(void* ptr, size_t) _NOEXCEPT
145{
146 ::operator delete(ptr);
147}
148
149_LIBCPP_WEAK
150void
151operator delete[] (void* ptr) _NOEXCEPT
152{
153 ::operator delete(ptr);
154}
155
156_LIBCPP_WEAK
157void
158operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
159{
160 ::operator delete[](ptr);
161}
162
163_LIBCPP_WEAK
164void
165operator delete[] (void* ptr, size_t) _NOEXCEPT
166{
167 ::operator delete[](ptr);
168}
169
Eric Fiselier44832fb2017-01-20 01:47:26 +0000170#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
171
Eric Fiselier850652f2017-01-20 01:13:49 +0000172_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000173void *
174operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
175{
176 if (size == 0)
177 size = 1;
178 if (static_cast<size_t>(alignment) < sizeof(void*))
179 alignment = std::align_val_t(sizeof(void*));
180 void* p;
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000181#if defined(_LIBCPP_MSVCRT)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000182 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
183#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000184 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000185#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000186 {
187 // If posix_memalign fails and there is a new_handler,
188 // call it to try free up memory.
189 std::new_handler nh = std::get_new_handler();
190 if (nh)
191 nh();
192 else {
193#ifndef _LIBCPP_NO_EXCEPTIONS
194 throw std::bad_alloc();
195#else
196 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
197 break;
198#endif
199 }
200 }
201 return p;
202}
203
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000204_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000205void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000206operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
207{
208 void* p = 0;
209#ifndef _LIBCPP_NO_EXCEPTIONS
210 try
211 {
212#endif // _LIBCPP_NO_EXCEPTIONS
213 p = ::operator new(size, alignment);
214#ifndef _LIBCPP_NO_EXCEPTIONS
215 }
216 catch (...)
217 {
218 }
219#endif // _LIBCPP_NO_EXCEPTIONS
220 return p;
221}
222
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000223_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000224void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000225operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
226{
227 return ::operator new(size, alignment);
228}
229
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000230_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000231void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000232operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
233{
234 void* p = 0;
235#ifndef _LIBCPP_NO_EXCEPTIONS
236 try
237 {
238#endif // _LIBCPP_NO_EXCEPTIONS
239 p = ::operator new[](size, alignment);
240#ifndef _LIBCPP_NO_EXCEPTIONS
241 }
242 catch (...)
243 {
244 }
245#endif // _LIBCPP_NO_EXCEPTIONS
246 return p;
247}
248
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000249_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000250void
Eric Fiselier162922f2016-10-14 06:46:30 +0000251operator delete(void* ptr, std::align_val_t) _NOEXCEPT
252{
253 if (ptr)
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000254#if defined(_LIBCPP_MSVCRT)
255 ::_aligned_free(ptr);
256#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000257 ::free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000258#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000259}
260
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000261_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000262void
Eric Fiselier162922f2016-10-14 06:46:30 +0000263operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
264{
265 ::operator delete(ptr, alignment);
266}
267
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000268_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000269void
Eric Fiselier162922f2016-10-14 06:46:30 +0000270operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
271{
272 ::operator delete(ptr, alignment);
273}
274
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000275_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000276void
Eric Fiselier162922f2016-10-14 06:46:30 +0000277operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
278{
279 ::operator delete(ptr, alignment);
280}
281
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000282_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000283void
Eric Fiselier162922f2016-10-14 06:46:30 +0000284operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
285{
286 ::operator delete[](ptr, alignment);
287}
288
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000289_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000290void
Eric Fiselier162922f2016-10-14 06:46:30 +0000291operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
292{
293 ::operator delete[](ptr, alignment);
294}
295
Eric Fiselierec3a1672017-02-10 08:57:35 +0000296#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
297#endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT