blob: 21b30740196b0e2397dac89ac36ce7fa20a12520 [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
Eric Fiselier95555c92017-03-02 19:35:33 +000056#if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_MICROSOFT) && \
57 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
Peter Collingbournedc009952013-10-06 22:13:16 +000058
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000059// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000060// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000061// that define non-weak copies of the functions.
62
Shoaib Meenai2d71db42016-11-16 22:18:10 +000063_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000064void *
Eric Fiselier162922f2016-10-14 06:46:30 +000065operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000066{
67 if (size == 0)
68 size = 1;
69 void* p;
70 while ((p = ::malloc(size)) == 0)
71 {
Howard Hinnant34468d42010-08-22 13:53:14 +000072 // If malloc fails and there is a new_handler,
73 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000074 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000075 if (nh)
76 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000077 else
Howard Hinnant72f73582010-08-11 17:04:31 +000078#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000079 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000080#else
81 break;
82#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000083 }
84 return p;
85}
86
Shoaib Meenai2d71db42016-11-16 22:18:10 +000087_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000088void*
89operator 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
107void*
108operator new[](size_t size) _THROW_BAD_ALLOC
109{
110 return ::operator new(size);
111}
112
113_LIBCPP_WEAK
114void*
115operator 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
133void
134operator delete(void* ptr) _NOEXCEPT
135{
136 if (ptr)
137 ::free(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
154_LIBCPP_WEAK
155void
156operator delete[] (void* ptr) _NOEXCEPT
157{
158 ::operator delete(ptr);
159}
160
161_LIBCPP_WEAK
162void
163operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
164{
165 ::operator delete[](ptr);
166}
167
168_LIBCPP_WEAK
169void
170operator delete[] (void* ptr, size_t) _NOEXCEPT
171{
172 ::operator delete[](ptr);
173}
174
Eric Fiselier44832fb2017-01-20 01:47:26 +0000175#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
176
Eric Fiselier850652f2017-01-20 01:13:49 +0000177_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000178void *
179operator 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;
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000186#if defined(_LIBCPP_MSVCRT)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000187 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
188#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000189 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000190#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000191 {
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
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000209_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000210void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000211operator 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
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000228_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000229void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000230operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
231{
232 return ::operator new(size, alignment);
233}
234
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000235_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000236void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000237operator 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
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000254_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000255void
Eric Fiselier162922f2016-10-14 06:46:30 +0000256operator delete(void* ptr, std::align_val_t) _NOEXCEPT
257{
258 if (ptr)
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000259#if defined(_LIBCPP_MSVCRT)
260 ::_aligned_free(ptr);
261#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000262 ::free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000263#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000264}
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
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000280_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000281void
Eric Fiselier162922f2016-10-14 06:46:30 +0000282operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
283{
284 ::operator delete(ptr, alignment);
285}
286
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000287_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000288void
Eric Fiselier162922f2016-10-14 06:46:30 +0000289operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
290{
291 ::operator delete[](ptr, alignment);
292}
293
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000294_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000295void
Eric Fiselier162922f2016-10-14 06:46:30 +0000296operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
297{
298 ::operator delete[](ptr, alignment);
299}
300
Eric Fiselierec3a1672017-02-10 08:57:35 +0000301#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
Eric Fiselier95555c92017-03-02 19:35:33 +0000302#endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS