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 !__has_builtin(__builtin_operator_new) || \
112 __has_builtin(__builtin_operator_new) < 201802L || \
113 defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
114 !defined(__cpp_aligned_new) || __cpp_aligned_new < 201606
115 #define _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
118 namespace std // purposefully not using versioning namespace
121 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
122 struct _LIBCPP_TYPE_VIS nothrow_t {};
123 extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
125 class _LIBCPP_EXCEPTION_ABI bad_alloc
129 bad_alloc() _NOEXCEPT;
130 virtual ~bad_alloc() _NOEXCEPT;
131 virtual const char* what() const _NOEXCEPT;
134 class _LIBCPP_EXCEPTION_ABI bad_array_new_length
138 bad_array_new_length() _NOEXCEPT;
139 virtual ~bad_array_new_length() _NOEXCEPT;
140 virtual const char* what() const _NOEXCEPT;
143 typedef void (*new_handler)();
144 _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
145 _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
147 #endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
149 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
151 #if defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
153 class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
154 bad_array_length : public bad_alloc {
156 bad_array_length() _NOEXCEPT;
157 virtual ~bad_array_length() _NOEXCEPT;
158 virtual const char* what() const _NOEXCEPT;
161 #define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
163 #endif // defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
165 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
166 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || _LIBCPP_STD_VER > 14
167 #ifndef _LIBCPP_CXX03_LANG
168 enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
170 enum align_val_t { __zero = 0, __max = (size_t)-1 };
177 #if defined(_LIBCPP_CXX03_LANG)
178 #define _THROW_BAD_ALLOC throw(std::bad_alloc)
180 #define _THROW_BAD_ALLOC
183 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
185 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
186 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
187 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
188 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
189 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
190 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
193 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
194 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
195 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
196 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
197 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
198 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
201 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
202 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
203 _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;
204 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
205 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
206 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
207 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
210 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
211 _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;
212 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
213 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
214 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
215 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
219 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
220 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
221 inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
222 inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
224 #endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
226 _LIBCPP_BEGIN_NAMESPACE_STD
228 _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
229 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
230 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
232 return __align > alignment_of<max_align_t>::value;
236 inline _LIBCPP_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) {
237 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
238 if (__is_overaligned_for_new(__align)) {
239 const align_val_t __align_val = static_cast<align_val_t>(__align);
240 # ifdef _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
241 return ::operator new(__size, __align_val);
243 return __builtin_operator_new(__size, __align_val);
249 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
250 return ::operator new(__size);
252 return __builtin_operator_new(__size);
256 inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __align) {
257 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
258 if (__is_overaligned_for_new(__align)) {
259 const align_val_t __align_val = static_cast<align_val_t>(__align);
260 # ifdef _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
261 return ::operator delete(__ptr, __align_val);
263 return __builtin_operator_delete(__ptr, __align_val);
269 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
270 return ::operator delete(__ptr);
272 return __builtin_operator_delete(__ptr);
276 #ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
277 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
278 #ifndef _LIBCPP_NO_EXCEPTIONS
279 _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
281 void __throw_bad_array_length()
283 #ifndef _LIBCPP_NO_EXCEPTIONS
284 throw bad_array_length();
292 _LIBCPP_NODISCARD_AFTER_CXX17 inline
293 _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
295 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
296 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
297 #ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
298 return __builtin_launder(__p);
305 #if _LIBCPP_STD_VER > 14
307 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
308 constexpr _Tp* launder(_Tp* __p) noexcept
310 return _VSTD::__launder(__p);
314 _LIBCPP_END_NAMESPACE_STD
316 #endif // _LIBCPP_NEW