blob: 01df2512126cbd81919590688ae1bffd3c9b7ebf [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)
18// nothing todo
19#elif defined(LIBCXX_BUILDING_LIBCXXABI)
20#include <cxxabi.h>
21#elif defined(LIBCXXRT)
22#include <cxxabi.h>
23#include "support/runtime/new_handler_fallback.ipp"
24#elif defined(__GLIBCXX__)
25// nothing todo
26#else
Eric Fiselier0fbd44d2017-02-10 09:16:29 +000027# if defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
28# include <cxxabi.h> // FIXME: remove this once buildit is gone.
29# else
30# include "support/runtime/new_handler_fallback.ipp"
31# endif
Eric Fiselier88d11712017-02-10 07:43:08 +000032#endif
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000033
Eric Fiselierec3a1672017-02-10 08:57:35 +000034namespace std
35{
36
Peter Collingbournedc009952013-10-06 22:13:16 +000037#ifndef __GLIBCXX__
Eric Fiselierec3a1672017-02-10 08:57:35 +000038const nothrow_t nothrow = {};
39#endif
40
41#ifndef LIBSTDCXX
42
43void
44__throw_bad_alloc()
45{
46#ifndef _LIBCPP_NO_EXCEPTIONS
47 throw bad_alloc();
48#else
49 _VSTD::abort();
50#endif
51}
52
53#endif // !LIBSTDCXX
54
55} // std
56
Eric Fiselier95555c92017-03-02 19:35:33 +000057#if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_MICROSOFT) && \
58 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
Peter Collingbournedc009952013-10-06 22:13:16 +000059
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000060// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000061// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000062// that define non-weak copies of the functions.
63
Shoaib Meenai2d71db42016-11-16 22:18:10 +000064_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000065void *
Eric Fiselier162922f2016-10-14 06:46:30 +000066operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000067{
68 if (size == 0)
69 size = 1;
70 void* p;
71 while ((p = ::malloc(size)) == 0)
72 {
Howard Hinnant34468d42010-08-22 13:53:14 +000073 // If malloc fails and there is a new_handler,
74 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000075 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000076 if (nh)
77 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000078 else
Howard Hinnant72f73582010-08-11 17:04:31 +000079#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000080 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000081#else
82 break;
83#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000084 }
85 return p;
86}
87
Shoaib Meenai2d71db42016-11-16 22:18:10 +000088_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000089void*
90operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
91{
92 void* p = 0;
93#ifndef _LIBCPP_NO_EXCEPTIONS
94 try
95 {
96#endif // _LIBCPP_NO_EXCEPTIONS
97 p = ::operator new(size);
98#ifndef _LIBCPP_NO_EXCEPTIONS
99 }
100 catch (...)
101 {
102 }
103#endif // _LIBCPP_NO_EXCEPTIONS
104 return p;
105}
106
107_LIBCPP_WEAK
108void*
109operator new[](size_t size) _THROW_BAD_ALLOC
110{
111 return ::operator new(size);
112}
113
114_LIBCPP_WEAK
115void*
116operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
117{
118 void* p = 0;
119#ifndef _LIBCPP_NO_EXCEPTIONS
120 try
121 {
122#endif // _LIBCPP_NO_EXCEPTIONS
123 p = ::operator new[](size);
124#ifndef _LIBCPP_NO_EXCEPTIONS
125 }
126 catch (...)
127 {
128 }
129#endif // _LIBCPP_NO_EXCEPTIONS
130 return p;
131}
132
133_LIBCPP_WEAK
134void
135operator delete(void* ptr) _NOEXCEPT
136{
137 if (ptr)
138 ::free(ptr);
139}
140
141_LIBCPP_WEAK
142void
143operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
144{
145 ::operator delete(ptr);
146}
147
148_LIBCPP_WEAK
149void
150operator delete(void* ptr, size_t) _NOEXCEPT
151{
152 ::operator delete(ptr);
153}
154
155_LIBCPP_WEAK
156void
157operator delete[] (void* ptr) _NOEXCEPT
158{
159 ::operator delete(ptr);
160}
161
162_LIBCPP_WEAK
163void
164operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
165{
166 ::operator delete[](ptr);
167}
168
169_LIBCPP_WEAK
170void
171operator delete[] (void* ptr, size_t) _NOEXCEPT
172{
173 ::operator delete[](ptr);
174}
175
Eric Fiselier44832fb2017-01-20 01:47:26 +0000176#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
177
Eric Fiselier850652f2017-01-20 01:13:49 +0000178_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000179void *
180operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
181{
182 if (size == 0)
183 size = 1;
184 if (static_cast<size_t>(alignment) < sizeof(void*))
185 alignment = std::align_val_t(sizeof(void*));
186 void* p;
Eric Fiselierbb999f92017-05-31 22:14:05 +0000187#if defined(_LIBCPP_MSVCRT_LIKE)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000188 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
189#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000190 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000191#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000192 {
193 // If posix_memalign fails and there is a new_handler,
194 // call it to try free up memory.
195 std::new_handler nh = std::get_new_handler();
196 if (nh)
197 nh();
198 else {
199#ifndef _LIBCPP_NO_EXCEPTIONS
200 throw std::bad_alloc();
201#else
202 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
203 break;
204#endif
205 }
206 }
207 return p;
208}
209
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000210_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000211void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000212operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
213{
214 void* p = 0;
215#ifndef _LIBCPP_NO_EXCEPTIONS
216 try
217 {
218#endif // _LIBCPP_NO_EXCEPTIONS
219 p = ::operator new(size, alignment);
220#ifndef _LIBCPP_NO_EXCEPTIONS
221 }
222 catch (...)
223 {
224 }
225#endif // _LIBCPP_NO_EXCEPTIONS
226 return p;
227}
228
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000229_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000230void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000231operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
232{
233 return ::operator new(size, alignment);
234}
235
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000236_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000237void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000238operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
239{
240 void* p = 0;
241#ifndef _LIBCPP_NO_EXCEPTIONS
242 try
243 {
244#endif // _LIBCPP_NO_EXCEPTIONS
245 p = ::operator new[](size, alignment);
246#ifndef _LIBCPP_NO_EXCEPTIONS
247 }
248 catch (...)
249 {
250 }
251#endif // _LIBCPP_NO_EXCEPTIONS
252 return p;
253}
254
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000255_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000256void
Eric Fiselier162922f2016-10-14 06:46:30 +0000257operator delete(void* ptr, std::align_val_t) _NOEXCEPT
258{
259 if (ptr)
Eric Fiselierbb999f92017-05-31 22:14:05 +0000260#if defined(_LIBCPP_MSVCRT_LIKE)
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000261 ::_aligned_free(ptr);
262#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000263 ::free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000264#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000265}
266
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000267_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000268void
Eric Fiselier162922f2016-10-14 06:46:30 +0000269operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
270{
271 ::operator delete(ptr, alignment);
272}
273
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000274_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000275void
Eric Fiselier162922f2016-10-14 06:46:30 +0000276operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
277{
278 ::operator delete(ptr, alignment);
279}
280
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000281_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000282void
Eric Fiselier162922f2016-10-14 06:46:30 +0000283operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
284{
285 ::operator delete(ptr, alignment);
286}
287
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000288_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000289void
Eric Fiselier162922f2016-10-14 06:46:30 +0000290operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
291{
292 ::operator delete[](ptr, alignment);
293}
294
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000295_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000296void
Eric Fiselier162922f2016-10-14 06:46:30 +0000297operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
298{
299 ::operator delete[](ptr, alignment);
300}
301
Eric Fiselierec3a1672017-02-10 08:57:35 +0000302#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
Eric Fiselier95555c92017-03-02 19:35:33 +0000303#endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS