150
|
1 // -*- C++ -*-
|
|
2 //===----------------------------- new ------------------------------------===//
|
|
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
|
221
|
89 #include <__availability>
|
150
|
90 #include <__config>
|
221
|
91 #include <cstddef>
|
|
92 #include <cstdlib>
|
150
|
93 #include <exception>
|
|
94 #include <type_traits>
|
|
95 #include <version>
|
|
96
|
|
97 #if defined(_LIBCPP_ABI_VCRUNTIME)
|
|
98 #include <new.h>
|
|
99 #endif
|
|
100
|
|
101 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
|
102 #pragma GCC system_header
|
|
103 #endif
|
|
104
|
|
105 #if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
|
|
106 #define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
|
|
107 #endif
|
|
108
|
|
109 #if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
|
|
110 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
|
|
111 # define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
112 #endif
|
|
113
|
|
114 #if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
|
|
115 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
|
|
116 # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
|
|
117 #endif
|
|
118
|
|
119 namespace std // purposefully not using versioning namespace
|
|
120 {
|
|
121
|
|
122 #if !defined(_LIBCPP_ABI_VCRUNTIME)
|
|
123 struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
|
|
124 extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
|
|
125
|
|
126 class _LIBCPP_EXCEPTION_ABI bad_alloc
|
|
127 : public exception
|
|
128 {
|
|
129 public:
|
|
130 bad_alloc() _NOEXCEPT;
|
|
131 virtual ~bad_alloc() _NOEXCEPT;
|
|
132 virtual const char* what() const _NOEXCEPT;
|
|
133 };
|
|
134
|
|
135 class _LIBCPP_EXCEPTION_ABI bad_array_new_length
|
|
136 : public bad_alloc
|
|
137 {
|
|
138 public:
|
|
139 bad_array_new_length() _NOEXCEPT;
|
|
140 virtual ~bad_array_new_length() _NOEXCEPT;
|
|
141 virtual const char* what() const _NOEXCEPT;
|
|
142 };
|
|
143
|
|
144 typedef void (*new_handler)();
|
|
145 _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
|
|
146 _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
|
|
147
|
|
148 #endif // !_LIBCPP_ABI_VCRUNTIME
|
|
149
|
|
150 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
|
|
151
|
|
152 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
|
|
153 !defined(_LIBCPP_ABI_VCRUNTIME)
|
|
154 #ifndef _LIBCPP_CXX03_LANG
|
|
155 enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
|
|
156 #else
|
|
157 enum align_val_t { __zero = 0, __max = (size_t)-1 };
|
|
158 #endif
|
|
159 #endif
|
|
160
|
|
161 #if _LIBCPP_STD_VER > 17
|
|
162 // Enable the declaration even if the compiler doesn't support the language
|
|
163 // feature.
|
|
164 struct destroying_delete_t {
|
|
165 explicit destroying_delete_t() = default;
|
|
166 };
|
|
167 _LIBCPP_INLINE_VAR constexpr destroying_delete_t destroying_delete{};
|
|
168 #endif // _LIBCPP_STD_VER > 17
|
|
169
|
|
170 } // std
|
|
171
|
|
172 #if defined(_LIBCPP_CXX03_LANG)
|
|
173 #define _THROW_BAD_ALLOC throw(std::bad_alloc)
|
|
174 #else
|
|
175 #define _THROW_BAD_ALLOC
|
|
176 #endif
|
|
177
|
|
178 #if !defined(_LIBCPP_ABI_VCRUNTIME)
|
|
179
|
|
180 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
|
|
181 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
|
182 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
|
|
183 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
|
|
184 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
185 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
|
|
186 #endif
|
|
187
|
|
188 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
|
|
189 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
|
|
190 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
|
|
191 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
|
|
192 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
193 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
|
|
194 #endif
|
|
195
|
|
196 #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
|
|
197 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
|
|
198 _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;
|
|
199 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
|
|
200 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
|
|
201 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
202 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
|
|
203 #endif
|
|
204
|
|
205 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
|
|
206 _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;
|
|
207 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
|
|
208 _LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
|
|
209 #ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
|
|
210 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
|
|
211 #endif
|
|
212 #endif
|
|
213
|
|
214 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
|
|
215 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
|
|
216 inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
|
|
217 inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
|
|
218
|
|
219 #endif // !_LIBCPP_ABI_VCRUNTIME
|
|
220
|
|
221 _LIBCPP_BEGIN_NAMESPACE_STD
|
|
222
|
|
223 _LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
|
|
224 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
|
|
225 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
|
|
226 #else
|
|
227 return __align > alignment_of<max_align_t>::value;
|
|
228 #endif
|
|
229 }
|
|
230
|
221
|
231 template <class ..._Args>
|
|
232 _LIBCPP_INLINE_VISIBILITY
|
|
233 void* __libcpp_operator_new(_Args ...__args) {
|
|
234 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
|
|
235 return __builtin_operator_new(__args...);
|
|
236 #else
|
|
237 return ::operator new(__args...);
|
|
238 #endif
|
|
239 }
|
|
240
|
|
241 template <class ..._Args>
|
|
242 _LIBCPP_INLINE_VISIBILITY
|
|
243 void __libcpp_operator_delete(_Args ...__args) {
|
|
244 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
|
|
245 __builtin_operator_delete(__args...);
|
|
246 #else
|
|
247 ::operator delete(__args...);
|
|
248 #endif
|
|
249 }
|
|
250
|
|
251 inline _LIBCPP_INLINE_VISIBILITY
|
|
252 void *__libcpp_allocate(size_t __size, size_t __align) {
|
150
|
253 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
|
|
254 if (__is_overaligned_for_new(__align)) {
|
|
255 const align_val_t __align_val = static_cast<align_val_t>(__align);
|
221
|
256 return __libcpp_operator_new(__size, __align_val);
|
150
|
257 }
|
221
|
258 #endif
|
|
259
|
|
260 (void)__align;
|
|
261 return __libcpp_operator_new(__size);
|
|
262 }
|
|
263
|
|
264 template <class ..._Args>
|
|
265 _LIBCPP_INLINE_VISIBILITY
|
|
266 void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
|
|
267 #ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
|
|
268 (void)__size;
|
|
269 return __libcpp_operator_delete(__ptr, __args...);
|
150
|
270 #else
|
221
|
271 return __libcpp_operator_delete(__ptr, __size, __args...);
|
150
|
272 #endif
|
|
273 }
|
|
274
|
221
|
275 inline _LIBCPP_INLINE_VISIBILITY
|
|
276 void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
|
150
|
277 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
|
221
|
278 (void)__align;
|
150
|
279 return __do_deallocate_handle_size(__ptr, __size);
|
|
280 #else
|
|
281 if (__is_overaligned_for_new(__align)) {
|
|
282 const align_val_t __align_val = static_cast<align_val_t>(__align);
|
|
283 return __do_deallocate_handle_size(__ptr, __size, __align_val);
|
|
284 } else {
|
|
285 return __do_deallocate_handle_size(__ptr, __size);
|
|
286 }
|
|
287 #endif
|
221
|
288 }
|
150
|
289
|
221
|
290 inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
|
150
|
291 #if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
|
221
|
292 (void)__align;
|
|
293 return __libcpp_operator_delete(__ptr);
|
150
|
294 #else
|
|
295 if (__is_overaligned_for_new(__align)) {
|
|
296 const align_val_t __align_val = static_cast<align_val_t>(__align);
|
221
|
297 return __libcpp_operator_delete(__ptr, __align_val);
|
150
|
298 } else {
|
221
|
299 return __libcpp_operator_delete(__ptr);
|
150
|
300 }
|
|
301 #endif
|
|
302 }
|
|
303
|
221
|
304 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
|
|
305 // Low-level helpers to call the aligned allocation and deallocation functions
|
|
306 // on the target platform. This is used to implement libc++'s own memory
|
|
307 // allocation routines -- if you need to allocate memory inside the library,
|
|
308 // chances are that you want to use `__libcpp_allocate` instead.
|
|
309 //
|
|
310 // Returns the allocated memory, or `nullptr` on failure.
|
|
311 inline _LIBCPP_INLINE_VISIBILITY
|
|
312 void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
|
|
313 #if defined(_LIBCPP_MSVCRT_LIKE)
|
|
314 return ::_aligned_malloc(__size, __alignment);
|
|
315 #else
|
|
316 void* __result = nullptr;
|
|
317 (void)::posix_memalign(&__result, __alignment, __size);
|
|
318 // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
|
|
319 return __result;
|
|
320 #endif
|
150
|
321 }
|
|
322
|
221
|
323 inline _LIBCPP_INLINE_VISIBILITY
|
|
324 void __libcpp_aligned_free(void* __ptr) {
|
|
325 #if defined(_LIBCPP_MSVCRT_LIKE)
|
|
326 ::_aligned_free(__ptr);
|
|
327 #else
|
|
328 ::free(__ptr);
|
|
329 #endif
|
|
330 }
|
|
331 #endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
|
|
332
|
|
333
|
150
|
334 template <class _Tp>
|
|
335 _LIBCPP_NODISCARD_AFTER_CXX17 inline
|
|
336 _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
|
|
337 {
|
|
338 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
|
|
339 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
|
|
340 #ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
|
|
341 return __builtin_launder(__p);
|
|
342 #else
|
|
343 return __p;
|
|
344 #endif
|
|
345 }
|
|
346
|
|
347
|
|
348 #if _LIBCPP_STD_VER > 14
|
|
349 template <class _Tp>
|
|
350 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
|
|
351 constexpr _Tp* launder(_Tp* __p) noexcept
|
|
352 {
|
|
353 return _VSTD::__launder(__p);
|
|
354 }
|
|
355 #endif
|
|
356
|
|
357 _LIBCPP_END_NAMESPACE_STD
|
|
358
|
221
|
359 #endif // _LIBCPP_NEW
|