2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___MEMORY_ALLOCATOR_H
11 #define _LIBCPP___MEMORY_ALLOCATOR_H
14 #include <__memory/allocator_traits.h>
15 #include <__utility/forward.h>
19 #include <type_traits>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #pragma GCC system_header
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 template <class _Tp
> class allocator
;
29 #if _LIBCPP_STD_VER <= 17
31 class _LIBCPP_TEMPLATE_VIS allocator
<void>
34 _LIBCPP_DEPRECATED_IN_CXX17
typedef void* pointer
;
35 _LIBCPP_DEPRECATED_IN_CXX17
typedef const void* const_pointer
;
36 _LIBCPP_DEPRECATED_IN_CXX17
typedef void value_type
;
38 template <class _Up
> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind
{typedef allocator
<_Up
> other
;};
42 class _LIBCPP_TEMPLATE_VIS allocator
<const void>
45 _LIBCPP_DEPRECATED_IN_CXX17
typedef const void* pointer
;
46 _LIBCPP_DEPRECATED_IN_CXX17
typedef const void* const_pointer
;
47 _LIBCPP_DEPRECATED_IN_CXX17
typedef const void value_type
;
49 template <class _Up
> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind
{typedef allocator
<_Up
> other
;};
53 // This class provides a non-trivial default constructor to the class that derives from it
54 // if the condition is satisfied.
56 // The second template parameter exists to allow giving a unique type to __non_trivial_if,
57 // which makes it possible to avoid breaking the ABI when making this a base class of an
58 // existing class. Without that, imagine we have classes D1 and D2, both of which used to
59 // have no base classes, but which now derive from __non_trivial_if. The layout of a class
60 // that inherits from both D1 and D2 will change because the two __non_trivial_if base
61 // classes are not allowed to share the same address.
63 // By making those __non_trivial_if base classes unique, we work around this problem and
64 // it is safe to start deriving from __non_trivial_if in existing classes.
65 template <bool _Cond
, class _Unique
>
66 struct __non_trivial_if
{ };
68 template <class _Unique
>
69 struct __non_trivial_if
<true, _Unique
> {
70 _LIBCPP_INLINE_VISIBILITY
71 _LIBCPP_CONSTEXPR
__non_trivial_if() _NOEXCEPT
{ }
76 // Note: For ABI compatibility between C++20 and previous standards, we make
77 // allocator<void> trivial in C++20.
80 class _LIBCPP_TEMPLATE_VIS allocator
81 : private __non_trivial_if
<!is_void
<_Tp
>::value
, allocator
<_Tp
> >
83 static_assert(!is_volatile
<_Tp
>::value
, "std::allocator does not support volatile types");
85 typedef size_t size_type
;
86 typedef ptrdiff_t difference_type
;
87 typedef _Tp value_type
;
88 typedef true_type propagate_on_container_move_assignment
;
89 typedef true_type is_always_equal
;
91 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
92 allocator() _NOEXCEPT
= default;
95 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
96 allocator(const allocator
<_Up
>&) _NOEXCEPT
{ }
98 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
99 _Tp
* allocate(size_t __n
) {
100 if (__n
> allocator_traits
<allocator
>::max_size(*this))
101 __throw_bad_array_new_length();
102 if (__libcpp_is_constant_evaluated()) {
103 return static_cast<_Tp
*>(::operator new(__n
* sizeof(_Tp
)));
105 return static_cast<_Tp
*>(_VSTD::__libcpp_allocate(__n
* sizeof(_Tp
), _LIBCPP_ALIGNOF(_Tp
)));
109 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
110 void deallocate(_Tp
* __p
, size_t __n
) _NOEXCEPT
{
111 if (__libcpp_is_constant_evaluated()) {
112 ::operator delete(__p
);
114 _VSTD::__libcpp_deallocate((void*)__p
, __n
* sizeof(_Tp
), _LIBCPP_ALIGNOF(_Tp
));
118 // C++20 Removed members
119 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
120 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp
* pointer
;
121 _LIBCPP_DEPRECATED_IN_CXX17
typedef const _Tp
* const_pointer
;
122 _LIBCPP_DEPRECATED_IN_CXX17
typedef _Tp
& reference
;
123 _LIBCPP_DEPRECATED_IN_CXX17
typedef const _Tp
& const_reference
;
126 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind
{
127 typedef allocator
<_Up
> other
;
130 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
131 pointer
address(reference __x
) const _NOEXCEPT
{
132 return _VSTD::addressof(__x
);
134 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
135 const_pointer
address(const_reference __x
) const _NOEXCEPT
{
136 return _VSTD::addressof(__x
);
139 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
140 _Tp
* allocate(size_t __n
, const void*) {
141 return allocate(__n
);
144 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type
max_size() const _NOEXCEPT
{
145 return size_type(~0) / sizeof(_Tp
);
148 template <class _Up
, class... _Args
>
149 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
150 void construct(_Up
* __p
, _Args
&&... __args
) {
151 ::new ((void*)__p
) _Up(_VSTD::forward
<_Args
>(__args
)...);
154 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
155 void destroy(pointer __p
) {
162 class _LIBCPP_TEMPLATE_VIS allocator
<const _Tp
>
163 : private __non_trivial_if
<!is_void
<_Tp
>::value
, allocator
<const _Tp
> >
165 static_assert(!is_volatile
<_Tp
>::value
, "std::allocator does not support volatile types");
167 typedef size_t size_type
;
168 typedef ptrdiff_t difference_type
;
169 typedef const _Tp value_type
;
170 typedef true_type propagate_on_container_move_assignment
;
171 typedef true_type is_always_equal
;
173 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
174 allocator() _NOEXCEPT
= default;
177 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
178 allocator(const allocator
<_Up
>&) _NOEXCEPT
{ }
180 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
181 const _Tp
* allocate(size_t __n
) {
182 if (__n
> allocator_traits
<allocator
>::max_size(*this))
183 __throw_bad_array_new_length();
184 if (__libcpp_is_constant_evaluated()) {
185 return static_cast<const _Tp
*>(::operator new(__n
* sizeof(_Tp
)));
187 return static_cast<const _Tp
*>(_VSTD::__libcpp_allocate(__n
* sizeof(_Tp
), _LIBCPP_ALIGNOF(_Tp
)));
191 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
192 void deallocate(const _Tp
* __p
, size_t __n
) {
193 if (__libcpp_is_constant_evaluated()) {
194 ::operator delete(const_cast<_Tp
*>(__p
));
196 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp
*>(__p
), __n
* sizeof(_Tp
), _LIBCPP_ALIGNOF(_Tp
));
200 // C++20 Removed members
201 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
202 _LIBCPP_DEPRECATED_IN_CXX17
typedef const _Tp
* pointer
;
203 _LIBCPP_DEPRECATED_IN_CXX17
typedef const _Tp
* const_pointer
;
204 _LIBCPP_DEPRECATED_IN_CXX17
typedef const _Tp
& reference
;
205 _LIBCPP_DEPRECATED_IN_CXX17
typedef const _Tp
& const_reference
;
208 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind
{
209 typedef allocator
<_Up
> other
;
212 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
213 const_pointer
address(const_reference __x
) const _NOEXCEPT
{
214 return _VSTD::addressof(__x
);
217 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
218 const _Tp
* allocate(size_t __n
, const void*) {
219 return allocate(__n
);
222 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type
max_size() const _NOEXCEPT
{
223 return size_type(~0) / sizeof(_Tp
);
226 template <class _Up
, class... _Args
>
227 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
228 void construct(_Up
* __p
, _Args
&&... __args
) {
229 ::new ((void*)__p
) _Up(_VSTD::forward
<_Args
>(__args
)...);
232 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
233 void destroy(pointer __p
) {
239 template <class _Tp
, class _Up
>
240 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
241 bool operator==(const allocator
<_Tp
>&, const allocator
<_Up
>&) _NOEXCEPT
{return true;}
243 template <class _Tp
, class _Up
>
244 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
245 bool operator!=(const allocator
<_Tp
>&, const allocator
<_Up
>&) _NOEXCEPT
{return false;}
247 _LIBCPP_END_NAMESPACE_STD
249 #endif // _LIBCPP___MEMORY_ALLOCATOR_H