150
|
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
|
|
25 void *
|
|
26 operator 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
|
|
49 void*
|
|
50 operator 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
|
|
68 void*
|
|
69 operator new[](size_t size) _THROW_BAD_ALLOC
|
|
70 {
|
|
71 return ::operator new(size);
|
|
72 }
|
|
73
|
|
74 _LIBCXXABI_WEAK
|
|
75 void*
|
|
76 operator 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
|
|
94 void
|
|
95 operator delete(void* ptr) _NOEXCEPT
|
|
96 {
|
|
97 if (ptr)
|
|
98 ::free(ptr);
|
|
99 }
|
|
100
|
|
101 _LIBCXXABI_WEAK
|
|
102 void
|
|
103 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
|
|
104 {
|
|
105 ::operator delete(ptr);
|
|
106 }
|
|
107
|
|
108 _LIBCXXABI_WEAK
|
|
109 void
|
|
110 operator delete(void* ptr, size_t) _NOEXCEPT
|
|
111 {
|
|
112 ::operator delete(ptr);
|
|
113 }
|
|
114
|
|
115 _LIBCXXABI_WEAK
|
|
116 void
|
|
117 operator delete[] (void* ptr) _NOEXCEPT
|
|
118 {
|
|
119 ::operator delete(ptr);
|
|
120 }
|
|
121
|
|
122 _LIBCXXABI_WEAK
|
|
123 void
|
|
124 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
|
|
125 {
|
|
126 ::operator delete[](ptr);
|
|
127 }
|
|
128
|
|
129 _LIBCXXABI_WEAK
|
|
130 void
|
|
131 operator 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
|
|
139 void *
|
|
140 operator 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
|
|
171 void*
|
|
172 operator 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
|
|
190 void*
|
|
191 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
|
|
192 {
|
|
193 return ::operator new(size, alignment);
|
|
194 }
|
|
195
|
|
196 _LIBCXXABI_WEAK
|
|
197 void*
|
|
198 operator 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
|
|
216 void
|
|
217 operator 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
|
|
228 void
|
|
229 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
|
|
230 {
|
|
231 ::operator delete(ptr, alignment);
|
|
232 }
|
|
233
|
|
234 _LIBCXXABI_WEAK
|
|
235 void
|
|
236 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
|
|
237 {
|
|
238 ::operator delete(ptr, alignment);
|
|
239 }
|
|
240
|
|
241 _LIBCXXABI_WEAK
|
|
242 void
|
|
243 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
|
|
244 {
|
|
245 ::operator delete(ptr, alignment);
|
|
246 }
|
|
247
|
|
248 _LIBCXXABI_WEAK
|
|
249 void
|
|
250 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
|
|
251 {
|
|
252 ::operator delete[](ptr, alignment);
|
|
253 }
|
|
254
|
|
255 _LIBCXXABI_WEAK
|
|
256 void
|
|
257 operator 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
|