blob: 9d01330ba7d5f2ac9bcfd2a0b3bb68bc9a92bd25 [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)
Louis Dionnea810c332019-03-21 18:19:21 +000015# if !defined(_LIBCPP_ABI_VCRUNTIME)
16# include "support/runtime/new_handler_fallback.ipp"
17# endif
Eric Fiselierec3a1672017-02-10 08:57:35 +000018#elif defined(LIBCXX_BUILDING_LIBCXXABI)
Louis Dionnea810c332019-03-21 18:19:21 +000019# include <cxxabi.h>
Eric Fiselierec3a1672017-02-10 08:57:35 +000020#elif defined(LIBCXXRT)
Louis Dionnea810c332019-03-21 18:19:21 +000021# include <cxxabi.h>
Eric Fiselier0fbd44d2017-02-10 09:16:29 +000022# include "support/runtime/new_handler_fallback.ipp"
Louis Dionnea810c332019-03-21 18:19:21 +000023#elif defined(__GLIBCXX__)
24 // nothing to do
Louis Dionne9fd1b092019-04-17 21:57:49 +000025#else
Louis Dionne549c0552019-04-16 19:26:56 +000026# include "support/runtime/new_handler_fallback.ipp"
Eric Fiselier88d11712017-02-10 07:43:08 +000027#endif
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000028
Eric Fiselierec3a1672017-02-10 08:57:35 +000029namespace std
30{
31
Peter Collingbournedc009952013-10-06 22:13:16 +000032#ifndef __GLIBCXX__
Thomas Anderson637a0ad2019-01-30 19:09:41 +000033const nothrow_t nothrow{};
Eric Fiselierec3a1672017-02-10 08:57:35 +000034#endif
35
36#ifndef LIBSTDCXX
37
38void
39__throw_bad_alloc()
40{
41#ifndef _LIBCPP_NO_EXCEPTIONS
42 throw bad_alloc();
43#else
44 _VSTD::abort();
45#endif
46}
47
48#endif // !LIBSTDCXX
49
50} // std
51
Shoaib Meenaicfd19602017-10-09 19:25:17 +000052#if !defined(__GLIBCXX__) && \
Eric Fiselier85f66332019-03-05 01:57:01 +000053 !defined(_LIBCPP_ABI_VCRUNTIME) && \
Eric Fiselier95555c92017-03-02 19:35:33 +000054 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
Peter Collingbournedc009952013-10-06 22:13:16 +000055
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000056// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000057// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000058// that define non-weak copies of the functions.
59
Shoaib Meenai2d71db42016-11-16 22:18:10 +000060_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000061void *
Eric Fiselier162922f2016-10-14 06:46:30 +000062operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000063{
64 if (size == 0)
65 size = 1;
66 void* p;
Bruce Mitchener170d8972020-11-24 12:53:53 -050067 while ((p = ::malloc(size)) == nullptr)
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000068 {
Howard Hinnant34468d42010-08-22 13:53:14 +000069 // If malloc fails and there is a new_handler,
70 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000071 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000072 if (nh)
73 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000074 else
Howard Hinnant72f73582010-08-11 17:04:31 +000075#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000076 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000077#else
78 break;
79#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000080 }
81 return p;
82}
83
Shoaib Meenai2d71db42016-11-16 22:18:10 +000084_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000085void*
86operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
87{
Bruce Mitchener170d8972020-11-24 12:53:53 -050088 void* p = nullptr;
Eric Fiselier850652f2017-01-20 01:13:49 +000089#ifndef _LIBCPP_NO_EXCEPTIONS
90 try
91 {
92#endif // _LIBCPP_NO_EXCEPTIONS
93 p = ::operator new(size);
94#ifndef _LIBCPP_NO_EXCEPTIONS
95 }
96 catch (...)
97 {
98 }
99#endif // _LIBCPP_NO_EXCEPTIONS
100 return p;
101}
102
103_LIBCPP_WEAK
104void*
105operator new[](size_t size) _THROW_BAD_ALLOC
106{
107 return ::operator new(size);
108}
109
110_LIBCPP_WEAK
111void*
112operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
113{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500114 void* p = nullptr;
Eric Fiselier850652f2017-01-20 01:13:49 +0000115#ifndef _LIBCPP_NO_EXCEPTIONS
116 try
117 {
118#endif // _LIBCPP_NO_EXCEPTIONS
119 p = ::operator new[](size);
120#ifndef _LIBCPP_NO_EXCEPTIONS
121 }
122 catch (...)
123 {
124 }
125#endif // _LIBCPP_NO_EXCEPTIONS
126 return p;
127}
128
129_LIBCPP_WEAK
130void
131operator delete(void* ptr) _NOEXCEPT
132{
Louis Dionne50869482020-12-17 13:26:47 -0500133 ::free(ptr);
Eric Fiselier850652f2017-01-20 01:13:49 +0000134}
135
136_LIBCPP_WEAK
137void
138operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
139{
140 ::operator delete(ptr);
141}
142
143_LIBCPP_WEAK
144void
145operator delete(void* ptr, size_t) _NOEXCEPT
146{
147 ::operator delete(ptr);
148}
149
150_LIBCPP_WEAK
151void
152operator delete[] (void* ptr) _NOEXCEPT
153{
154 ::operator delete(ptr);
155}
156
157_LIBCPP_WEAK
158void
159operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
160{
161 ::operator delete[](ptr);
162}
163
164_LIBCPP_WEAK
165void
166operator delete[] (void* ptr, size_t) _NOEXCEPT
167{
168 ::operator delete[](ptr);
169}
170
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000171#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Eric Fiselier44832fb2017-01-20 01:47:26 +0000172
Eric Fiselier850652f2017-01-20 01:13:49 +0000173_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000174void *
175operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
176{
177 if (size == 0)
178 size = 1;
179 if (static_cast<size_t>(alignment) < sizeof(void*))
180 alignment = std::align_val_t(sizeof(void*));
Louis Dionnea3922172020-11-12 15:14:33 -0500181
182 // Try allocating memory. If allocation fails and there is a new_handler,
183 // call it to try free up memory, and try again until it succeeds, or until
184 // the new_handler decides to terminate.
185 //
186 // If allocation fails and there is no new_handler, we throw bad_alloc
187 // (or return nullptr if exceptions are disabled).
Eric Fiselier162922f2016-10-14 06:46:30 +0000188 void* p;
Louis Dionnea3922172020-11-12 15:14:33 -0500189 while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr)
Eric Fiselier162922f2016-10-14 06:46:30 +0000190 {
Eric Fiselier162922f2016-10-14 06:46:30 +0000191 std::new_handler nh = std::get_new_handler();
192 if (nh)
193 nh();
194 else {
195#ifndef _LIBCPP_NO_EXCEPTIONS
196 throw std::bad_alloc();
197#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000198 break;
199#endif
200 }
201 }
202 return p;
203}
204
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000205_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000206void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000207operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
208{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500209 void* p = nullptr;
Eric Fiselier162922f2016-10-14 06:46:30 +0000210#ifndef _LIBCPP_NO_EXCEPTIONS
211 try
212 {
213#endif // _LIBCPP_NO_EXCEPTIONS
214 p = ::operator new(size, alignment);
215#ifndef _LIBCPP_NO_EXCEPTIONS
216 }
217 catch (...)
218 {
219 }
220#endif // _LIBCPP_NO_EXCEPTIONS
221 return p;
222}
223
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000224_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000225void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000226operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
227{
228 return ::operator new(size, alignment);
229}
230
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000231_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000232void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000233operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
234{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500235 void* p = nullptr;
Eric Fiselier162922f2016-10-14 06:46:30 +0000236#ifndef _LIBCPP_NO_EXCEPTIONS
237 try
238 {
239#endif // _LIBCPP_NO_EXCEPTIONS
240 p = ::operator new[](size, alignment);
241#ifndef _LIBCPP_NO_EXCEPTIONS
242 }
243 catch (...)
244 {
245 }
246#endif // _LIBCPP_NO_EXCEPTIONS
247 return p;
248}
249
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000250_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000251void
Eric Fiselier162922f2016-10-14 06:46:30 +0000252operator delete(void* ptr, std::align_val_t) _NOEXCEPT
253{
Louis Dionne50869482020-12-17 13:26:47 -0500254 std::__libcpp_aligned_free(ptr);
Eric Fiselier162922f2016-10-14 06:46:30 +0000255}
256
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000257_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000258void
Eric Fiselier162922f2016-10-14 06:46:30 +0000259operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
260{
261 ::operator delete(ptr, alignment);
262}
263
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000264_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000265void
Eric Fiselier162922f2016-10-14 06:46:30 +0000266operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
267{
268 ::operator delete(ptr, alignment);
269}
270
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000271_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000272void
Eric Fiselier162922f2016-10-14 06:46:30 +0000273operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
274{
275 ::operator delete(ptr, alignment);
276}
277
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000278_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000279void
Eric Fiselier162922f2016-10-14 06:46:30 +0000280operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
281{
282 ::operator delete[](ptr, alignment);
283}
284
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000285_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000286void
Eric Fiselier162922f2016-10-14 06:46:30 +0000287operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
288{
289 ::operator delete[](ptr, alignment);
290}
291
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000292#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Eric Fiselier85f66332019-03-05 01:57:01 +0000293#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS