blob: e228a0d83d8e68b49056fec0131eb7359b0f116f [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"
Weiming Zhaob613db72017-09-19 23:18:03 +000015#include "include/atomic_support.h"
Howard Hinnantc51e1022010-05-11 19:42:16 +000016
Eric Fiselierec3a1672017-02-10 08:57:35 +000017#if defined(_LIBCPP_ABI_MICROSOFT)
Shoaib Meenaicfd19602017-10-09 19:25:17 +000018#if defined(_LIBCPP_NO_VCRUNTIME)
19#include "support/runtime/new_handler_fallback.ipp"
20#endif
Eric Fiselierec3a1672017-02-10 08:57:35 +000021#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
Eric Fiselier0fbd44d2017-02-10 09:16:29 +000029# 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
Eric Fiselier88d11712017-02-10 07:43:08 +000034#endif
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000035
Eric Fiselierec3a1672017-02-10 08:57:35 +000036namespace std
37{
38
Peter Collingbournedc009952013-10-06 22:13:16 +000039#ifndef __GLIBCXX__
Eric Fiselierec3a1672017-02-10 08:57:35 +000040const nothrow_t nothrow = {};
41#endif
42
43#ifndef LIBSTDCXX
44
45void
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
Shoaib Meenaicfd19602017-10-09 19:25:17 +000059#if !defined(__GLIBCXX__) && \
60 (!defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)) && \
Eric Fiselier95555c92017-03-02 19:35:33 +000061 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
Peter Collingbournedc009952013-10-06 22:13:16 +000062
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000063// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000064// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000065// that define non-weak copies of the functions.
66
Shoaib Meenai2d71db42016-11-16 22:18:10 +000067_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000068void *
Eric Fiselier162922f2016-10-14 06:46:30 +000069operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000070{
71 if (size == 0)
72 size = 1;
73 void* p;
74 while ((p = ::malloc(size)) == 0)
75 {
Howard Hinnant34468d42010-08-22 13:53:14 +000076 // If malloc fails and there is a new_handler,
77 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000078 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000079 if (nh)
80 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000081 else
Howard Hinnant72f73582010-08-11 17:04:31 +000082#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000083 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000084#else
85 break;
86#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000087 }
88 return p;
89}
90
Shoaib Meenai2d71db42016-11-16 22:18:10 +000091_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000092void*
93operator 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
111void*
112operator new[](size_t size) _THROW_BAD_ALLOC
113{
114 return ::operator new(size);
115}
116
117_LIBCPP_WEAK
118void*
119operator 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
137void
138operator delete(void* ptr) _NOEXCEPT
139{
140 if (ptr)
141 ::free(ptr);
142}
143
144_LIBCPP_WEAK
145void
146operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
147{
148 ::operator delete(ptr);
149}
150
151_LIBCPP_WEAK
152void
153operator delete(void* ptr, size_t) _NOEXCEPT
154{
155 ::operator delete(ptr);
156}
157
158_LIBCPP_WEAK
159void
160operator delete[] (void* ptr) _NOEXCEPT
161{
162 ::operator delete(ptr);
163}
164
165_LIBCPP_WEAK
166void
167operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
168{
169 ::operator delete[](ptr);
170}
171
172_LIBCPP_WEAK
173void
174operator delete[] (void* ptr, size_t) _NOEXCEPT
175{
176 ::operator delete[](ptr);
177}
178
Eric Fiselier44832fb2017-01-20 01:47:26 +0000179#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
180
Eric Fiselier850652f2017-01-20 01:13:49 +0000181_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000182void *
183operator 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;
Eric Fiselierbb999f92017-05-31 22:14:05 +0000190#if defined(_LIBCPP_MSVCRT_LIKE)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000191 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
192#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000193 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000194#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000195 {
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
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000213_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000214void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000215operator 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
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000232_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000233void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000234operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
235{
236 return ::operator new(size, alignment);
237}
238
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000239_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000240void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000241operator 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
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000258_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000259void
Eric Fiselier162922f2016-10-14 06:46:30 +0000260operator delete(void* ptr, std::align_val_t) _NOEXCEPT
261{
262 if (ptr)
Eric Fiselierbb999f92017-05-31 22:14:05 +0000263#if defined(_LIBCPP_MSVCRT_LIKE)
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000264 ::_aligned_free(ptr);
265#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000266 ::free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000267#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000268}
269
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000270_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000271void
Eric Fiselier162922f2016-10-14 06:46:30 +0000272operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
273{
274 ::operator delete(ptr, alignment);
275}
276
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000277_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000278void
Eric Fiselier162922f2016-10-14 06:46:30 +0000279operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
280{
281 ::operator delete(ptr, alignment);
282}
283
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000284_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000285void
Eric Fiselier162922f2016-10-14 06:46:30 +0000286operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
287{
288 ::operator delete(ptr, alignment);
289}
290
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000291_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000292void
Eric Fiselier162922f2016-10-14 06:46:30 +0000293operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
294{
295 ::operator delete[](ptr, alignment);
296}
297
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000298_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000299void
Eric Fiselier162922f2016-10-14 06:46:30 +0000300operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
301{
302 ::operator delete[](ptr, alignment);
303}
304
Eric Fiselierec3a1672017-02-10 08:57:35 +0000305#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
Shoaib Meenaicfd19602017-10-09 19:25:17 +0000306#endif // !__GLIBCXX__ && (!_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME) && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS