1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
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
16namespace std
17{
18
19class bad_alloc
20 : public exception
21{
22public:
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
29class bad_array_new_length : public bad_alloc // C++14
30{
31public:
32 bad_array_new_length() noexcept;
33};
34
35enum class align_val_t : size_t {}; // C++17
36
37struct destroying_delete_t { // C++20
38 explicit destroying_delete_t() = default;
39};
40inline constexpr destroying_delete_t destroying_delete{}; // C++20
41
42struct nothrow_t { explicit nothrow_t() = default; };
43extern const nothrow_t nothrow;
44typedef void (*new_handler)();
45new_handler set_new_handler(new_handler new_p) noexcept;
46new_handler get_new_handler() noexcept;
47
48// 21.6.4, pointer optimization barrier
49template <class T> constexpr T* launder(T* p) noexcept; // C++17
50} // std
51
52void* operator new(std::size_t size); // replaceable, nodiscard in C++20
53void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20
54void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
55void* operator new(std::size_t size, std::align_val_t alignment,
56 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
57void operator delete(void* ptr) noexcept; // replaceable
58void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
59void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17
60void operator delete(void* ptr, std::size_t size,
61 std::align_val_t alignment) noexcept; // replaceable, C++17
62void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
63void operator delete(void* ptr, std:align_val_t alignment,
64 const std::nothrow_t&) noexcept; // replaceable, C++17
65
66void* operator new[](std::size_t size); // replaceable, nodiscard in C++20
67void* operator new[](std::size_t size,
68 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20
69void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
70void* operator new[](std::size_t size, std::align_val_t alignment,
71 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
72void operator delete[](void* ptr) noexcept; // replaceable
73void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
74void operator delete[](void* ptr,
75 std::align_val_t alignment) noexcept; // replaceable, C++17
76void operator delete[](void* ptr, std::size_t size,
77 std::align_val_t alignment) noexcept; // replaceable, C++17
78void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
79void operator delete[](void* ptr, std::align_val_t alignment,
80 const std::nothrow_t&) noexcept; // replaceable, C++17
81
82void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20
83void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20
84void operator delete (void* ptr, void*) noexcept;
85void operator delete[](void* ptr, void*) noexcept;
86
87*/
88
89#include <__assert> // all public C++ headers provide the assertion handler
90#include <__availability>
91#include <__config>
92#include <cstddef>
93#include <cstdlib>
94#include <exception>
95#include <type_traits>
96#include <version>
97
98#if defined(_LIBCPP_ABI_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(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
107#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
108#endif
109
110#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
111 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
112# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
113#endif
114
115#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
116 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
117# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
118#endif
119
120namespace std // purposefully not using versioning namespace
121{
122
123#if !defined(_LIBCPP_ABI_VCRUNTIME)
124struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
125extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
126
127class _LIBCPP_EXCEPTION_ABI bad_alloc
128 : public exception
129{
130public:
131 bad_alloc() _NOEXCEPT;
132 virtual ~bad_alloc() _NOEXCEPT;
133 virtual const char* what() const _NOEXCEPT;
134};
135
136class _LIBCPP_EXCEPTION_ABI bad_array_new_length
137 : public bad_alloc
138{
139public:
140 bad_array_new_length() _NOEXCEPT;
141 virtual ~bad_array_new_length() _NOEXCEPT;
142 virtual const char* what() const _NOEXCEPT;
143};
144
145typedef void (*new_handler)();
146_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
147_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
148
149#endif // !_LIBCPP_ABI_VCRUNTIME
150
151_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
152
153_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
154void __throw_bad_array_new_length()
155{
156#ifndef _LIBCPP_NO_EXCEPTIONS
157 throw bad_array_new_length();
158#else
159 _VSTD::abort();
160#endif
161}
162
163#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
164 !defined(_LIBCPP_ABI_VCRUNTIME)
165#ifndef _LIBCPP_CXX03_LANG
166enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
167#else
168enum align_val_t { __zero = 0, __max = (size_t)-1 };
169#endif
170#endif
171
172#if _LIBCPP_STD_VER > 17
173// Enable the declaration even if the compiler doesn't support the language
174// feature.
175struct destroying_delete_t {
176 explicit destroying_delete_t() = default;
177};
178inline constexpr destroying_delete_t destroying_delete{};
179#endif // _LIBCPP_STD_VER > 17
180
181} // namespace std
182
183#if defined(_LIBCPP_CXX03_LANG)
184#define _THROW_BAD_ALLOC throw(std::bad_alloc)
185#else
186#define _THROW_BAD_ALLOC
187#endif
188
189#if !defined(_LIBCPP_ABI_VCRUNTIME)
190
191_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
192_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
193_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
194_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
195#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
196_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
197#endif
198
199_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
200_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
201_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
202_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
203#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
204_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
205#endif
206
207#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
208_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
209_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;
210_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
211_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
212#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
213_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
214#endif
215
216_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
217_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;
218_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
219_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
220#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
221_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
222#endif
223#endif
224
225_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
226_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
227inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
228inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
229
230#endif // !_LIBCPP_ABI_VCRUNTIME
231
232_LIBCPP_BEGIN_NAMESPACE_STD
233
234_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
235#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
236 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
237#else
238 return __align > alignment_of<max_align_t>::value;
239#endif
240}
241
242template <class ..._Args>
243_LIBCPP_INLINE_VISIBILITY
244void* __libcpp_operator_new(_Args ...__args) {
245#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
246 return __builtin_operator_new(__args...);
247#else
248 return ::operator new(__args...);
249#endif
250}
251
252template <class ..._Args>
253_LIBCPP_INLINE_VISIBILITY
254void __libcpp_operator_delete(_Args ...__args) {
255#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
256 __builtin_operator_delete(__args...);
257#else
258 ::operator delete(__args...);
259#endif
260}
261
262inline _LIBCPP_INLINE_VISIBILITY
263void *__libcpp_allocate(size_t __size, 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 return __libcpp_operator_new(args: __size, args: __align_val);
268 }
269#endif
270
271 (void)__align;
272 return __libcpp_operator_new(args: __size);
273}
274
275template <class ..._Args>
276_LIBCPP_INLINE_VISIBILITY
277void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
278#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
279 (void)__size;
280 return __libcpp_operator_delete(__ptr, __args...);
281#else
282 return __libcpp_operator_delete(__ptr, __size, __args...);
283#endif
284}
285
286inline _LIBCPP_INLINE_VISIBILITY
287void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
288#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
289 (void)__align;
290 return __do_deallocate_handle_size(__ptr, __size);
291#else
292 if (__is_overaligned_for_new(__align)) {
293 const align_val_t __align_val = static_cast<align_val_t>(__align);
294 return __do_deallocate_handle_size(__ptr, __size, args: __align_val);
295 } else {
296 return __do_deallocate_handle_size(__ptr, __size);
297 }
298#endif
299}
300
301inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
302#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
303 (void)__align;
304 return __libcpp_operator_delete(__ptr);
305#else
306 if (__is_overaligned_for_new(__align)) {
307 const align_val_t __align_val = static_cast<align_val_t>(__align);
308 return __libcpp_operator_delete(args: __ptr, args: __align_val);
309 } else {
310 return __libcpp_operator_delete(args: __ptr);
311 }
312#endif
313}
314
315#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
316// Low-level helpers to call the aligned allocation and deallocation functions
317// on the target platform. This is used to implement libc++'s own memory
318// allocation routines -- if you need to allocate memory inside the library,
319// chances are that you want to use `__libcpp_allocate` instead.
320//
321// Returns the allocated memory, or `nullptr` on failure.
322inline _LIBCPP_INLINE_VISIBILITY
323void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
324#if defined(_LIBCPP_MSVCRT_LIKE)
325 return ::_aligned_malloc(__size, __alignment);
326#else
327 void* __result = nullptr;
328 (void)::posix_memalign(memptr: &__result, __alignment, __size);
329 // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
330 return __result;
331#endif
332}
333
334inline _LIBCPP_INLINE_VISIBILITY
335void __libcpp_aligned_free(void* __ptr) {
336#if defined(_LIBCPP_MSVCRT_LIKE)
337 ::_aligned_free(__ptr);
338#else
339 ::free(__ptr);
340#endif
341}
342#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
343
344
345template <class _Tp>
346_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
347_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
348{
349 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
350 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
351 return __builtin_launder(__p);
352}
353
354#if _LIBCPP_STD_VER > 14
355template <class _Tp>
356_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
357constexpr _Tp* launder(_Tp* __p) noexcept
358{
359 return _VSTD::__launder(__p);
360}
361#endif
362
363#if _LIBCPP_STD_VER > 14
364
365#if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
366
367inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
368inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
369
370#endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
371
372#endif // _LIBCPP_STD_VER > 14
373
374_LIBCPP_END_NAMESPACE_STD
375
376#endif // _LIBCPP_NEW
377

source code of flutter_engine/third_party/libcxx/include/new