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