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