xref: /aosp_15_r20/external/libcxx/include/new (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker// -*- C++ -*-
2*58b9f456SAndroid Build Coastguard Worker//===----------------------------- new ------------------------------------===//
3*58b9f456SAndroid Build Coastguard Worker//
4*58b9f456SAndroid Build Coastguard Worker//                     The LLVM Compiler Infrastructure
5*58b9f456SAndroid Build Coastguard Worker//
6*58b9f456SAndroid Build Coastguard Worker// This file is dual licensed under the MIT and the University of Illinois Open
7*58b9f456SAndroid Build Coastguard Worker// Source Licenses. See LICENSE.TXT for details.
8*58b9f456SAndroid Build Coastguard Worker//
9*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
10*58b9f456SAndroid Build Coastguard Worker
11*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_NEW
12*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_NEW
13*58b9f456SAndroid Build Coastguard Worker
14*58b9f456SAndroid Build Coastguard Worker/*
15*58b9f456SAndroid Build Coastguard Worker    new synopsis
16*58b9f456SAndroid Build Coastguard Worker
17*58b9f456SAndroid Build Coastguard Workernamespace std
18*58b9f456SAndroid Build Coastguard Worker{
19*58b9f456SAndroid Build Coastguard Worker
20*58b9f456SAndroid Build Coastguard Workerclass bad_alloc
21*58b9f456SAndroid Build Coastguard Worker    : public exception
22*58b9f456SAndroid Build Coastguard Worker{
23*58b9f456SAndroid Build Coastguard Workerpublic:
24*58b9f456SAndroid Build Coastguard Worker    bad_alloc() noexcept;
25*58b9f456SAndroid Build Coastguard Worker    bad_alloc(const bad_alloc&) noexcept;
26*58b9f456SAndroid Build Coastguard Worker    bad_alloc& operator=(const bad_alloc&) noexcept;
27*58b9f456SAndroid Build Coastguard Worker    virtual const char* what() const noexcept;
28*58b9f456SAndroid Build Coastguard Worker};
29*58b9f456SAndroid Build Coastguard Worker
30*58b9f456SAndroid Build Coastguard Workerclass bad_array_new_length : public bad_alloc // C++14
31*58b9f456SAndroid Build Coastguard Worker{
32*58b9f456SAndroid Build Coastguard Workerpublic:
33*58b9f456SAndroid Build Coastguard Worker    bad_array_new_length() noexcept;
34*58b9f456SAndroid Build Coastguard Worker};
35*58b9f456SAndroid Build Coastguard Worker
36*58b9f456SAndroid Build Coastguard Workerenum class align_val_t : size_t {}; // C++17
37*58b9f456SAndroid Build Coastguard Workerstruct nothrow_t {};
38*58b9f456SAndroid Build Coastguard Workerextern const nothrow_t nothrow;
39*58b9f456SAndroid Build Coastguard Workertypedef void (*new_handler)();
40*58b9f456SAndroid Build Coastguard Workernew_handler set_new_handler(new_handler new_p) noexcept;
41*58b9f456SAndroid Build Coastguard Workernew_handler get_new_handler() noexcept;
42*58b9f456SAndroid Build Coastguard Worker
43*58b9f456SAndroid Build Coastguard Worker// 21.6.4, pointer optimization barrier
44*58b9f456SAndroid Build Coastguard Workertemplate <class T> constexpr T* launder(T* p) noexcept; // C++17
45*58b9f456SAndroid Build Coastguard Worker}  // std
46*58b9f456SAndroid Build Coastguard Worker
47*58b9f456SAndroid Build Coastguard Workervoid* operator new(std::size_t size);                                   // replaceable, nodiscard in C++2a
48*58b9f456SAndroid Build Coastguard Workervoid* operator new(std::size_t size, std::align_val_t alignment);       // replaceable, C++17, nodiscard in C++2a
49*58b9f456SAndroid Build Coastguard Workervoid* operator new(std::size_t size, const std::nothrow_t&) noexcept;   // replaceable, nodiscard in C++2a
50*58b9f456SAndroid Build Coastguard Workervoid* operator new(std::size_t size, std::align_val_t alignment,
51*58b9f456SAndroid Build Coastguard Worker                   const std::nothrow_t&) noexcept;                     // replaceable, C++17, nodiscard in C++2a
52*58b9f456SAndroid Build Coastguard Workervoid  operator delete(void* ptr) noexcept;                              // replaceable
53*58b9f456SAndroid Build Coastguard Workervoid  operator delete(void* ptr, std::size_t size) noexcept;            // replaceable, C++14
54*58b9f456SAndroid Build Coastguard Workervoid  operator delete(void* ptr, std::align_val_t alignment) noexcept;  // replaceable, C++17
55*58b9f456SAndroid Build Coastguard Workervoid  operator delete(void* ptr, std::size_t size,
56*58b9f456SAndroid Build Coastguard Worker                      std::align_val_t alignment) noexcept;             // replaceable, C++17
57*58b9f456SAndroid Build Coastguard Workervoid  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
58*58b9f456SAndroid Build Coastguard Workervoid  operator delete(void* ptr, std:align_val_t alignment,
59*58b9f456SAndroid Build Coastguard Worker                      const std::nothrow_t&) noexcept;                  // replaceable, C++17
60*58b9f456SAndroid Build Coastguard Worker
61*58b9f456SAndroid Build Coastguard Workervoid* operator new[](std::size_t size);                                 // replaceable, nodiscard in C++2a
62*58b9f456SAndroid Build Coastguard Workervoid* operator new[](std::size_t size,
63*58b9f456SAndroid Build Coastguard Worker                     std::align_val_t alignment) noexcept;              // replaceable, C++17, nodiscard in C++2a
64*58b9f456SAndroid Build Coastguard Workervoid* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a
65*58b9f456SAndroid Build Coastguard Workervoid* operator new[](std::size_t size, std::align_val_t alignment,
66*58b9f456SAndroid Build Coastguard Worker                     const std::nothrow_t&) noexcept;                   // replaceable, C++17, nodiscard in C++2a
67*58b9f456SAndroid Build Coastguard Workervoid  operator delete[](void* ptr) noexcept;                            // replaceable
68*58b9f456SAndroid Build Coastguard Workervoid  operator delete[](void* ptr, std::size_t size) noexcept;          // replaceable, C++14
69*58b9f456SAndroid Build Coastguard Workervoid  operator delete[](void* ptr,
70*58b9f456SAndroid Build Coastguard Worker                        std::align_val_t alignment) noexcept;           // replaceable, C++17
71*58b9f456SAndroid Build Coastguard Workervoid  operator delete[](void* ptr, std::size_t size,
72*58b9f456SAndroid Build Coastguard Worker                        std::align_val_t alignment) noexcept;           // replaceable, C++17
73*58b9f456SAndroid Build Coastguard Workervoid  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable
74*58b9f456SAndroid Build Coastguard Workervoid  operator delete[](void* ptr, std::align_val_t alignment,
75*58b9f456SAndroid Build Coastguard Worker                        const std::nothrow_t&) noexcept;                // replaceable, C++17
76*58b9f456SAndroid Build Coastguard Worker
77*58b9f456SAndroid Build Coastguard Workervoid* operator new  (std::size_t size, void* ptr) noexcept;             // nodiscard in C++2a
78*58b9f456SAndroid Build Coastguard Workervoid* operator new[](std::size_t size, void* ptr) noexcept;             // nodiscard in C++2a
79*58b9f456SAndroid Build Coastguard Workervoid  operator delete  (void* ptr, void*) noexcept;
80*58b9f456SAndroid Build Coastguard Workervoid  operator delete[](void* ptr, void*) noexcept;
81*58b9f456SAndroid Build Coastguard Worker
82*58b9f456SAndroid Build Coastguard Worker*/
83*58b9f456SAndroid Build Coastguard Worker
84*58b9f456SAndroid Build Coastguard Worker#include <__config>
85*58b9f456SAndroid Build Coastguard Worker#include <exception>
86*58b9f456SAndroid Build Coastguard Worker#include <type_traits>
87*58b9f456SAndroid Build Coastguard Worker#include <cstddef>
88*58b9f456SAndroid Build Coastguard Worker#include <version>
89*58b9f456SAndroid Build Coastguard Worker#ifdef _LIBCPP_NO_EXCEPTIONS
90*58b9f456SAndroid Build Coastguard Worker#include <cstdlib>
91*58b9f456SAndroid Build Coastguard Worker#endif
92*58b9f456SAndroid Build Coastguard Worker
93*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME)
94*58b9f456SAndroid Build Coastguard Worker#include <new.h>
95*58b9f456SAndroid Build Coastguard Worker#endif
96*58b9f456SAndroid Build Coastguard Worker
97*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
98*58b9f456SAndroid Build Coastguard Worker#pragma GCC system_header
99*58b9f456SAndroid Build Coastguard Worker#endif
100*58b9f456SAndroid Build Coastguard Worker
101*58b9f456SAndroid Build Coastguard Worker#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation  < 201309L
102*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
103*58b9f456SAndroid Build Coastguard Worker#endif
104*58b9f456SAndroid Build Coastguard Worker
105*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
106*58b9f456SAndroid Build Coastguard Worker    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
107*58b9f456SAndroid Build Coastguard Worker# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
108*58b9f456SAndroid Build Coastguard Worker#endif
109*58b9f456SAndroid Build Coastguard Worker
110*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
111*58b9f456SAndroid Build Coastguard Worker    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
112*58b9f456SAndroid Build Coastguard Worker# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
113*58b9f456SAndroid Build Coastguard Worker#endif
114*58b9f456SAndroid Build Coastguard Worker
115*58b9f456SAndroid Build Coastguard Worker#if !__has_builtin(__builtin_operator_new) || \
116*58b9f456SAndroid Build Coastguard Worker   __has_builtin(__builtin_operator_new) < 201802L
117*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE
118*58b9f456SAndroid Build Coastguard Worker#endif
119*58b9f456SAndroid Build Coastguard Worker
120*58b9f456SAndroid Build Coastguard Workernamespace std  // purposefully not using versioning namespace
121*58b9f456SAndroid Build Coastguard Worker{
122*58b9f456SAndroid Build Coastguard Worker
123*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
124*58b9f456SAndroid Build Coastguard Workerstruct _LIBCPP_TYPE_VIS nothrow_t {};
125*58b9f456SAndroid Build Coastguard Workerextern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
126*58b9f456SAndroid Build Coastguard Worker
127*58b9f456SAndroid Build Coastguard Workerclass _LIBCPP_EXCEPTION_ABI bad_alloc
128*58b9f456SAndroid Build Coastguard Worker    : public exception
129*58b9f456SAndroid Build Coastguard Worker{
130*58b9f456SAndroid Build Coastguard Workerpublic:
131*58b9f456SAndroid Build Coastguard Worker    bad_alloc() _NOEXCEPT;
132*58b9f456SAndroid Build Coastguard Worker    virtual ~bad_alloc() _NOEXCEPT;
133*58b9f456SAndroid Build Coastguard Worker    virtual const char* what() const _NOEXCEPT;
134*58b9f456SAndroid Build Coastguard Worker};
135*58b9f456SAndroid Build Coastguard Worker
136*58b9f456SAndroid Build Coastguard Workerclass _LIBCPP_EXCEPTION_ABI bad_array_new_length
137*58b9f456SAndroid Build Coastguard Worker    : public bad_alloc
138*58b9f456SAndroid Build Coastguard Worker{
139*58b9f456SAndroid Build Coastguard Workerpublic:
140*58b9f456SAndroid Build Coastguard Worker    bad_array_new_length() _NOEXCEPT;
141*58b9f456SAndroid Build Coastguard Worker    virtual ~bad_array_new_length() _NOEXCEPT;
142*58b9f456SAndroid Build Coastguard Worker    virtual const char* what() const _NOEXCEPT;
143*58b9f456SAndroid Build Coastguard Worker};
144*58b9f456SAndroid Build Coastguard Worker
145*58b9f456SAndroid Build Coastguard Workertypedef void (*new_handler)();
146*58b9f456SAndroid Build Coastguard Worker_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
147*58b9f456SAndroid Build Coastguard Worker_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
148*58b9f456SAndroid Build Coastguard Worker
149*58b9f456SAndroid Build Coastguard Worker#endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
150*58b9f456SAndroid Build Coastguard Worker
151*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
152*58b9f456SAndroid Build Coastguard Worker
153*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
154*58b9f456SAndroid Build Coastguard Worker    !defined(_LIBCPP_DEFER_NEW_TO_VCRUNTIME)
155*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
156*58b9f456SAndroid Build Coastguard Workerenum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
157*58b9f456SAndroid Build Coastguard Worker#else
158*58b9f456SAndroid Build Coastguard Workerenum align_val_t { __zero = 0, __max = (size_t)-1 };
159*58b9f456SAndroid Build Coastguard Worker#endif
160*58b9f456SAndroid Build Coastguard Worker#endif
161*58b9f456SAndroid Build Coastguard Worker
162*58b9f456SAndroid Build Coastguard Worker}  // std
163*58b9f456SAndroid Build Coastguard Worker
164*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_CXX03_LANG)
165*58b9f456SAndroid Build Coastguard Worker#define _THROW_BAD_ALLOC throw(std::bad_alloc)
166*58b9f456SAndroid Build Coastguard Worker#else
167*58b9f456SAndroid Build Coastguard Worker#define _THROW_BAD_ALLOC
168*58b9f456SAndroid Build Coastguard Worker#endif
169*58b9f456SAndroid Build Coastguard Worker
170*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_DEFER_NEW_TO_VCRUNTIME)
171*58b9f456SAndroid Build Coastguard Worker
172*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
173*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
174*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
175*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
176*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
177*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
178*58b9f456SAndroid Build Coastguard Worker#endif
179*58b9f456SAndroid Build Coastguard Worker
180*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
181*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
182*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
183*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
184*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
185*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
186*58b9f456SAndroid Build Coastguard Worker#endif
187*58b9f456SAndroid Build Coastguard Worker
188*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
189*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
190*58b9f456SAndroid Build Coastguard Worker_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;
191*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t) _NOEXCEPT;
192*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
193*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
194*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
195*58b9f456SAndroid Build Coastguard Worker#endif
196*58b9f456SAndroid Build Coastguard Worker
197*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
198*58b9f456SAndroid Build Coastguard Worker_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;
199*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
200*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
201*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
202*58b9f456SAndroid Build Coastguard Worker_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
203*58b9f456SAndroid Build Coastguard Worker#endif
204*58b9f456SAndroid Build Coastguard Worker#endif
205*58b9f456SAndroid Build Coastguard Worker
206*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
207*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
208*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
209*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
210*58b9f456SAndroid Build Coastguard Worker
211*58b9f456SAndroid Build Coastguard Worker#endif // !_LIBCPP_DEFER_NEW_TO_VCRUNTIME
212*58b9f456SAndroid Build Coastguard Worker
213*58b9f456SAndroid Build Coastguard Worker_LIBCPP_BEGIN_NAMESPACE_STD
214*58b9f456SAndroid Build Coastguard Worker
215*58b9f456SAndroid Build Coastguard Worker_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
216*58b9f456SAndroid Build Coastguard Worker#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
217*58b9f456SAndroid Build Coastguard Worker  return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
218*58b9f456SAndroid Build Coastguard Worker#else
219*58b9f456SAndroid Build Coastguard Worker  return __align > alignment_of<max_align_t>::value;
220*58b9f456SAndroid Build Coastguard Worker#endif
221*58b9f456SAndroid Build Coastguard Worker}
222*58b9f456SAndroid Build Coastguard Worker
223*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) {
224*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
225*58b9f456SAndroid Build Coastguard Worker  if (__is_overaligned_for_new(__align)) {
226*58b9f456SAndroid Build Coastguard Worker    const align_val_t __align_val = static_cast<align_val_t>(__align);
227*58b9f456SAndroid Build Coastguard Worker# ifdef _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE
228*58b9f456SAndroid Build Coastguard Worker    return ::operator new(__size, __align_val);
229*58b9f456SAndroid Build Coastguard Worker# else
230*58b9f456SAndroid Build Coastguard Worker    return __builtin_operator_new(__size, __align_val);
231*58b9f456SAndroid Build Coastguard Worker# endif
232*58b9f456SAndroid Build Coastguard Worker  }
233*58b9f456SAndroid Build Coastguard Worker#else
234*58b9f456SAndroid Build Coastguard Worker  ((void)__align);
235*58b9f456SAndroid Build Coastguard Worker#endif
236*58b9f456SAndroid Build Coastguard Worker#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
237*58b9f456SAndroid Build Coastguard Worker  return ::operator new(__size);
238*58b9f456SAndroid Build Coastguard Worker#else
239*58b9f456SAndroid Build Coastguard Worker  return __builtin_operator_new(__size);
240*58b9f456SAndroid Build Coastguard Worker#endif
241*58b9f456SAndroid Build Coastguard Worker}
242*58b9f456SAndroid Build Coastguard Worker
243*58b9f456SAndroid Build Coastguard Workerstruct _DeallocateCaller {
244*58b9f456SAndroid Build Coastguard Worker  static inline _LIBCPP_INLINE_VISIBILITY
245*58b9f456SAndroid Build Coastguard Worker  void __do_deallocate_handle_size_align(void *__ptr, size_t __size, size_t __align) {
246*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
247*58b9f456SAndroid Build Coastguard Worker    ((void)__align);
248*58b9f456SAndroid Build Coastguard Worker    return __do_deallocate_handle_size(__ptr, __size);
249*58b9f456SAndroid Build Coastguard Worker#else
250*58b9f456SAndroid Build Coastguard Worker    if (__is_overaligned_for_new(__align)) {
251*58b9f456SAndroid Build Coastguard Worker      const align_val_t __align_val = static_cast<align_val_t>(__align);
252*58b9f456SAndroid Build Coastguard Worker      return __do_deallocate_handle_size(__ptr, __size, __align_val);
253*58b9f456SAndroid Build Coastguard Worker    } else {
254*58b9f456SAndroid Build Coastguard Worker      return __do_deallocate_handle_size(__ptr, __size);
255*58b9f456SAndroid Build Coastguard Worker    }
256*58b9f456SAndroid Build Coastguard Worker#endif
257*58b9f456SAndroid Build Coastguard Worker  }
258*58b9f456SAndroid Build Coastguard Worker
259*58b9f456SAndroid Build Coastguard Worker  static inline _LIBCPP_INLINE_VISIBILITY
260*58b9f456SAndroid Build Coastguard Worker  void __do_deallocate_handle_align(void *__ptr, size_t __align) {
261*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
262*58b9f456SAndroid Build Coastguard Worker    ((void)__align);
263*58b9f456SAndroid Build Coastguard Worker    return __do_call(__ptr);
264*58b9f456SAndroid Build Coastguard Worker#else
265*58b9f456SAndroid Build Coastguard Worker    if (__is_overaligned_for_new(__align)) {
266*58b9f456SAndroid Build Coastguard Worker      const align_val_t __align_val = static_cast<align_val_t>(__align);
267*58b9f456SAndroid Build Coastguard Worker      return __do_call(__ptr, __align_val);
268*58b9f456SAndroid Build Coastguard Worker    } else {
269*58b9f456SAndroid Build Coastguard Worker      return __do_call(__ptr);
270*58b9f456SAndroid Build Coastguard Worker    }
271*58b9f456SAndroid Build Coastguard Worker#endif
272*58b9f456SAndroid Build Coastguard Worker  }
273*58b9f456SAndroid Build Coastguard Worker
274*58b9f456SAndroid Build Coastguard Worker private:
275*58b9f456SAndroid Build Coastguard Worker  static inline void __do_deallocate_handle_size(void *__ptr, size_t __size) {
276*58b9f456SAndroid Build Coastguard Worker#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
277*58b9f456SAndroid Build Coastguard Worker    ((void)__size);
278*58b9f456SAndroid Build Coastguard Worker    return __do_call(__ptr);
279*58b9f456SAndroid Build Coastguard Worker#else
280*58b9f456SAndroid Build Coastguard Worker    return __do_call(__ptr, __size);
281*58b9f456SAndroid Build Coastguard Worker#endif
282*58b9f456SAndroid Build Coastguard Worker  }
283*58b9f456SAndroid Build Coastguard Worker
284*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
285*58b9f456SAndroid Build Coastguard Worker  static inline void __do_deallocate_handle_size(void *__ptr, size_t __size, align_val_t __align) {
286*58b9f456SAndroid Build Coastguard Worker#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
287*58b9f456SAndroid Build Coastguard Worker    ((void)__size);
288*58b9f456SAndroid Build Coastguard Worker    return __do_call(__ptr, __align);
289*58b9f456SAndroid Build Coastguard Worker#else
290*58b9f456SAndroid Build Coastguard Worker    return __do_call(__ptr, __size, __align);
291*58b9f456SAndroid Build Coastguard Worker#endif
292*58b9f456SAndroid Build Coastguard Worker  }
293*58b9f456SAndroid Build Coastguard Worker#endif
294*58b9f456SAndroid Build Coastguard Worker
295*58b9f456SAndroid Build Coastguard Workerprivate:
296*58b9f456SAndroid Build Coastguard Worker  template <class _A1, class _A2>
297*58b9f456SAndroid Build Coastguard Worker  static inline void __do_call(void *__ptr, _A1 __a1, _A2 __a2) {
298*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \
299*58b9f456SAndroid Build Coastguard Worker    defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)
300*58b9f456SAndroid Build Coastguard Worker    return ::operator delete(__ptr, __a1, __a2);
301*58b9f456SAndroid Build Coastguard Worker#else
302*58b9f456SAndroid Build Coastguard Worker    return __builtin_operator_delete(__ptr, __a1, __a2);
303*58b9f456SAndroid Build Coastguard Worker#endif
304*58b9f456SAndroid Build Coastguard Worker  }
305*58b9f456SAndroid Build Coastguard Worker
306*58b9f456SAndroid Build Coastguard Worker  template <class _A1>
307*58b9f456SAndroid Build Coastguard Worker  static inline void __do_call(void *__ptr, _A1 __a1) {
308*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \
309*58b9f456SAndroid Build Coastguard Worker    defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)
310*58b9f456SAndroid Build Coastguard Worker    return ::operator delete(__ptr, __a1);
311*58b9f456SAndroid Build Coastguard Worker#else
312*58b9f456SAndroid Build Coastguard Worker    return __builtin_operator_delete(__ptr, __a1);
313*58b9f456SAndroid Build Coastguard Worker#endif
314*58b9f456SAndroid Build Coastguard Worker  }
315*58b9f456SAndroid Build Coastguard Worker
316*58b9f456SAndroid Build Coastguard Worker  static inline void __do_call(void *__ptr) {
317*58b9f456SAndroid Build Coastguard Worker#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
318*58b9f456SAndroid Build Coastguard Worker    return ::operator delete(__ptr);
319*58b9f456SAndroid Build Coastguard Worker#else
320*58b9f456SAndroid Build Coastguard Worker    return __builtin_operator_delete(__ptr);
321*58b9f456SAndroid Build Coastguard Worker#endif
322*58b9f456SAndroid Build Coastguard Worker  }
323*58b9f456SAndroid Build Coastguard Worker};
324*58b9f456SAndroid Build Coastguard Worker
325*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
326*58b9f456SAndroid Build Coastguard Worker  _DeallocateCaller::__do_deallocate_handle_size_align(__ptr, __size, __align);
327*58b9f456SAndroid Build Coastguard Worker}
328*58b9f456SAndroid Build Coastguard Worker
329*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
330*58b9f456SAndroid Build Coastguard Worker  _DeallocateCaller::__do_deallocate_handle_align(__ptr, __align);
331*58b9f456SAndroid Build Coastguard Worker}
332*58b9f456SAndroid Build Coastguard Worker
333*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp>
334*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 inline
335*58b9f456SAndroid Build Coastguard Worker_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
336*58b9f456SAndroid Build Coastguard Worker{
337*58b9f456SAndroid Build Coastguard Worker    static_assert (!(is_function<_Tp>::value), "can't launder functions" );
338*58b9f456SAndroid Build Coastguard Worker    static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
339*58b9f456SAndroid Build Coastguard Worker#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
340*58b9f456SAndroid Build Coastguard Worker    return __builtin_launder(__p);
341*58b9f456SAndroid Build Coastguard Worker#else
342*58b9f456SAndroid Build Coastguard Worker    return __p;
343*58b9f456SAndroid Build Coastguard Worker#endif
344*58b9f456SAndroid Build Coastguard Worker}
345*58b9f456SAndroid Build Coastguard Worker
346*58b9f456SAndroid Build Coastguard Worker
347*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 14
348*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp>
349*58b9f456SAndroid Build Coastguard Worker_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
350*58b9f456SAndroid Build Coastguard Workerconstexpr _Tp* launder(_Tp* __p) noexcept
351*58b9f456SAndroid Build Coastguard Worker{
352*58b9f456SAndroid Build Coastguard Worker    return _VSTD::__launder(__p);
353*58b9f456SAndroid Build Coastguard Worker}
354*58b9f456SAndroid Build Coastguard Worker#endif
355*58b9f456SAndroid Build Coastguard Worker
356*58b9f456SAndroid Build Coastguard Worker_LIBCPP_END_NAMESPACE_STD
357*58b9f456SAndroid Build Coastguard Worker
358*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_NEW
359