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_TRAITS_H
11#define _LIBCPP___MEMORY_ALLOCATOR_TRAITS_H
12
13#include <__config>
14#include <__cstddef/ptrdiff_t.h>
15#include <__cstddef/size_t.h>
16#include <__fwd/memory.h>
17#include <__memory/construct_at.h>
18#include <__memory/pointer_traits.h>
19#include <__type_traits/detected_or.h>
20#include <__type_traits/enable_if.h>
21#include <__type_traits/is_constructible.h>
22#include <__type_traits/is_empty.h>
23#include <__type_traits/is_same.h>
24#include <__type_traits/make_unsigned.h>
25#include <__type_traits/remove_reference.h>
26#include <__type_traits/void_t.h>
27#include <__utility/declval.h>
28#include <__utility/forward.h>
29#include <limits>
30
31#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32# pragma GCC system_header
33#endif
34
35_LIBCPP_PUSH_MACROS
36#include <__undef_macros>
37
38_LIBCPP_BEGIN_NAMESPACE_STD
39
40_LIBCPP_SUPPRESS_DEPRECATED_PUSH
41// __pointer
42template <class _Tp>
43using __pointer_member _LIBCPP_NODEBUG = typename _Tp::pointer;
44
45template <class _Tp, class _Alloc>
46using __pointer _LIBCPP_NODEBUG = __detected_or_t<_Tp*, __pointer_member, __libcpp_remove_reference_t<_Alloc> >;
47
48// This trait returns _Alias<_Alloc> if that's well-formed, and _Ptr rebound to _Tp otherwise
49template <class _Alloc, template <class> class _Alias, class _Ptr, class _Tp, class = void>
50struct __rebind_or_alias_pointer {
51#ifdef _LIBCPP_CXX03_LANG
52 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<_Tp>::other;
53#else
54 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<_Tp>;
55#endif
56};
57
58template <class _Ptr, class _Alloc, class _Tp, template <class> class _Alias>
59struct __rebind_or_alias_pointer<_Alloc, _Alias, _Ptr, _Tp, __void_t<_Alias<_Alloc> > > {
60 using type _LIBCPP_NODEBUG = _Alias<_Alloc>;
61};
62
63// __const_pointer
64template <class _Alloc>
65using __const_pointer_member _LIBCPP_NODEBUG = typename _Alloc::const_pointer;
66
67template <class _Tp, class _Ptr, class _Alloc>
68using __const_pointer_t _LIBCPP_NODEBUG =
69 typename __rebind_or_alias_pointer<_Alloc, __const_pointer_member, _Ptr, const _Tp>::type;
70_LIBCPP_SUPPRESS_DEPRECATED_POP
71
72// __void_pointer
73template <class _Alloc>
74using __void_pointer_member _LIBCPP_NODEBUG = typename _Alloc::void_pointer;
75
76template <class _Ptr, class _Alloc>
77using __void_pointer_t _LIBCPP_NODEBUG =
78 typename __rebind_or_alias_pointer<_Alloc, __void_pointer_member, _Ptr, void>::type;
79
80// __const_void_pointer
81template <class _Alloc>
82using __const_void_pointer_member _LIBCPP_NODEBUG = typename _Alloc::const_void_pointer;
83
84template <class _Ptr, class _Alloc>
85using __const_void_pointer_t _LIBCPP_NODEBUG =
86 typename __rebind_or_alias_pointer<_Alloc, __const_void_pointer_member, _Ptr, const void>::type;
87
88// __size_type
89template <class _Tp>
90using __size_type_member _LIBCPP_NODEBUG = typename _Tp::size_type;
91
92template <class _Alloc, class _DiffType>
93using __size_type _LIBCPP_NODEBUG = __detected_or_t<__make_unsigned_t<_DiffType>, __size_type_member, _Alloc>;
94
95// __alloc_traits_difference_type
96template <class _Alloc, class _Ptr, class = void>
97struct __alloc_traits_difference_type {
98 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::difference_type;
99};
100
101template <class _Alloc, class _Ptr>
102struct __alloc_traits_difference_type<_Alloc, _Ptr, __void_t<typename _Alloc::difference_type> > {
103 using type _LIBCPP_NODEBUG = typename _Alloc::difference_type;
104};
105
106// __propagate_on_container_copy_assignment
107template <class _Tp>
108using __propagate_on_container_copy_assignment_member _LIBCPP_NODEBUG =
109 typename _Tp::propagate_on_container_copy_assignment;
110
111template <class _Alloc>
112using __propagate_on_container_copy_assignment _LIBCPP_NODEBUG =
113 __detected_or_t<false_type, __propagate_on_container_copy_assignment_member, _Alloc>;
114
115// __propagate_on_container_move_assignment
116template <class _Tp>
117using __propagate_on_container_move_assignment_member _LIBCPP_NODEBUG =
118 typename _Tp::propagate_on_container_move_assignment;
119
120template <class _Alloc>
121using __propagate_on_container_move_assignment _LIBCPP_NODEBUG =
122 __detected_or_t<false_type, __propagate_on_container_move_assignment_member, _Alloc>;
123
124// __propagate_on_container_swap
125template <class _Tp>
126using __propagate_on_container_swap_member _LIBCPP_NODEBUG = typename _Tp::propagate_on_container_swap;
127
128template <class _Alloc>
129using __propagate_on_container_swap _LIBCPP_NODEBUG =
130 __detected_or_t<false_type, __propagate_on_container_swap_member, _Alloc>;
131
132_LIBCPP_SUPPRESS_DEPRECATED_PUSH
133// __is_always_equal
134template <class _Tp>
135using __is_always_equal_member _LIBCPP_NODEBUG = typename _Tp::is_always_equal;
136
137template <class _Alloc>
138using __is_always_equal _LIBCPP_NODEBUG =
139 __detected_or_t<typename is_empty<_Alloc>::type, __is_always_equal_member, _Alloc>;
140
141// __allocator_traits_rebind
142template <class _Tp, class _Up, class = void>
143inline const bool __has_rebind_other_v = false;
144template <class _Tp, class _Up>
145inline const bool __has_rebind_other_v<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>::other> > = true;
146
147template <class _Tp, class _Up, bool = __has_rebind_other_v<_Tp, _Up> >
148struct __allocator_traits_rebind {
149 static_assert(__has_rebind_other_v<_Tp, _Up>, "This allocator has to implement rebind");
150 using type _LIBCPP_NODEBUG = typename _Tp::template rebind<_Up>::other;
151};
152template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up>
153struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true> {
154 using type _LIBCPP_NODEBUG = typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other;
155};
156template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up>
157struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false> {
158 using type _LIBCPP_NODEBUG = _Alloc<_Up, _Args...>;
159};
160_LIBCPP_SUPPRESS_DEPRECATED_POP
161
162template <class _Alloc, class _Tp>
163using __allocator_traits_rebind_t _LIBCPP_NODEBUG = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
164
165_LIBCPP_SUPPRESS_DEPRECATED_PUSH
166
167// __has_allocate_hint_v
168template <class _Alloc, class _SizeType, class _ConstVoidPtr, class = void>
169inline const bool __has_allocate_hint_v = false;
170
171template <class _Alloc, class _SizeType, class _ConstVoidPtr>
172inline const bool __has_allocate_hint_v<
173 _Alloc,
174 _SizeType,
175 _ConstVoidPtr,
176 decltype((void)std::declval<_Alloc>().allocate(std::declval<_SizeType>(), std::declval<_ConstVoidPtr>()))> = true;
177
178// __has_construct_v
179template <class, class _Alloc, class... _Args>
180inline const bool __has_construct_impl = false;
181
182template <class _Alloc, class... _Args>
183inline const bool
184 __has_construct_impl<decltype((void)std::declval<_Alloc>().construct(std::declval<_Args>()...)), _Alloc, _Args...> =
185 true;
186
187template <class _Alloc, class... _Args>
188inline const bool __has_construct_v = __has_construct_impl<void, _Alloc, _Args...>;
189
190// __has_destroy_v
191template <class _Alloc, class _Pointer, class = void>
192inline const bool __has_destroy_v = false;
193
194template <class _Alloc, class _Pointer>
195inline const bool
196 __has_destroy_v<_Alloc, _Pointer, decltype((void)std::declval<_Alloc>().destroy(std::declval<_Pointer>()))> = true;
197
198// __has_max_size_v
199template <class _Alloc, class = void>
200inline const bool __has_max_size_v = false;
201
202template <class _Alloc>
203inline const bool __has_max_size_v<_Alloc, decltype((void)std::declval<_Alloc&>().max_size())> = true;
204
205// __has_select_on_container_copy_construction_v
206template <class _Alloc, class = void>
207inline const bool __has_select_on_container_copy_construction_v = false;
208
209template <class _Alloc>
210inline const bool __has_select_on_container_copy_construction_v<
211 _Alloc,
212 decltype((void)std::declval<_Alloc>().select_on_container_copy_construction())> = true;
213
214_LIBCPP_SUPPRESS_DEPRECATED_POP
215
216#if _LIBCPP_STD_VER >= 23
217
218template <class _Pointer, class _SizeType = size_t>
219struct allocation_result {
220 _Pointer ptr;
221 _SizeType count;
222};
223_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(allocation_result);
224
225#endif // _LIBCPP_STD_VER
226
227template <class>
228struct allocator_traits;
229
230// We have a base class that can be specialized for different allocators, since the metaprogramming to get the aliases
231// is quite expensive and the definition of these aliases is usually quite trivial in the end.
232template <class _Alloc>
233struct __allocator_traits_base {
234 using allocator_type = _Alloc;
235 using value_type = typename allocator_type::value_type;
236 using pointer = __pointer<value_type, allocator_type>;
237 using const_pointer = __const_pointer_t<value_type, pointer, allocator_type>;
238 using void_pointer = __void_pointer_t<pointer, allocator_type>;
239 using const_void_pointer = __const_void_pointer_t<pointer, allocator_type>;
240 using difference_type = typename __alloc_traits_difference_type<allocator_type, pointer>::type;
241 using size_type = __size_type<allocator_type, difference_type>;
242 using propagate_on_container_copy_assignment = __propagate_on_container_copy_assignment<allocator_type>;
243 using propagate_on_container_move_assignment = __propagate_on_container_move_assignment<allocator_type>;
244 using propagate_on_container_swap = __propagate_on_container_swap<allocator_type>;
245 using is_always_equal = __is_always_equal<allocator_type>;
246
247#ifndef _LIBCPP_CXX03_LANG
248 template <class _Tp>
249 using rebind_alloc = __allocator_traits_rebind_t<allocator_type, _Tp>;
250 template <class _Tp>
251 using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
252#else // _LIBCPP_CXX03_LANG
253 template <class _Tp>
254 struct rebind_alloc {
255 using other = __allocator_traits_rebind_t<allocator_type, _Tp>;
256 };
257 template <class _Tp>
258 struct rebind_traits {
259 using other = allocator_traits<typename rebind_alloc<_Tp>::other>;
260 };
261#endif // _LIBCPP_CXX03_LANG
262};
263
264template <class _Tp>
265struct __allocator_traits_base<allocator<_Tp> > {
266 using allocator_type = allocator<_Tp>;
267 using value_type = _Tp;
268 using pointer = _Tp*;
269 using const_pointer = const _Tp*;
270 using void_pointer = void*;
271 using const_void_pointer = const void*;
272 using difference_type = ptrdiff_t;
273 using size_type = size_t;
274 using propagate_on_container_copy_assignment = false_type;
275 using propagate_on_container_move_assignment = true_type;
276 using propagate_on_container_swap = false_type;
277 using is_always_equal = true_type;
278
279#ifndef _LIBCPP_CXX03_LANG
280 template <class _Up>
281 using rebind_alloc = allocator<_Up>;
282 template <class _Up>
283 using rebind_traits = allocator_traits<allocator<_Up> >;
284#else
285 template <class _Up>
286 struct rebind_alloc {
287 using other = allocator<_Up>;
288 };
289 template <class _Up>
290 struct rebind_traits {
291 using other = allocator_traits<allocator<_Up> >;
292 };
293#endif
294};
295
296template <class _Alloc>
297struct allocator_traits : __allocator_traits_base<_Alloc> {
298 using __base _LIBCPP_NODEBUG = __allocator_traits_base<_Alloc>;
299
300 using allocator_type = typename __base::allocator_type;
301 using value_type = typename __base::value_type;
302 using pointer = typename __base::pointer;
303 using const_pointer = typename __base::const_pointer;
304 using void_pointer = typename __base::void_pointer;
305 using const_void_pointer = typename __base::const_void_pointer;
306 using difference_type = typename __base::difference_type;
307 using size_type = typename __base::size_type;
308 using propagate_on_container_copy_assignment = typename __base::propagate_on_container_copy_assignment;
309 using propagate_on_container_move_assignment = typename __base::propagate_on_container_move_assignment;
310 using is_always_equal = typename __base::is_always_equal;
311
312 template <class _Tp>
313 using rebind_alloc = typename __base::template rebind_alloc<_Tp>;
314 template <class _Tp>
315 using rebind_traits = typename __base::template rebind_traits<_Tp>;
316
317 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
318 allocate(allocator_type& __a, size_type __n) {
319 return __a.allocate(__n);
320 }
321
322 template <class _Ap = _Alloc, __enable_if_t<__has_allocate_hint_v<_Ap, size_type, const_void_pointer>, int> = 0>
323 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
324 allocate(allocator_type& __a, size_type __n, const_void_pointer __hint) {
325 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
326 return __a.allocate(__n, __hint);
327 _LIBCPP_SUPPRESS_DEPRECATED_POP
328 }
329 template <class _Ap = _Alloc, __enable_if_t<!__has_allocate_hint_v<_Ap, size_type, const_void_pointer>, int> = 0>
330 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
331 allocate(allocator_type& __a, size_type __n, const_void_pointer) {
332 return __a.allocate(__n);
333 }
334
335#if _LIBCPP_STD_VER >= 23
336 template <class _Ap = _Alloc>
337 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr allocation_result<pointer, size_type>
338 allocate_at_least(_Ap& __alloc, size_type __n) {
339 if constexpr (requires { __alloc.allocate_at_least(__n); }) {
340 return __alloc.allocate_at_least(__n);
341 } else {
342 return {__alloc.allocate(__n), __n};
343 }
344 }
345#endif
346
347 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
348 deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT {
349 __a.deallocate(__p, __n);
350 }
351
352 template <class _Tp, class... _Args, __enable_if_t<__has_construct_v<allocator_type, _Tp*, _Args...>, int> = 0>
353 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
354 construct(allocator_type& __a, _Tp* __p, _Args&&... __args) {
355 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
356 __a.construct(__p, std::forward<_Args>(__args)...);
357 _LIBCPP_SUPPRESS_DEPRECATED_POP
358 }
359 template <class _Tp, class... _Args, __enable_if_t<!__has_construct_v<allocator_type, _Tp*, _Args...>, int> = 0>
360 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
361 construct(allocator_type&, _Tp* __p, _Args&&... __args) {
362 std::__construct_at(__p, std::forward<_Args>(__args)...);
363 }
364
365 template <class _Tp, __enable_if_t<__has_destroy_v<allocator_type, _Tp*>, int> = 0>
366 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type& __a, _Tp* __p) {
367 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
368 __a.destroy(__p);
369 _LIBCPP_SUPPRESS_DEPRECATED_POP
370 }
371 template <class _Tp, __enable_if_t<!__has_destroy_v<allocator_type, _Tp*>, int> = 0>
372 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type&, _Tp* __p) {
373 std::__destroy_at(__p);
374 }
375
376 template <class _Ap = _Alloc, __enable_if_t<__has_max_size_v<const _Ap>, int> = 0>
377 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type
378 max_size(const allocator_type& __a) _NOEXCEPT {
379 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
380 return __a.max_size();
381 _LIBCPP_SUPPRESS_DEPRECATED_POP
382 }
383 template <class _Ap = _Alloc, __enable_if_t<!__has_max_size_v<const _Ap>, int> = 0>
384 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type
385 max_size(const allocator_type&) _NOEXCEPT {
386 return numeric_limits<size_type>::max() / sizeof(value_type);
387 }
388
389 template <class _Ap = _Alloc, __enable_if_t<__has_select_on_container_copy_construction_v<const _Ap>, int> = 0>
390 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
391 select_on_container_copy_construction(const allocator_type& __a) {
392 return __a.select_on_container_copy_construction();
393 }
394 template <class _Ap = _Alloc, __enable_if_t<!__has_select_on_container_copy_construction_v<const _Ap>, int> = 0>
395 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
396 select_on_container_copy_construction(const allocator_type& __a) {
397 return __a;
398 }
399};
400
401#ifndef _LIBCPP_CXX03_LANG
402template <class _Traits, class _Tp>
403using __rebind_alloc _LIBCPP_NODEBUG = typename _Traits::template rebind_alloc<_Tp>;
404#else
405template <class _Traits, class _Tp>
406using __rebind_alloc _LIBCPP_NODEBUG = typename _Traits::template rebind_alloc<_Tp>::other;
407#endif
408
409template <class _Alloc>
410struct __check_valid_allocator : true_type {
411 using _Traits _LIBCPP_NODEBUG = std::allocator_traits<_Alloc>;
412 static_assert(is_same<_Alloc, __rebind_alloc<_Traits, typename _Traits::value_type> >::value,
413 "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
414 "original allocator");
415};
416
417// __is_default_allocator_v
418template <class _Tp>
419inline const bool __is_std_allocator_v = false;
420
421template <class _Tp>
422inline const bool __is_std_allocator_v<allocator<_Tp> > = true;
423
424// __is_cpp17_move_insertable_v
425template <class _Alloc>
426inline const bool __is_cpp17_move_insertable_v =
427 is_move_constructible<typename _Alloc::value_type>::value ||
428 (!__is_std_allocator_v<_Alloc> &&
429 __has_construct_v<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>);
430
431// __is_cpp17_copy_insertable_v
432template <class _Alloc>
433inline const bool __is_cpp17_copy_insertable_v =
434 __is_cpp17_move_insertable_v<_Alloc> &&
435 (is_copy_constructible<typename _Alloc::value_type>::value ||
436 (!__is_std_allocator_v<_Alloc> &&
437 __has_construct_v<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>));
438
439_LIBCPP_END_NAMESPACE_STD
440
441_LIBCPP_POP_MACROS
442
443#endif // _LIBCPP___MEMORY_ALLOCATOR_TRAITS_H
444