2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
24 bad_alloc(const bad_alloc&) noexcept;
25 bad_alloc& operator=(const bad_alloc&) noexcept;
26 virtual const char* what() const noexcept;
29 class bad_array_new_length : public bad_alloc // C++14
32 bad_array_new_length() noexcept;
35 enum class align_val_t : size_t {}; // C++17
37 struct destroying_delete_t { // C++20
38 explicit destroying_delete_t() = default;
40 inline constexpr destroying_delete_t destroying_delete{}; // C++20
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;
48 // 21.6.4, pointer optimization barrier
49 template <class T> constexpr T* launder(T* p) noexcept; // C++17
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
55 void* operator new(std::size_t size, std::align_val_t alignment,
56 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
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
66 void* operator new[](std::size_t size); // replaceable, nodiscard in C++20
67 void* operator new[](std::size_t size,
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
70 void* operator new[](std::size_t size, std::align_val_t alignment,
71 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
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
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
84 void operator delete (void* ptr, void*) noexcept;
85 void operator delete[](void* ptr, void*) noexcept;
89 #include <__assert> // all public C++ headers provide the assertion handler
90 #include <__availability>
92 #include <__exception/exception.h>
93 #include <__type_traits/alignment_of.h>
94 #include <__type_traits/is_function.h>
95 #include <__type_traits/is_same.h>
96 #include <__type_traits/remove_cv.h>
97 #include <__verbose_abort>
102 #if defined(_LIBCPP_ABI_VCRUNTIME)
106 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
107 # pragma GCC system_header
110 #if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
111 #define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
114 #if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
115 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
116 # define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
119 #if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
120 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
121 # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
124 namespace std // purposefully not using versioning namespace
127 #if !defined(_LIBCPP_ABI_VCRUNTIME)
128 struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t { explicit nothrow_t() = default; };
129 extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;
131 class _LIBCPP_EXPORTED_FROM_ABI bad_alloc
135 bad_alloc() _NOEXCEPT;
136 _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;
137 _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
138 ~bad_alloc() _NOEXCEPT override;
139 const char* what() const _NOEXCEPT override;
142 class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length
146 bad_array_new_length() _NOEXCEPT;
147 _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;
148 _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
149 ~bad_array_new_length() _NOEXCEPT override;
150 const char* what() const _NOEXCEPT override;
153 typedef void (*new_handler)();
154 _LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
155 _LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
157 #elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
159 // When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
160 // since they would normally be provided in vcruntime_exception.h
161 class bad_alloc : public exception {
163 bad_alloc() noexcept : exception("bad allocation") {}
166 friend class bad_array_new_length;
168 bad_alloc(char const* const __message) noexcept : exception(__message) {}
171 class bad_array_new_length : public bad_alloc {
173 bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
175 #endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
177 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec
179 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
180 void __throw_bad_array_new_length()
182 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
183 throw bad_array_new_length();
185 _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
189 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
190 !defined(_LIBCPP_ABI_VCRUNTIME)
191 #ifndef _LIBCPP_CXX03_LANG
192 enum class align_val_t : size_t { };
194 enum align_val_t { __zero = 0, __max = (size_t)-1 };
198 #if _LIBCPP_STD_VER >= 20
199 // Enable the declaration even if the compiler doesn't support the language
201 struct destroying_delete_t {
202 explicit destroying_delete_t() = default;
204 inline constexpr destroying_delete_t destroying_delete{};
205 #endif // _LIBCPP_STD_VER >= 20
209 #if defined(_LIBCPP_CXX03_LANG)
210 #define _THROW_BAD_ALLOC throw(std::bad_alloc)
212 #define _THROW_BAD_ALLOC
215 #if !defined(_LIBCPP_ABI_VCRUNTIME)
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;
225 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
226 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
227 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
228 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
229 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
230 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
233 #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
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;
242 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
243 _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;
244 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
245 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
246 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
247 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
251 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
252 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
253 inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
254 inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
256 #endif // !_LIBCPP_ABI_VCRUNTIME
258 _LIBCPP_BEGIN_NAMESPACE_STD
260 _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
261 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
262 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
264 return __align > alignment_of<max_align_t>::value;
268 template <class ..._Args>
269 _LIBCPP_INLINE_VISIBILITY
270 void* __libcpp_operator_new(_Args ...__args) {
271 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
272 return __builtin_operator_new(__args...);
274 return ::operator new(__args...);
278 template <class ..._Args>
279 _LIBCPP_INLINE_VISIBILITY
280 void __libcpp_operator_delete(_Args ...__args) {
281 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
282 __builtin_operator_delete(__args...);
284 ::operator delete(__args...);
288 inline _LIBCPP_INLINE_VISIBILITY
289 void *__libcpp_allocate(size_t __size, size_t __align) {
290 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
291 if (__is_overaligned_for_new(__align)) {
292 const align_val_t __align_val = static_cast<align_val_t>(__align);
293 return __libcpp_operator_new(__size, __align_val);
298 return __libcpp_operator_new(__size);
301 template <class ..._Args>
302 _LIBCPP_INLINE_VISIBILITY
303 void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
304 #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
306 return std::__libcpp_operator_delete(__ptr, __args...);
308 return std::__libcpp_operator_delete(__ptr, __size, __args...);
312 inline _LIBCPP_INLINE_VISIBILITY
313 void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
314 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
316 return __do_deallocate_handle_size(__ptr, __size);
318 if (__is_overaligned_for_new(__align)) {
319 const align_val_t __align_val = static_cast<align_val_t>(__align);
320 return __do_deallocate_handle_size(__ptr, __size, __align_val);
322 return __do_deallocate_handle_size(__ptr, __size);
327 inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
328 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
330 return __libcpp_operator_delete(__ptr);
332 if (__is_overaligned_for_new(__align)) {
333 const align_val_t __align_val = static_cast<align_val_t>(__align);
334 return __libcpp_operator_delete(__ptr, __align_val);
336 return __libcpp_operator_delete(__ptr);
342 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
343 _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
345 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
346 static_assert (!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void" );
347 return __builtin_launder(__p);
350 #if _LIBCPP_STD_VER >= 17
352 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
353 constexpr _Tp* launder(_Tp* __p) noexcept
355 return _VSTD::__launder(__p);
359 #if _LIBCPP_STD_VER >= 17
361 #if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
363 inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
364 inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
366 #endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
368 #endif // _LIBCPP_STD_VER >= 17
370 _LIBCPP_END_NAMESPACE_STD
372 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
373 # include <exception>
374 # include <type_traits>
377 #endif // _LIBCPP_NEW