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