blob: cc8383d4f1469bd74e38e09b739913ff528184b9 [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
Howard Hinnant155c2af2010-05-24 17:49:41 +000010#include <stdlib.h>
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000011
Howard Hinnantc51e1022010-05-11 19:42:16 +000012#include "new"
Weiming Zhaob613db72017-09-19 23:18:03 +000013#include "include/atomic_support.h"
Howard Hinnantc51e1022010-05-11 19:42:16 +000014
Eric Fiselierec3a1672017-02-10 08:57:35 +000015#if defined(_LIBCPP_ABI_MICROSOFT)
Shoaib Meenaicfd19602017-10-09 19:25:17 +000016#if defined(_LIBCPP_NO_VCRUNTIME)
17#include "support/runtime/new_handler_fallback.ipp"
18#endif
Eric Fiselierec3a1672017-02-10 08:57:35 +000019#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
Shoaib Meenaicfd19602017-10-09 19:25:17 +000057#if !defined(__GLIBCXX__) && \
Eric Fiselierff4c8a22018-10-11 00:17:24 +000058 !defined(_LIBCPP_DEFER_NEW_TO_VCRUNTIME) && \
Eric Fiselier95555c92017-03-02 19:35:33 +000059 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
Peter Collingbournedc009952013-10-06 22:13:16 +000060
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000061// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000062// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000063// that define non-weak copies of the functions.
64
Shoaib Meenai2d71db42016-11-16 22:18:10 +000065_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000066void *
Eric Fiselier162922f2016-10-14 06:46:30 +000067operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000068{
69 if (size == 0)
70 size = 1;
71 void* p;
72 while ((p = ::malloc(size)) == 0)
73 {
Howard Hinnant34468d42010-08-22 13:53:14 +000074 // If malloc fails and there is a new_handler,
75 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000076 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000077 if (nh)
78 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000079 else
Howard Hinnant72f73582010-08-11 17:04:31 +000080#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000081 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000082#else
83 break;
84#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000085 }
86 return p;
87}
88
Shoaib Meenai2d71db42016-11-16 22:18:10 +000089_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000090void*
91operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
92{
93 void* p = 0;
94#ifndef _LIBCPP_NO_EXCEPTIONS
95 try
96 {
97#endif // _LIBCPP_NO_EXCEPTIONS
98 p = ::operator new(size);
99#ifndef _LIBCPP_NO_EXCEPTIONS
100 }
101 catch (...)
102 {
103 }
104#endif // _LIBCPP_NO_EXCEPTIONS
105 return p;
106}
107
108_LIBCPP_WEAK
109void*
110operator new[](size_t size) _THROW_BAD_ALLOC
111{
112 return ::operator new(size);
113}
114
115_LIBCPP_WEAK
116void*
117operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
118{
119 void* p = 0;
120#ifndef _LIBCPP_NO_EXCEPTIONS
121 try
122 {
123#endif // _LIBCPP_NO_EXCEPTIONS
124 p = ::operator new[](size);
125#ifndef _LIBCPP_NO_EXCEPTIONS
126 }
127 catch (...)
128 {
129 }
130#endif // _LIBCPP_NO_EXCEPTIONS
131 return p;
132}
133
134_LIBCPP_WEAK
135void
136operator delete(void* ptr) _NOEXCEPT
137{
Fangrui Songfd64ad02018-10-01 17:21:07 +0000138 ::free(ptr);
Eric Fiselier850652f2017-01-20 01:13:49 +0000139}
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 Fiselierff4c8a22018-10-11 00:17:24 +0000176#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Eric Fiselier44832fb2017-01-20 01:47:26 +0000177
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{
Eric Fiselierbb999f92017-05-31 22:14:05 +0000259#if defined(_LIBCPP_MSVCRT_LIKE)
Fangrui Songfd64ad02018-10-01 17:21:07 +0000260 ::_aligned_free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000261#else
Fangrui Songfd64ad02018-10-01 17:21:07 +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 Fiselierff4c8a22018-10-11 00:17:24 +0000301#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Shoaib Meenaicfd19602017-10-09 19:25:17 +0000302#endif // !__GLIBCXX__ && (!_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME) && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS