]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libc++/include/new
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / libc++ / include / new
1 // -*- C++ -*-
2 //===----------------------------- new ------------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_NEW
12 #define _LIBCPP_NEW
13
14 /*
15     new synopsis
16
17 namespace std
18 {
19
20 class bad_alloc
21     : public exception
22 {
23 public:
24     bad_alloc() noexcept;
25     bad_alloc(const bad_alloc&) noexcept;
26     bad_alloc& operator=(const bad_alloc&) noexcept;
27     virtual const char* what() const noexcept;
28 };
29
30 class bad_array_length : public bad_alloc // FIXME: Not part of C++
31 {
32 public:
33     bad_array_length() noexcept;
34 };
35
36 class bad_array_new_length : public bad_alloc // C++14
37 {
38 public:
39     bad_array_new_length() noexcept;
40 };
41
42 enum class align_val_t : size_t {}; // C++17
43 struct nothrow_t {};
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;
48
49 // 21.6.4, pointer optimization barrier
50 template <class T> constexpr T* launder(T* p) noexcept; // C++17
51 }  // std
52
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
66
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
82
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;
87
88 */
89
90 #include <__config>
91 #include <exception>
92 #include <type_traits>
93 #include <cstddef>
94 #ifdef _LIBCPP_NO_EXCEPTIONS
95 #include <cstdlib>
96 #endif
97
98 #if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME)
99 #include <new.h>
100 #endif
101
102 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
103 #pragma GCC system_header
104 #endif
105
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
109 #endif
110
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
116 #endif
117
118 namespace std  // purposefully not using versioning namespace
119 {
120
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;
124
125 class _LIBCPP_EXCEPTION_ABI bad_alloc
126     : public exception
127 {
128 public:
129     bad_alloc() _NOEXCEPT;
130     virtual ~bad_alloc() _NOEXCEPT;
131     virtual const char* what() const _NOEXCEPT;
132 };
133
134 class _LIBCPP_EXCEPTION_ABI bad_array_new_length
135     : public bad_alloc
136 {
137 public:
138     bad_array_new_length() _NOEXCEPT;
139     virtual ~bad_array_new_length() _NOEXCEPT;
140     virtual const char* what() const _NOEXCEPT;
141 };
142
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;
146
147 #endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
148
149 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
150
151 #if defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
152
153 class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
154     bad_array_length : public bad_alloc {
155 public:
156     bad_array_length() _NOEXCEPT;
157     virtual ~bad_array_length() _NOEXCEPT;
158     virtual const char* what() const _NOEXCEPT;
159 };
160
161 #define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
162
163 #endif  // defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
164
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 { };
169 #else
170 enum align_val_t { __zero = 0, __max = (size_t)-1 };
171 #endif
172 #endif
173 #endif
174
175 }  // std
176
177 #if defined(_LIBCPP_CXX03_LANG)
178 #define _THROW_BAD_ALLOC throw(std::bad_alloc)
179 #else
180 #define _THROW_BAD_ALLOC
181 #endif
182
183 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
184
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;
191 #endif
192
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;
199 #endif
200
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;
208 #endif
209
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;
216 #endif
217 #endif
218
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 {}
223
224 #endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
225
226 _LIBCPP_BEGIN_NAMESPACE_STD
227
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__;
231 #else
232   return __align > alignment_of<max_align_t>::value;
233 #endif
234 }
235
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);
242 # else
243     return __builtin_operator_new(__size, __align_val);
244 # endif
245   }
246 #else
247   ((void)__align);
248 #endif
249 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
250   return ::operator new(__size);
251 #else
252   return __builtin_operator_new(__size);
253 #endif
254 }
255
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);
262 # else
263     return __builtin_operator_delete(__ptr, __align_val);
264 # endif
265   }
266 #else
267   ((void)__align);
268 #endif
269 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
270   return ::operator delete(__ptr);
271 #else
272   return __builtin_operator_delete(__ptr);
273 #endif
274 }
275
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
280 #endif
281 void __throw_bad_array_length()
282 {
283 #ifndef _LIBCPP_NO_EXCEPTIONS
284     throw bad_array_length();
285 #else
286         _VSTD::abort();
287 #endif
288 }
289 #endif
290
291 template <class _Tp>
292 _LIBCPP_NODISCARD_AFTER_CXX17 inline 
293 _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
294 {
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);
299 #else
300     return __p;
301 #endif
302 }
303
304
305 #if _LIBCPP_STD_VER > 14
306 template <class _Tp>
307 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
308 constexpr _Tp* launder(_Tp* __p) noexcept
309 {
310     return _VSTD::__launder(__p);
311 }
312 #endif
313
314 _LIBCPP_END_NAMESPACE_STD
315
316 #endif  // _LIBCPP_NEW