2 //===----------------------------- new ------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
25 bad_alloc(const bad_alloc&) noexcept;
26 bad_alloc& operator=(const bad_alloc&) noexcept;
27 virtual const char* what() const noexcept;
30 class bad_array_length : public bad_alloc // FIXME: Not part of C++
33 bad_array_length() noexcept;
36 class bad_array_new_length : public bad_alloc // C++14
39 bad_array_new_length() noexcept;
42 enum class align_val_t : size_t {}; // C++17
44 extern const nothrow_t nothrow;
45 typedef void (*new_handler)();
46 new_handler set_new_handler(new_handler new_p) noexcept;
47 new_handler get_new_handler() noexcept;
49 // 21.6.4, pointer optimization barrier
50 template <class T> constexpr T* launder(T* p) noexcept; // C++17
53 void* operator new(std::size_t size); // replaceable, nodiscard in C++2a
54 void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++2a
55 void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a
56 void* operator new(std::size_t size, std::align_val_t alignment,
57 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a
58 void operator delete(void* ptr) noexcept; // replaceable
59 void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
60 void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17
61 void operator delete(void* ptr, std::size_t size,
62 std::align_val_t alignment) noexcept; // replaceable, C++17
63 void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
64 void operator delete(void* ptr, std:align_val_t alignment,
65 const std::nothrow_t&) noexcept; // replaceable, C++17
67 void* operator new[](std::size_t size); // replaceable, nodiscard in C++2a
68 void* operator new[](std::size_t size,
69 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++2a
70 void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a
71 void* operator new[](std::size_t size, std::align_val_t alignment,
72 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a
73 void operator delete[](void* ptr) noexcept; // replaceable
74 void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
75 void operator delete[](void* ptr,
76 std::align_val_t alignment) noexcept; // replaceable, C++17
77 void operator delete[](void* ptr, std::size_t size,
78 std::align_val_t alignment) noexcept; // replaceable, C++17
79 void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
80 void operator delete[](void* ptr, std::align_val_t alignment,
81 const std::nothrow_t&) noexcept; // replaceable, C++17
83 void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++2a
84 void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++2a
85 void operator delete (void* ptr, void*) noexcept;
86 void operator delete[](void* ptr, void*) noexcept;
92 #include <type_traits>
94 #ifdef _LIBCPP_NO_EXCEPTIONS
98 #if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME)
102 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
103 #pragma GCC system_header
106 #if !(defined(_LIBCPP_BUILDING_LIBRARY) || _LIBCPP_STD_VER >= 14 || \
107 (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309))
108 # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
111 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) && \
112 (!(defined(_LIBCPP_BUILDING_LIBRARY) || _LIBCPP_STD_VER > 14 || \
113 (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606)))
114 # define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
118 #if !__has_builtin(__builtin_operator_new) || \
119 __has_builtin(__builtin_operator_new) < 201802L || \
120 defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
121 !defined(__cpp_aligned_new) || __cpp_aligned_new < 201606
122 #define _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
125 namespace std // purposefully not using versioning namespace
128 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
129 struct _LIBCPP_TYPE_VIS nothrow_t {};
130 extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
132 class _LIBCPP_EXCEPTION_ABI bad_alloc
136 bad_alloc() _NOEXCEPT;
137 virtual ~bad_alloc() _NOEXCEPT;
138 virtual const char* what() const _NOEXCEPT;
141 class _LIBCPP_EXCEPTION_ABI bad_array_new_length
145 bad_array_new_length() _NOEXCEPT;
146 virtual ~bad_array_new_length() _NOEXCEPT;
147 virtual const char* what() const _NOEXCEPT;
150 typedef void (*new_handler)();
151 _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
152 _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
154 #endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
156 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
158 #if defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
160 class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
161 bad_array_length : public bad_alloc {
163 bad_array_length() _NOEXCEPT;
164 virtual ~bad_array_length() _NOEXCEPT;
165 virtual const char* what() const _NOEXCEPT;
168 #define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
170 #endif // defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
172 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
173 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || _LIBCPP_STD_VER > 14
174 #ifndef _LIBCPP_CXX03_LANG
175 enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
177 enum align_val_t { __zero = 0, __max = (size_t)-1 };
184 #if defined(_LIBCPP_CXX03_LANG)
185 #define _THROW_BAD_ALLOC throw(std::bad_alloc)
187 #define _THROW_BAD_ALLOC
190 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
192 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
193 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
194 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
195 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
196 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
197 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
200 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
201 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
202 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
203 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
204 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
205 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
208 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
209 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
210 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
211 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
212 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
213 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
214 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
217 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
218 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
219 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
220 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
221 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
222 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
226 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
227 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
228 inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
229 inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
231 #endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
233 _LIBCPP_BEGIN_NAMESPACE_STD
235 _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
236 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
237 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
239 return __align > alignment_of<max_align_t>::value;
243 inline _LIBCPP_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) {
244 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
245 if (__is_overaligned_for_new(__align)) {
246 const align_val_t __align_val = static_cast<align_val_t>(__align);
247 # ifdef _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
248 return ::operator new(__size, __align_val);
250 return __builtin_operator_new(__size, __align_val);
256 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
257 return ::operator new(__size);
259 return __builtin_operator_new(__size);
263 inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __align) {
264 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
265 if (__is_overaligned_for_new(__align)) {
266 const align_val_t __align_val = static_cast<align_val_t>(__align);
267 # ifdef _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
268 return ::operator delete(__ptr, __align_val);
270 return __builtin_operator_delete(__ptr, __align_val);
276 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
277 return ::operator delete(__ptr);
279 return __builtin_operator_delete(__ptr);
283 #ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
284 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
285 #ifndef _LIBCPP_NO_EXCEPTIONS
286 _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
288 void __throw_bad_array_length()
290 #ifndef _LIBCPP_NO_EXCEPTIONS
291 throw bad_array_length();
299 _LIBCPP_NODISCARD_AFTER_CXX17 inline
300 _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
302 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
303 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
304 #ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
305 return __builtin_launder(__p);
312 #if _LIBCPP_STD_VER > 14
314 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
315 constexpr _Tp* launder(_Tp* __p) noexcept
317 return _VSTD::__launder(__p);
321 _LIBCPP_END_NAMESPACE_STD
323 #endif // _LIBCPP_NEW