• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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___MEMORY_ALLOCATOR_H
11 #define _LIBCPP___MEMORY_ALLOCATOR_H
12 
13 #include <__config>
14 #include <__memory/addressof.h>
15 #include <__memory/allocate_at_least.h>
16 #include <__memory/allocator_traits.h>
17 #include <__type_traits/is_constant_evaluated.h>
18 #include <__type_traits/is_same.h>
19 #include <__type_traits/is_void.h>
20 #include <__type_traits/is_volatile.h>
21 #include <__utility/forward.h>
22 #include <cstddef>
23 #include <new>
24 #include <stdexcept>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 _LIBCPP_BEGIN_NAMESPACE_STD
31 
32 template <class _Tp> class allocator;
33 
34 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_VOID_SPECIALIZATION)
35 // These specializations shouldn't be marked _LIBCPP_DEPRECATED_IN_CXX17.
36 // Specializing allocator<void> is deprecated, but not using it.
37 template <>
38 class _LIBCPP_TEMPLATE_VIS allocator<void>
39 {
40 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
41 public:
42     _LIBCPP_DEPRECATED_IN_CXX17 typedef void*             pointer;
43     _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       const_pointer;
44     _LIBCPP_DEPRECATED_IN_CXX17 typedef void              value_type;
45 
46     template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
47 #endif
48 };
49 
50 template <>
51 class _LIBCPP_TEMPLATE_VIS allocator<const void>
52 {
53 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
54 public:
55     _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       pointer;
56     _LIBCPP_DEPRECATED_IN_CXX17 typedef const void*       const_pointer;
57     _LIBCPP_DEPRECATED_IN_CXX17 typedef const void        value_type;
58 
59     template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
60 #endif
61 };
62 #endif
63 
64 // This class provides a non-trivial default constructor to the class that derives from it
65 // if the condition is satisfied.
66 //
67 // The second template parameter exists to allow giving a unique type to __non_trivial_if,
68 // which makes it possible to avoid breaking the ABI when making this a base class of an
69 // existing class. Without that, imagine we have classes D1 and D2, both of which used to
70 // have no base classes, but which now derive from __non_trivial_if. The layout of a class
71 // that inherits from both D1 and D2 will change because the two __non_trivial_if base
72 // classes are not allowed to share the same address.
73 //
74 // By making those __non_trivial_if base classes unique, we work around this problem and
75 // it is safe to start deriving from __non_trivial_if in existing classes.
76 template <bool _Cond, class _Unique>
77 struct __non_trivial_if { };
78 
79 template <class _Unique>
80 struct __non_trivial_if<true, _Unique> {
81     _LIBCPP_INLINE_VISIBILITY
82     _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT { }
83 };
84 
85 // allocator
86 //
87 // Note: For ABI compatibility between C++20 and previous standards, we make
88 //       allocator<void> trivial in C++20.
89 
90 template <class _Tp>
91 class _LIBCPP_TEMPLATE_VIS allocator
92     : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> >
93 {
94     static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
95 public:
96     typedef size_t      size_type;
97     typedef ptrdiff_t   difference_type;
98     typedef _Tp         value_type;
99     typedef true_type   propagate_on_container_move_assignment;
100     typedef true_type   is_always_equal;
101 
102     _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
103 
104     template <class _Up>
105     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
106     allocator(const allocator<_Up>&) _NOEXCEPT { }
107 
108     _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
109     _Tp* allocate(size_t __n) {
110         if (__n > allocator_traits<allocator>::max_size(*this))
111             __throw_bad_array_new_length();
112         if (__libcpp_is_constant_evaluated()) {
113             return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
114         } else {
115             return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
116         }
117     }
118 
119 #if _LIBCPP_STD_VER >= 23
120     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr
121     allocation_result<_Tp*> allocate_at_least(size_t __n) {
122         return {allocate(__n), __n};
123     }
124 #endif
125 
126     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
127     void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
128         if (__libcpp_is_constant_evaluated()) {
129             ::operator delete(__p);
130         } else {
131             _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
132         }
133     }
134 
135     // C++20 Removed members
136 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
137     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp*       pointer;
138     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
139     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp&       reference;
140     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
141 
142     template <class _Up>
143     struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
144         typedef allocator<_Up> other;
145     };
146 
147     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
148     pointer address(reference __x) const _NOEXCEPT {
149         return _VSTD::addressof(__x);
150     }
151     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
152     const_pointer address(const_reference __x) const _NOEXCEPT {
153         return _VSTD::addressof(__x);
154     }
155 
156     _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
157     _Tp* allocate(size_t __n, const void*) {
158         return allocate(__n);
159     }
160 
161     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
162         return size_type(~0) / sizeof(_Tp);
163     }
164 
165     template <class _Up, class... _Args>
166     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
167     void construct(_Up* __p, _Args&&... __args) {
168         ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
169     }
170 
171     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
172     void destroy(pointer __p) {
173         __p->~_Tp();
174     }
175 #endif
176 };
177 
178 template <class _Tp>
179 class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
180     : private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> >
181 {
182     static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
183 public:
184     typedef size_t      size_type;
185     typedef ptrdiff_t   difference_type;
186     typedef const _Tp   value_type;
187     typedef true_type   propagate_on_container_move_assignment;
188     typedef true_type   is_always_equal;
189 
190     _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
191 
192     template <class _Up>
193     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
194     allocator(const allocator<_Up>&) _NOEXCEPT { }
195 
196     _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
197     const _Tp* allocate(size_t __n) {
198         if (__n > allocator_traits<allocator>::max_size(*this))
199             __throw_bad_array_new_length();
200         if (__libcpp_is_constant_evaluated()) {
201             return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
202         } else {
203             return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
204         }
205     }
206 
207 #if _LIBCPP_STD_VER >= 23
208     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr
209     allocation_result<const _Tp*> allocate_at_least(size_t __n) {
210         return {allocate(__n), __n};
211     }
212 #endif
213 
214     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
215     void deallocate(const _Tp* __p, size_t __n) {
216         if (__libcpp_is_constant_evaluated()) {
217             ::operator delete(const_cast<_Tp*>(__p));
218         } else {
219             _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
220         }
221     }
222 
223     // C++20 Removed members
224 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
225     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
226     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
227     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
228     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
229 
230     template <class _Up>
231     struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
232         typedef allocator<_Up> other;
233     };
234 
235     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
236     const_pointer address(const_reference __x) const _NOEXCEPT {
237         return _VSTD::addressof(__x);
238     }
239 
240     _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
241     const _Tp* allocate(size_t __n, const void*) {
242         return allocate(__n);
243     }
244 
245     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
246         return size_type(~0) / sizeof(_Tp);
247     }
248 
249     template <class _Up, class... _Args>
250     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
251     void construct(_Up* __p, _Args&&... __args) {
252         ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
253     }
254 
255     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
256     void destroy(pointer __p) {
257         __p->~_Tp();
258     }
259 #endif
260 };
261 
262 template <class _Tp, class _Up>
263 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
264 bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
265 
266 template <class _Tp, class _Up>
267 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
268 bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
269 
270 _LIBCPP_END_NAMESPACE_STD
271 
272 #endif // _LIBCPP___MEMORY_ALLOCATOR_H
273