blob: eb629b36fa33ea3466716580952e4d653902183c [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===--------------------------- new.cpp ----------------------------------===//
2//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
Howard Hinnant155c2af2010-05-24 17:49:41 +00009#include <stdlib.h>
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000010
Howard Hinnantc51e1022010-05-11 19:42:16 +000011#include "new"
Weiming Zhaob613db72017-09-19 23:18:03 +000012#include "include/atomic_support.h"
Howard Hinnantc51e1022010-05-11 19:42:16 +000013
Eric Fiselierec3a1672017-02-10 08:57:35 +000014#if defined(_LIBCPP_ABI_MICROSOFT)
Eric Fiselier85f66332019-03-05 01:57:01 +000015#if !defined(_LIBCPP_ABI_VCRUNTIME)
Shoaib Meenaicfd19602017-10-09 19:25:17 +000016#include "support/runtime/new_handler_fallback.ipp"
17#endif
Eric Fiselierec3a1672017-02-10 08:57:35 +000018#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__
Thomas Anderson637a0ad2019-01-30 19:09:41 +000037const nothrow_t nothrow{};
Eric Fiselierec3a1672017-02-10 08:57:35 +000038#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
Shoaib Meenaicfd19602017-10-09 19:25:17 +000056#if !defined(__GLIBCXX__) && \
Eric Fiselier85f66332019-03-05 01:57:01 +000057 !defined(_LIBCPP_ABI_VCRUNTIME) && \
Eric Fiselier95555c92017-03-02 19:35:33 +000058 !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{
Fangrui Songfd64ad02018-10-01 17:21:07 +0000137 ::free(ptr);
Eric Fiselier850652f2017-01-20 01:13:49 +0000138}
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 Fiselierff4c8a22018-10-11 00:17:24 +0000175#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Eric Fiselier44832fb2017-01-20 01:47:26 +0000176
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;
Eric Fiselierbb999f92017-05-31 22:14:05 +0000186#if defined(_LIBCPP_MSVCRT_LIKE)
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{
Eric Fiselierbb999f92017-05-31 22:14:05 +0000258#if defined(_LIBCPP_MSVCRT_LIKE)
Fangrui Songfd64ad02018-10-01 17:21:07 +0000259 ::_aligned_free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000260#else
Fangrui Songfd64ad02018-10-01 17:21:07 +0000261 ::free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000262#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000263}
264
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000265_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000266void
Eric Fiselier162922f2016-10-14 06:46:30 +0000267operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
268{
269 ::operator delete(ptr, alignment);
270}
271
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000272_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000273void
Eric Fiselier162922f2016-10-14 06:46:30 +0000274operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
275{
276 ::operator delete(ptr, alignment);
277}
278
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000279_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000280void
Eric Fiselier162922f2016-10-14 06:46:30 +0000281operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
282{
283 ::operator delete(ptr, alignment);
284}
285
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000286_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000287void
Eric Fiselier162922f2016-10-14 06:46:30 +0000288operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
289{
290 ::operator delete[](ptr, alignment);
291}
292
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000293_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000294void
Eric Fiselier162922f2016-10-14 06:46:30 +0000295operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
296{
297 ::operator delete[](ptr, alignment);
298}
299
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000300#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Eric Fiselier85f66332019-03-05 01:57:01 +0000301#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS