blob: f2ca698512ff4cc708629c6d142f7e844277e799 [file] [log] [blame]
Louis Dionne9bd93882021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00002//
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
Louis Dionnebb8099a2022-12-02 13:45:49 -08009#include <__memory/aligned_alloc.h>
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050010#include <new>
Howard Hinnant155c2af2010-05-24 17:49:41 +000011#include <stdlib.h>
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000012
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)
Louis Dionnea810c332019-03-21 18:19:21 +000016# if !defined(_LIBCPP_ABI_VCRUNTIME)
17# include "support/runtime/new_handler_fallback.ipp"
18# endif
Eric Fiselierec3a1672017-02-10 08:57:35 +000019#elif defined(LIBCXX_BUILDING_LIBCXXABI)
Louis Dionnea810c332019-03-21 18:19:21 +000020# include <cxxabi.h>
Eric Fiselierec3a1672017-02-10 08:57:35 +000021#elif defined(LIBCXXRT)
Louis Dionnea810c332019-03-21 18:19:21 +000022# include <cxxabi.h>
Eric Fiselier0fbd44d2017-02-10 09:16:29 +000023# include "support/runtime/new_handler_fallback.ipp"
Louis Dionnea810c332019-03-21 18:19:21 +000024#elif defined(__GLIBCXX__)
25 // nothing to do
Louis Dionne9fd1b092019-04-17 21:57:49 +000026#else
Louis Dionne549c0552019-04-16 19:26:56 +000027# include "support/runtime/new_handler_fallback.ipp"
Eric Fiselier88d11712017-02-10 07:43:08 +000028#endif
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000029
Eric Fiselierec3a1672017-02-10 08:57:35 +000030namespace std
31{
32
Peter Collingbournedc009952013-10-06 22:13:16 +000033#ifndef __GLIBCXX__
Thomas Anderson637a0ad2019-01-30 19:09:41 +000034const nothrow_t nothrow{};
Eric Fiselierec3a1672017-02-10 08:57:35 +000035#endif
36
37#ifndef LIBSTDCXX
38
39void
40__throw_bad_alloc()
41{
42#ifndef _LIBCPP_NO_EXCEPTIONS
43 throw bad_alloc();
44#else
45 _VSTD::abort();
46#endif
47}
48
49#endif // !LIBSTDCXX
50
51} // std
52
Shoaib Meenaicfd19602017-10-09 19:25:17 +000053#if !defined(__GLIBCXX__) && \
Eric Fiselier85f66332019-03-05 01:57:01 +000054 !defined(_LIBCPP_ABI_VCRUNTIME) && \
Eric Fiselier95555c92017-03-02 19:35:33 +000055 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
Peter Collingbournedc009952013-10-06 22:13:16 +000056
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000057// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000058// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000059// that define non-weak copies of the functions.
60
Shoaib Meenai2d71db42016-11-16 22:18:10 +000061_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000062void *
Eric Fiselier162922f2016-10-14 06:46:30 +000063operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000064{
65 if (size == 0)
66 size = 1;
67 void* p;
Bruce Mitchener170d8972020-11-24 12:53:53 -050068 while ((p = ::malloc(size)) == nullptr)
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000069 {
Howard Hinnant34468d42010-08-22 13:53:14 +000070 // If malloc fails and there is a new_handler,
71 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000072 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000073 if (nh)
74 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000075 else
Howard Hinnant72f73582010-08-11 17:04:31 +000076#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000077 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000078#else
79 break;
80#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000081 }
82 return p;
83}
84
Shoaib Meenai2d71db42016-11-16 22:18:10 +000085_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000086void*
Louis Dionne65358e12021-03-01 12:09:45 -050087operator new(size_t size, const std::nothrow_t&) noexcept
Eric Fiselier850652f2017-01-20 01:13:49 +000088{
Bruce Mitchener170d8972020-11-24 12:53:53 -050089 void* p = nullptr;
Eric Fiselier850652f2017-01-20 01:13:49 +000090#ifndef _LIBCPP_NO_EXCEPTIONS
91 try
92 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -040093#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier850652f2017-01-20 01:13:49 +000094 p = ::operator new(size);
95#ifndef _LIBCPP_NO_EXCEPTIONS
96 }
97 catch (...)
98 {
99 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400100#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier850652f2017-01-20 01:13:49 +0000101 return p;
102}
103
104_LIBCPP_WEAK
105void*
106operator new[](size_t size) _THROW_BAD_ALLOC
107{
108 return ::operator new(size);
109}
110
111_LIBCPP_WEAK
112void*
Louis Dionne65358e12021-03-01 12:09:45 -0500113operator new[](size_t size, const std::nothrow_t&) noexcept
Eric Fiselier850652f2017-01-20 01:13:49 +0000114{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500115 void* p = nullptr;
Eric Fiselier850652f2017-01-20 01:13:49 +0000116#ifndef _LIBCPP_NO_EXCEPTIONS
117 try
118 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400119#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier850652f2017-01-20 01:13:49 +0000120 p = ::operator new[](size);
121#ifndef _LIBCPP_NO_EXCEPTIONS
122 }
123 catch (...)
124 {
125 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400126#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier850652f2017-01-20 01:13:49 +0000127 return p;
128}
129
130_LIBCPP_WEAK
131void
Louis Dionne65358e12021-03-01 12:09:45 -0500132operator delete(void* ptr) noexcept
Eric Fiselier850652f2017-01-20 01:13:49 +0000133{
Louis Dionne50869482020-12-17 13:26:47 -0500134 ::free(ptr);
Eric Fiselier850652f2017-01-20 01:13:49 +0000135}
136
137_LIBCPP_WEAK
138void
Louis Dionne65358e12021-03-01 12:09:45 -0500139operator delete(void* ptr, const std::nothrow_t&) noexcept
Eric Fiselier850652f2017-01-20 01:13:49 +0000140{
141 ::operator delete(ptr);
142}
143
144_LIBCPP_WEAK
145void
Louis Dionne65358e12021-03-01 12:09:45 -0500146operator delete(void* ptr, size_t) noexcept
Eric Fiselier850652f2017-01-20 01:13:49 +0000147{
148 ::operator delete(ptr);
149}
150
151_LIBCPP_WEAK
152void
Louis Dionne65358e12021-03-01 12:09:45 -0500153operator delete[] (void* ptr) noexcept
Eric Fiselier850652f2017-01-20 01:13:49 +0000154{
155 ::operator delete(ptr);
156}
157
158_LIBCPP_WEAK
159void
Louis Dionne65358e12021-03-01 12:09:45 -0500160operator delete[] (void* ptr, const std::nothrow_t&) noexcept
Eric Fiselier850652f2017-01-20 01:13:49 +0000161{
162 ::operator delete[](ptr);
163}
164
165_LIBCPP_WEAK
166void
Louis Dionne65358e12021-03-01 12:09:45 -0500167operator delete[] (void* ptr, size_t) noexcept
Eric Fiselier850652f2017-01-20 01:13:49 +0000168{
169 ::operator delete[](ptr);
170}
171
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000172#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Eric Fiselier44832fb2017-01-20 01:47:26 +0000173
Eric Fiselier850652f2017-01-20 01:13:49 +0000174_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000175void *
176operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
177{
178 if (size == 0)
179 size = 1;
180 if (static_cast<size_t>(alignment) < sizeof(void*))
181 alignment = std::align_val_t(sizeof(void*));
Louis Dionnea3922172020-11-12 15:14:33 -0500182
183 // Try allocating memory. If allocation fails and there is a new_handler,
184 // call it to try free up memory, and try again until it succeeds, or until
185 // the new_handler decides to terminate.
186 //
187 // If allocation fails and there is no new_handler, we throw bad_alloc
188 // (or return nullptr if exceptions are disabled).
Eric Fiselier162922f2016-10-14 06:46:30 +0000189 void* p;
Louis Dionnea3922172020-11-12 15:14:33 -0500190 while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr)
Eric Fiselier162922f2016-10-14 06:46:30 +0000191 {
Eric Fiselier162922f2016-10-14 06:46:30 +0000192 std::new_handler nh = std::get_new_handler();
193 if (nh)
194 nh();
195 else {
196#ifndef _LIBCPP_NO_EXCEPTIONS
197 throw std::bad_alloc();
198#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000199 break;
200#endif
201 }
202 }
203 return p;
204}
205
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000206_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000207void*
Louis Dionne65358e12021-03-01 12:09:45 -0500208operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
Eric Fiselier162922f2016-10-14 06:46:30 +0000209{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500210 void* p = nullptr;
Eric Fiselier162922f2016-10-14 06:46:30 +0000211#ifndef _LIBCPP_NO_EXCEPTIONS
212 try
213 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400214#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier162922f2016-10-14 06:46:30 +0000215 p = ::operator new(size, alignment);
216#ifndef _LIBCPP_NO_EXCEPTIONS
217 }
218 catch (...)
219 {
220 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400221#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier162922f2016-10-14 06:46:30 +0000222 return p;
223}
224
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000225_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000226void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000227operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
228{
229 return ::operator new(size, alignment);
230}
231
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000232_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000233void*
Louis Dionne65358e12021-03-01 12:09:45 -0500234operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
Eric Fiselier162922f2016-10-14 06:46:30 +0000235{
Bruce Mitchener170d8972020-11-24 12:53:53 -0500236 void* p = nullptr;
Eric Fiselier162922f2016-10-14 06:46:30 +0000237#ifndef _LIBCPP_NO_EXCEPTIONS
238 try
239 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400240#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier162922f2016-10-14 06:46:30 +0000241 p = ::operator new[](size, alignment);
242#ifndef _LIBCPP_NO_EXCEPTIONS
243 }
244 catch (...)
245 {
246 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400247#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselier162922f2016-10-14 06:46:30 +0000248 return p;
249}
250
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000251_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000252void
Louis Dionne65358e12021-03-01 12:09:45 -0500253operator delete(void* ptr, std::align_val_t) noexcept
Eric Fiselier162922f2016-10-14 06:46:30 +0000254{
Louis Dionne50869482020-12-17 13:26:47 -0500255 std::__libcpp_aligned_free(ptr);
Eric Fiselier162922f2016-10-14 06:46:30 +0000256}
257
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000258_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000259void
Louis Dionne65358e12021-03-01 12:09:45 -0500260operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
Eric Fiselier162922f2016-10-14 06:46:30 +0000261{
262 ::operator delete(ptr, alignment);
263}
264
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000265_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000266void
Louis Dionne65358e12021-03-01 12:09:45 -0500267operator delete(void* ptr, size_t, std::align_val_t alignment) noexcept
Eric Fiselier162922f2016-10-14 06:46:30 +0000268{
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
Louis Dionne65358e12021-03-01 12:09:45 -0500274operator delete[] (void* ptr, std::align_val_t alignment) noexcept
Eric Fiselier162922f2016-10-14 06:46:30 +0000275{
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
Louis Dionne65358e12021-03-01 12:09:45 -0500281operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
Eric Fiselier162922f2016-10-14 06:46:30 +0000282{
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
Louis Dionne65358e12021-03-01 12:09:45 -0500288operator delete[] (void* ptr, size_t, std::align_val_t alignment) noexcept
Eric Fiselier162922f2016-10-14 06:46:30 +0000289{
290 ::operator delete[](ptr, alignment);
291}
292
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000293#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Eric Fiselier85f66332019-03-05 01:57:01 +0000294#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS