1//===--------------------- stdlib_new_delete.cpp --------------------------===//
2//
3// 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
6//
7//
8// This file implements the new and delete operators.
9//===----------------------------------------------------------------------===//
10
11#define _LIBCPP_BUILDING_LIBRARY
12#include "__cxxabi_config.h"
13#include <new>
14#include <cstdlib>
15
16#if !defined(_THROW_BAD_ALLOC) || !defined(_NOEXCEPT) || !defined(_LIBCXXABI_WEAK)
17#error The _THROW_BAD_ALLOC, _NOEXCEPT, and _LIBCXXABI_WEAK libc++ macros must \
18 already be defined by libc++.
19#endif
20// Implement all new and delete operators as weak definitions
21// in this shared library, so that they can be overridden by programs
22// that define non-weak copies of the functions.
23
24_LIBCXXABI_WEAK
25void *
26operator new(std::size_t size) _THROW_BAD_ALLOC
27{
28 if (size == 0)
29 size = 1;
30 void* p;
31 while ((p = ::malloc(size)) == 0)
32 {
33 // If malloc fails and there is a new_handler,
34 // call it to try free up memory.
35 std::new_handler nh = std::get_new_handler();
36 if (nh)
37 nh();
38 else
39#ifndef _LIBCXXABI_NO_EXCEPTIONS
40 throw std::bad_alloc();
41#else
42 break;
43#endif
44 }
45 return p;
46}
47
48_LIBCXXABI_WEAK
49void*
50operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
51{
52 void* p = 0;
53#ifndef _LIBCXXABI_NO_EXCEPTIONS
54 try
55 {
56#endif // _LIBCXXABI_NO_EXCEPTIONS
57 p = ::operator new(size);
58#ifndef _LIBCXXABI_NO_EXCEPTIONS
59 }
60 catch (...)
61 {
62 }
63#endif // _LIBCXXABI_NO_EXCEPTIONS
64 return p;
65}
66
67_LIBCXXABI_WEAK
68void*
69operator new[](size_t size) _THROW_BAD_ALLOC
70{
71 return ::operator new(size);
72}
73
74_LIBCXXABI_WEAK
75void*
76operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
77{
78 void* p = 0;
79#ifndef _LIBCXXABI_NO_EXCEPTIONS
80 try
81 {
82#endif // _LIBCXXABI_NO_EXCEPTIONS
83 p = ::operator new[](size);
84#ifndef _LIBCXXABI_NO_EXCEPTIONS
85 }
86 catch (...)
87 {
88 }
89#endif // _LIBCXXABI_NO_EXCEPTIONS
90 return p;
91}
92
93_LIBCXXABI_WEAK
94void
95operator delete(void* ptr) _NOEXCEPT
96{
97 if (ptr)
98 ::free(ptr);
99}
100
101_LIBCXXABI_WEAK
102void
103operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
104{
105 ::operator delete(ptr);
106}
107
108_LIBCXXABI_WEAK
109void
110operator delete(void* ptr, size_t) _NOEXCEPT
111{
112 ::operator delete(ptr);
113}
114
115_LIBCXXABI_WEAK
116void
117operator delete[] (void* ptr) _NOEXCEPT
118{
119 ::operator delete(ptr);
120}
121
122_LIBCXXABI_WEAK
123void
124operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
125{
126 ::operator delete[](ptr);
127}
128
129_LIBCXXABI_WEAK
130void
131operator delete[] (void* ptr, size_t) _NOEXCEPT
132{
133 ::operator delete[](ptr);
134}
135
136#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
137
138_LIBCXXABI_WEAK
139void *
140operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
141{
142 if (size == 0)
143 size = 1;
144 if (static_cast<size_t>(alignment) < sizeof(void*))
145 alignment = std::align_val_t(sizeof(void*));
146 void* p;
147#if defined(_LIBCPP_WIN32API)
148 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
149#else
150 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
151#endif
152 {
153 // If posix_memalign fails and there is a new_handler,
154 // call it to try free up memory.
155 std::new_handler nh = std::get_new_handler();
156 if (nh)
157 nh();
158 else {
159#ifndef _LIBCXXABI_NO_EXCEPTIONS
160 throw std::bad_alloc();
161#else
162 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
163 break;
164#endif
165 }
166 }
167 return p;
168}
169
170_LIBCXXABI_WEAK
171void*
172operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
173{
174 void* p = 0;
175#ifndef _LIBCXXABI_NO_EXCEPTIONS
176 try
177 {
178#endif // _LIBCXXABI_NO_EXCEPTIONS
179 p = ::operator new(size, alignment);
180#ifndef _LIBCXXABI_NO_EXCEPTIONS
181 }
182 catch (...)
183 {
184 }
185#endif // _LIBCXXABI_NO_EXCEPTIONS
186 return p;
187}
188
189_LIBCXXABI_WEAK
190void*
191operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
192{
193 return ::operator new(size, alignment);
194}
195
196_LIBCXXABI_WEAK
197void*
198operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
199{
200 void* p = 0;
201#ifndef _LIBCXXABI_NO_EXCEPTIONS
202 try
203 {
204#endif // _LIBCXXABI_NO_EXCEPTIONS
205 p = ::operator new[](size, alignment);
206#ifndef _LIBCXXABI_NO_EXCEPTIONS
207 }
208 catch (...)
209 {
210 }
211#endif // _LIBCXXABI_NO_EXCEPTIONS
212 return p;
213}
214
215_LIBCXXABI_WEAK
216void
217operator delete(void* ptr, std::align_val_t) _NOEXCEPT
218{
219 if (ptr)
220#if defined(_LIBCPP_WIN32API)
221 ::_aligned_free(ptr);
222#else
223 ::free(ptr);
224#endif
225}
226
227_LIBCXXABI_WEAK
228void
229operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
230{
231 ::operator delete(ptr, alignment);
232}
233
234_LIBCXXABI_WEAK
235void
236operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
237{
238 ::operator delete(ptr, alignment);
239}
240
241_LIBCXXABI_WEAK
242void
243operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
244{
245 ::operator delete(ptr, alignment);
246}
247
248_LIBCXXABI_WEAK
249void
250operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
251{
252 ::operator delete[](ptr, alignment);
253}
254
255_LIBCXXABI_WEAK
256void
257operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
258{
259 ::operator delete[](ptr, alignment);
260}
261
262#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
263