150
|
1 // -*- C++ -*-
|
236
|
2 //===----------------------------------------------------------------------===//
|
150
|
3 //
|
|
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
5 // See https://llvm.org/LICENSE.txt for license information.
|
|
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
7 //
|
|
8 //===----------------------------------------------------------------------===//
|
|
9
|
|
10 #ifndef _LIBCPP_NEW
|
|
11 #define _LIBCPP_NEW
|
|
12
|
|
13 /*
|
|
14 new synopsis
|
|
15
|
|
16 namespace std
|
|
17 {
|
|
18
|
|
19 class bad_alloc
|
|
20 : public exception
|
|
21 {
|
|
22 public:
|
|
23 bad_alloc() noexcept;
|
|
24 bad_alloc(const bad_alloc&) noexcept;
|
|
25 bad_alloc& operator=(const bad_alloc&) noexcept;
|
|
26 virtual const char* what() const noexcept;
|
|
27 };
|
|
28
|
|
29 class bad_array_new_length : public bad_alloc // C++14
|
|
30 {
|
|
31 public:
|
|
32 bad_array_new_length() noexcept;
|
|
33 };
|
|
34
|
|
35 enum class align_val_t : size_t {}; // C++17
|
|
36
|
|
37 struct destroying_delete_t { // C++20
|
|
38 explicit destroying_delete_t() = default;
|
|
39 };
|
|
40 inline constexpr destroying_delete_t destroying_delete{}; // C++20
|
|
41
|
|
42 struct nothrow_t { explicit nothrow_t() = default; };
|
|
43 extern const nothrow_t nothrow;
|
|
44 typedef void (*new_handler)();
|
|
45 new_handler set_new_handler(new_handler new_p) noexcept;
|
|
46 new_handler get_new_handler() noexcept;
|
|
47
|
|
48 // 21.6.4, pointer optimization barrier
|
|
49 template <class T> constexpr T* launder(T* p) noexcept; // C++17
|
|
50 } // std
|
|
51
|
221
|
52 void* operator new(std::size_t size); // replaceable, nodiscard in C++20
|
|
53 void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20
|
|
54 void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
|
150
|
55 void* operator new(std::size_t size, std::align_val_t alignment,
|
221
|
56 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
|
150
|
57 void operator delete(void* ptr) noexcept; // replaceable
|
|
58 void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
|
|
59 void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17
|
|
60 void operator delete(void* ptr, std::size_t size,
|
|
61 std::align_val_t alignment) noexcept; // replaceable, C++17
|
|
62 void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
|
|
63 void operator delete(void* ptr, std:align_val_t alignment,
|
|
64 const std::nothrow_t&) noexcept; // replaceable, C++17
|
|
65
|
221
|
66 void* operator new[](std::size_t size); // replaceable, nodiscard in C++20
|
150
|
67 void* operator new[](std::size_t size,
|
221
|
68 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20
|
|
69 void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
|
150
|
70 void* operator new[](std::size_t size, std::align_val_t alignment,
|
221
|
71 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
|
150
|
72 void operator delete[](void* ptr) noexcept; // replaceable
|
|
73 void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
|
|
74 void operator delete[](void* ptr,
|
|
75 std::align_val_t alignment) noexcept; // replaceable, C++17
|
|
76 void operator delete[](void* ptr, std::size_t size,
|
|
77 std::align_val_t alignment) noexcept; // replaceable, C++17
|
|
78 void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
|
|
79 void operator delete[](void* ptr, std::align_val_t alignment,
|
|
80 const std::nothrow_t&) noexcept; // replaceable, C++17
|
|
81
|
221
|
82 void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20
|
|
83 void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20
|
150
|
84 void operator delete (void* ptr, void*) noexcept;
|
|
85 void operator delete[](void* ptr, void*) noexcept;
|
|
86
|
|
87 */
|
|
88
|
236
|
89 #include <__assert> // all public C++ headers provide the assertion handler
|
221
|
90 #include <__availability>
|
150
|
91 #include <__config>
|
221
|
92 #include <cstddef>
|
|
93 #include <cstdlib>
|
150
|
94 #include <exception>
|
|
95 #include <type_traits>
|
|
96 #include <version>
|
|
97
|
|
98 #if defined(_LIBCPP_ABI_VCRUNTIME)
|
|
99 #include <new.h>
|
|
100 #endif
|
|
101
|
|
102 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
236
|
103 # pragma GCC system_header
|
150
|
104 #endif
|
|
105
|
|
106 #if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
|
|
107 #define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
|
|
108 #endif
|
|
109
|
|
110 #if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
|
|
111 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
|
|
112 # define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
113 #endif
|
|
114
|
|
115 #if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
|
|
116 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
|
|
117 # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
|
|
118 #endif
|
|
119
|
|
120 namespace std // purposefully not using versioning namespace
|
|
121 {
|
|
122
|
|
123 #if !defined(_LIBCPP_ABI_VCRUNTIME)
|
|
124 struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
|
|
125 extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
|
|
126
|
|
127 class _LIBCPP_EXCEPTION_ABI bad_alloc
|
|
128 : public exception
|
|
129 {
|
|
130 public:
|
|
131 bad_alloc() _NOEXCEPT;
|
236
|
132 ~bad_alloc() _NOEXCEPT override;
|
|
133 const char* what() const _NOEXCEPT override;
|
150
|
134 };
|
|
135
|
|
136 class _LIBCPP_EXCEPTION_ABI bad_array_new_length
|
|
137 : public bad_alloc
|
|
138 {
|
|
139 public:
|
|
140 bad_array_new_length() _NOEXCEPT;
|
236
|
141 ~bad_array_new_length() _NOEXCEPT override;
|
|
142 const char* what() const _NOEXCEPT override;
|
150
|
143 };
|
|
144
|
|
145 typedef void (*new_handler)();
|
|
146 _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
|
|
147 _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
|
|
148
|
236
|
149 #elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
|
|
150
|
|
151 // When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
|
|
152 // since they would normally be provided in vcruntime_exception.h
|
|
153 class bad_alloc : public exception {
|
|
154 public:
|
|
155 bad_alloc() noexcept : exception("bad allocation") {}
|
|
156
|
|
157 private:
|
|
158 friend class bad_array_new_length;
|
|
159
|
|
160 bad_alloc(char const* const __message) noexcept : exception(__message) {}
|
|
161 };
|
|
162
|
|
163 class bad_array_new_length : public bad_alloc {
|
|
164 public:
|
|
165 bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
|
|
166 };
|
|
167 #endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
|
150
|
168
|
|
169 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
|
|
170
|
236
|
171 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
|
|
172 void __throw_bad_array_new_length()
|
|
173 {
|
|
174 #ifndef _LIBCPP_NO_EXCEPTIONS
|
|
175 throw bad_array_new_length();
|
|
176 #else
|
|
177 _VSTD::abort();
|
|
178 #endif
|
|
179 }
|
|
180
|
150
|
181 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
|
|
182 !defined(_LIBCPP_ABI_VCRUNTIME)
|
|
183 #ifndef _LIBCPP_CXX03_LANG
|
|
184 enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
|
|
185 #else
|
|
186 enum align_val_t { __zero = 0, __max = (size_t)-1 };
|
|
187 #endif
|
|
188 #endif
|
|
189
|
|
190 #if _LIBCPP_STD_VER > 17
|
|
191 // Enable the declaration even if the compiler doesn't support the language
|
|
192 // feature.
|
|
193 struct destroying_delete_t {
|
|
194 explicit destroying_delete_t() = default;
|
|
195 };
|
236
|
196 inline constexpr destroying_delete_t destroying_delete{};
|
150
|
197 #endif // _LIBCPP_STD_VER > 17
|
|
198
|
236
|
199 } // namespace std
|
150
|
200
|
|
201 #if defined(_LIBCPP_CXX03_LANG)
|
|
202 #define _THROW_BAD_ALLOC throw(std::bad_alloc)
|
|
203 #else
|
|
204 #define _THROW_BAD_ALLOC
|
|
205 #endif
|
|
206
|
|
207 #if !defined(_LIBCPP_ABI_VCRUNTIME)
|
|
208
|
|
209 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
|
|
210 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
|
211 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
|
|
212 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
|
|
213 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
214 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
|
|
215 #endif
|
|
216
|
|
217 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
|
|
218 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
|
219 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
|
|
220 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
|
|
221 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
222 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
|
|
223 #endif
|
|
224
|
|
225 #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
|
|
226 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
|
|
227 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
|
228 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
|
|
229 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
|
|
230 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
231 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
|
|
232 #endif
|
|
233
|
|
234 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
|
|
235 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
|
236 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
|
|
237 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
|
|
238 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
239 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
|
|
240 #endif
|
|
241 #endif
|
|
242
|
|
243 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
|
|
244 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
|
|
245 inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
|
|
246 inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
|
|
247
|
|
248 #endif // !_LIBCPP_ABI_VCRUNTIME
|
|
249
|
|
250 _LIBCPP_BEGIN_NAMESPACE_STD
|
|
251
|
|
252 _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
|
|
253 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
|
|
254 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
|
|
255 #else
|
|
256 return __align > alignment_of<max_align_t>::value;
|
|
257 #endif
|
|
258 }
|
|
259
|
221
|
260 template <class ..._Args>
|
|
261 _LIBCPP_INLINE_VISIBILITY
|
|
262 void* __libcpp_operator_new(_Args ...__args) {
|
|
263 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
|
|
264 return __builtin_operator_new(__args...);
|
|
265 #else
|
|
266 return ::operator new(__args...);
|
|
267 #endif
|
|
268 }
|
|
269
|
|
270 template <class ..._Args>
|
|
271 _LIBCPP_INLINE_VISIBILITY
|
|
272 void __libcpp_operator_delete(_Args ...__args) {
|
|
273 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
|
|
274 __builtin_operator_delete(__args...);
|
|
275 #else
|
|
276 ::operator delete(__args...);
|
|
277 #endif
|
|
278 }
|
|
279
|
|
280 inline _LIBCPP_INLINE_VISIBILITY
|
|
281 void *__libcpp_allocate(size_t __size, size_t __align) {
|
150
|
282 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
|
|
283 if (__is_overaligned_for_new(__align)) {
|
|
284 const align_val_t __align_val = static_cast<align_val_t>(__align);
|
221
|
285 return __libcpp_operator_new(__size, __align_val);
|
150
|
286 }
|
221
|
287 #endif
|
|
288
|
|
289 (void)__align;
|
|
290 return __libcpp_operator_new(__size);
|
|
291 }
|
|
292
|
|
293 template <class ..._Args>
|
|
294 _LIBCPP_INLINE_VISIBILITY
|
|
295 void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
|
|
296 #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
|
|
297 (void)__size;
|
|
298 return __libcpp_operator_delete(__ptr, __args...);
|
150
|
299 #else
|
221
|
300 return __libcpp_operator_delete(__ptr, __size, __args...);
|
150
|
301 #endif
|
|
302 }
|
|
303
|
221
|
304 inline _LIBCPP_INLINE_VISIBILITY
|
|
305 void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
|
150
|
306 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
|
221
|
307 (void)__align;
|
150
|
308 return __do_deallocate_handle_size(__ptr, __size);
|
|
309 #else
|
|
310 if (__is_overaligned_for_new(__align)) {
|
|
311 const align_val_t __align_val = static_cast<align_val_t>(__align);
|
|
312 return __do_deallocate_handle_size(__ptr, __size, __align_val);
|
|
313 } else {
|
|
314 return __do_deallocate_handle_size(__ptr, __size);
|
|
315 }
|
|
316 #endif
|
221
|
317 }
|
150
|
318
|
221
|
319 inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
|
150
|
320 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
|
221
|
321 (void)__align;
|
|
322 return __libcpp_operator_delete(__ptr);
|
150
|
323 #else
|
|
324 if (__is_overaligned_for_new(__align)) {
|
|
325 const align_val_t __align_val = static_cast<align_val_t>(__align);
|
221
|
326 return __libcpp_operator_delete(__ptr, __align_val);
|
150
|
327 } else {
|
221
|
328 return __libcpp_operator_delete(__ptr);
|
150
|
329 }
|
|
330 #endif
|
|
331 }
|
|
332
|
221
|
333 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
|
|
334 // Low-level helpers to call the aligned allocation and deallocation functions
|
|
335 // on the target platform. This is used to implement libc++'s own memory
|
|
336 // allocation routines -- if you need to allocate memory inside the library,
|
|
337 // chances are that you want to use `__libcpp_allocate` instead.
|
|
338 //
|
|
339 // Returns the allocated memory, or `nullptr` on failure.
|
|
340 inline _LIBCPP_INLINE_VISIBILITY
|
|
341 void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
|
|
342 #if defined(_LIBCPP_MSVCRT_LIKE)
|
|
343 return ::_aligned_malloc(__size, __alignment);
|
|
344 #else
|
|
345 void* __result = nullptr;
|
|
346 (void)::posix_memalign(&__result, __alignment, __size);
|
|
347 // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
|
|
348 return __result;
|
|
349 #endif
|
150
|
350 }
|
|
351
|
221
|
352 inline _LIBCPP_INLINE_VISIBILITY
|
|
353 void __libcpp_aligned_free(void* __ptr) {
|
|
354 #if defined(_LIBCPP_MSVCRT_LIKE)
|
|
355 ::_aligned_free(__ptr);
|
|
356 #else
|
|
357 ::free(__ptr);
|
|
358 #endif
|
|
359 }
|
|
360 #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
|
|
361
|
|
362
|
150
|
363 template <class _Tp>
|
236
|
364 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
|
150
|
365 _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
|
|
366 {
|
|
367 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
|
236
|
368 static_assert (!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void" );
|
150
|
369 return __builtin_launder(__p);
|
|
370 }
|
|
371
|
|
372 #if _LIBCPP_STD_VER > 14
|
|
373 template <class _Tp>
|
236
|
374 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
|
150
|
375 constexpr _Tp* launder(_Tp* __p) noexcept
|
|
376 {
|
|
377 return _VSTD::__launder(__p);
|
|
378 }
|
|
379 #endif
|
|
380
|
236
|
381 #if _LIBCPP_STD_VER > 14
|
|
382
|
|
383 #if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
|
|
384
|
|
385 inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
|
|
386 inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
|
|
387
|
|
388 #endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
|
|
389
|
|
390 #endif // _LIBCPP_STD_VER > 14
|
|
391
|
150
|
392 _LIBCPP_END_NAMESPACE_STD
|
|
393
|
221
|
394 #endif // _LIBCPP_NEW
|