File indexing completed on 2026-05-03 08:13:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___MEMORY_ALLOCATOR_TRAITS_H
0011 #define _LIBCPP___MEMORY_ALLOCATOR_TRAITS_H
0012
0013 #include <__config>
0014 #include <__cstddef/size_t.h>
0015 #include <__fwd/memory.h>
0016 #include <__memory/construct_at.h>
0017 #include <__memory/pointer_traits.h>
0018 #include <__type_traits/detected_or.h>
0019 #include <__type_traits/enable_if.h>
0020 #include <__type_traits/is_constructible.h>
0021 #include <__type_traits/is_empty.h>
0022 #include <__type_traits/is_same.h>
0023 #include <__type_traits/make_unsigned.h>
0024 #include <__type_traits/remove_reference.h>
0025 #include <__type_traits/void_t.h>
0026 #include <__utility/declval.h>
0027 #include <__utility/forward.h>
0028 #include <limits>
0029
0030 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0031 # pragma GCC system_header
0032 #endif
0033
0034 _LIBCPP_PUSH_MACROS
0035 #include <__undef_macros>
0036
0037 _LIBCPP_BEGIN_NAMESPACE_STD
0038
0039 #define _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(NAME, PROPERTY) \
0040 template <class _Tp, class = void> \
0041 struct NAME : false_type {}; \
0042 template <class _Tp> \
0043 struct NAME<_Tp, __void_t<typename _Tp::PROPERTY > > : true_type {}
0044
0045
0046 template <class _Tp>
0047 using __pointer_member _LIBCPP_NODEBUG = typename _Tp::pointer;
0048
0049 template <class _Tp, class _Alloc>
0050 using __pointer _LIBCPP_NODEBUG = __detected_or_t<_Tp*, __pointer_member, __libcpp_remove_reference_t<_Alloc> >;
0051
0052
0053 _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_pointer, const_pointer);
0054 template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
0055 struct __const_pointer {
0056 using type _LIBCPP_NODEBUG = typename _Alloc::const_pointer;
0057 };
0058 template <class _Tp, class _Ptr, class _Alloc>
0059 struct __const_pointer<_Tp, _Ptr, _Alloc, false> {
0060 #ifdef _LIBCPP_CXX03_LANG
0061 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const _Tp>::other;
0062 #else
0063 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const _Tp>;
0064 #endif
0065 };
0066
0067
0068 _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_void_pointer, void_pointer);
0069 template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
0070 struct __void_pointer {
0071 using type _LIBCPP_NODEBUG = typename _Alloc::void_pointer;
0072 };
0073 template <class _Ptr, class _Alloc>
0074 struct __void_pointer<_Ptr, _Alloc, false> {
0075 #ifdef _LIBCPP_CXX03_LANG
0076 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<void>::other;
0077 #else
0078 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<void>;
0079 #endif
0080 };
0081
0082
0083 _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_void_pointer, const_void_pointer);
0084 template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
0085 struct __const_void_pointer {
0086 using type _LIBCPP_NODEBUG = typename _Alloc::const_void_pointer;
0087 };
0088 template <class _Ptr, class _Alloc>
0089 struct __const_void_pointer<_Ptr, _Alloc, false> {
0090 #ifdef _LIBCPP_CXX03_LANG
0091 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const void>::other;
0092 #else
0093 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const void>;
0094 #endif
0095 };
0096
0097
0098 template <class _Tp>
0099 using __size_type_member _LIBCPP_NODEBUG = typename _Tp::size_type;
0100
0101 template <class _Alloc, class _DiffType>
0102 using __size_type _LIBCPP_NODEBUG = __detected_or_t<__make_unsigned_t<_DiffType>, __size_type_member, _Alloc>;
0103
0104
0105 _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_alloc_traits_difference_type, difference_type);
0106 template <class _Alloc, class _Ptr, bool = __has_alloc_traits_difference_type<_Alloc>::value>
0107 struct __alloc_traits_difference_type {
0108 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::difference_type;
0109 };
0110 template <class _Alloc, class _Ptr>
0111 struct __alloc_traits_difference_type<_Alloc, _Ptr, true> {
0112 using type _LIBCPP_NODEBUG = typename _Alloc::difference_type;
0113 };
0114
0115
0116 template <class _Tp>
0117 using __propagate_on_container_copy_assignment_member _LIBCPP_NODEBUG =
0118 typename _Tp::propagate_on_container_copy_assignment;
0119
0120 template <class _Alloc>
0121 using __propagate_on_container_copy_assignment _LIBCPP_NODEBUG =
0122 __detected_or_t<false_type, __propagate_on_container_copy_assignment_member, _Alloc>;
0123
0124
0125 template <class _Tp>
0126 using __propagate_on_container_move_assignment_member _LIBCPP_NODEBUG =
0127 typename _Tp::propagate_on_container_move_assignment;
0128
0129 template <class _Alloc>
0130 using __propagate_on_container_move_assignment _LIBCPP_NODEBUG =
0131 __detected_or_t<false_type, __propagate_on_container_move_assignment_member, _Alloc>;
0132
0133
0134 template <class _Tp>
0135 using __propagate_on_container_swap_member _LIBCPP_NODEBUG = typename _Tp::propagate_on_container_swap;
0136
0137 template <class _Alloc>
0138 using __propagate_on_container_swap _LIBCPP_NODEBUG =
0139 __detected_or_t<false_type, __propagate_on_container_swap_member, _Alloc>;
0140
0141
0142 template <class _Tp>
0143 using __is_always_equal_member _LIBCPP_NODEBUG = typename _Tp::is_always_equal;
0144
0145 template <class _Alloc>
0146 using __is_always_equal _LIBCPP_NODEBUG =
0147 __detected_or_t<typename is_empty<_Alloc>::type, __is_always_equal_member, _Alloc>;
0148
0149
0150 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0151 template <class _Tp, class _Up, class = void>
0152 struct __has_rebind_other : false_type {};
0153 template <class _Tp, class _Up>
0154 struct __has_rebind_other<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>::other> > : true_type {};
0155
0156 template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
0157 struct __allocator_traits_rebind {
0158 static_assert(__has_rebind_other<_Tp, _Up>::value, "This allocator has to implement rebind");
0159 using type _LIBCPP_NODEBUG = typename _Tp::template rebind<_Up>::other;
0160 };
0161 template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up>
0162 struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true> {
0163 using type _LIBCPP_NODEBUG = typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other;
0164 };
0165 template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up>
0166 struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false> {
0167 using type _LIBCPP_NODEBUG = _Alloc<_Up, _Args...>;
0168 };
0169 _LIBCPP_SUPPRESS_DEPRECATED_POP
0170
0171 template <class _Alloc, class _Tp>
0172 using __allocator_traits_rebind_t _LIBCPP_NODEBUG = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
0173
0174 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0175
0176
0177 template <class _Alloc, class _SizeType, class _ConstVoidPtr, class = void>
0178 struct __has_allocate_hint : false_type {};
0179
0180 template <class _Alloc, class _SizeType, class _ConstVoidPtr>
0181 struct __has_allocate_hint<
0182 _Alloc,
0183 _SizeType,
0184 _ConstVoidPtr,
0185 decltype((void)std::declval<_Alloc>().allocate(std::declval<_SizeType>(), std::declval<_ConstVoidPtr>()))>
0186 : true_type {};
0187
0188
0189 template <class, class _Alloc, class... _Args>
0190 struct __has_construct_impl : false_type {};
0191
0192 template <class _Alloc, class... _Args>
0193 struct __has_construct_impl<decltype((void)std::declval<_Alloc>().construct(std::declval<_Args>()...)),
0194 _Alloc,
0195 _Args...> : true_type {};
0196
0197 template <class _Alloc, class... _Args>
0198 struct __has_construct : __has_construct_impl<void, _Alloc, _Args...> {};
0199
0200
0201 template <class _Alloc, class _Pointer, class = void>
0202 struct __has_destroy : false_type {};
0203
0204 template <class _Alloc, class _Pointer>
0205 struct __has_destroy<_Alloc, _Pointer, decltype((void)std::declval<_Alloc>().destroy(std::declval<_Pointer>()))>
0206 : true_type {};
0207
0208
0209 template <class _Alloc, class = void>
0210 struct __has_max_size : false_type {};
0211
0212 template <class _Alloc>
0213 struct __has_max_size<_Alloc, decltype((void)std::declval<_Alloc&>().max_size())> : true_type {};
0214
0215
0216 template <class _Alloc, class = void>
0217 struct __has_select_on_container_copy_construction : false_type {};
0218
0219 template <class _Alloc>
0220 struct __has_select_on_container_copy_construction<
0221 _Alloc,
0222 decltype((void)std::declval<_Alloc>().select_on_container_copy_construction())> : true_type {};
0223
0224 _LIBCPP_SUPPRESS_DEPRECATED_POP
0225
0226 #if _LIBCPP_STD_VER >= 23
0227
0228 template <class _Pointer, class _SizeType = size_t>
0229 struct allocation_result {
0230 _Pointer ptr;
0231 _SizeType count;
0232 };
0233 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(allocation_result);
0234
0235 #endif
0236
0237 template <class _Alloc>
0238 struct _LIBCPP_TEMPLATE_VIS allocator_traits {
0239 using allocator_type = _Alloc;
0240 using value_type = typename allocator_type::value_type;
0241 using pointer = __pointer<value_type, allocator_type>;
0242 using const_pointer = typename __const_pointer<value_type, pointer, allocator_type>::type;
0243 using void_pointer = typename __void_pointer<pointer, allocator_type>::type;
0244 using const_void_pointer = typename __const_void_pointer<pointer, allocator_type>::type;
0245 using difference_type = typename __alloc_traits_difference_type<allocator_type, pointer>::type;
0246 using size_type = __size_type<allocator_type, difference_type>;
0247 using propagate_on_container_copy_assignment = __propagate_on_container_copy_assignment<allocator_type>;
0248 using propagate_on_container_move_assignment = __propagate_on_container_move_assignment<allocator_type>;
0249 using propagate_on_container_swap = __propagate_on_container_swap<allocator_type>;
0250 using is_always_equal = __is_always_equal<allocator_type>;
0251
0252 #ifndef _LIBCPP_CXX03_LANG
0253 template <class _Tp>
0254 using rebind_alloc = __allocator_traits_rebind_t<allocator_type, _Tp>;
0255 template <class _Tp>
0256 using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
0257 #else
0258 template <class _Tp>
0259 struct rebind_alloc {
0260 using other = __allocator_traits_rebind_t<allocator_type, _Tp>;
0261 };
0262 template <class _Tp>
0263 struct rebind_traits {
0264 using other = allocator_traits<typename rebind_alloc<_Tp>::other>;
0265 };
0266 #endif
0267
0268 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
0269 allocate(allocator_type& __a, size_type __n) {
0270 return __a.allocate(__n);
0271 }
0272
0273 template <class _Ap = _Alloc, __enable_if_t<__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0>
0274 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
0275 allocate(allocator_type& __a, size_type __n, const_void_pointer __hint) {
0276 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0277 return __a.allocate(__n, __hint);
0278 _LIBCPP_SUPPRESS_DEPRECATED_POP
0279 }
0280 template <class _Ap = _Alloc,
0281 class = void,
0282 __enable_if_t<!__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0>
0283 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
0284 allocate(allocator_type& __a, size_type __n, const_void_pointer) {
0285 return __a.allocate(__n);
0286 }
0287
0288 #if _LIBCPP_STD_VER >= 23
0289 template <class _Ap = _Alloc>
0290 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr allocation_result<pointer, size_type>
0291 allocate_at_least(_Ap& __alloc, size_type __n) {
0292 if constexpr (requires { __alloc.allocate_at_least(__n); }) {
0293 return __alloc.allocate_at_least(__n);
0294 } else {
0295 return {__alloc.allocate(__n), __n};
0296 }
0297 }
0298 #endif
0299
0300 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
0301 deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT {
0302 __a.deallocate(__p, __n);
0303 }
0304
0305 template <class _Tp, class... _Args, __enable_if_t<__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0>
0306 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
0307 construct(allocator_type& __a, _Tp* __p, _Args&&... __args) {
0308 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0309 __a.construct(__p, std::forward<_Args>(__args)...);
0310 _LIBCPP_SUPPRESS_DEPRECATED_POP
0311 }
0312 template <class _Tp,
0313 class... _Args,
0314 class = void,
0315 __enable_if_t<!__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0>
0316 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void
0317 construct(allocator_type&, _Tp* __p, _Args&&... __args) {
0318 std::__construct_at(__p, std::forward<_Args>(__args)...);
0319 }
0320
0321 template <class _Tp, __enable_if_t<__has_destroy<allocator_type, _Tp*>::value, int> = 0>
0322 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type& __a, _Tp* __p) {
0323 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0324 __a.destroy(__p);
0325 _LIBCPP_SUPPRESS_DEPRECATED_POP
0326 }
0327 template <class _Tp, class = void, __enable_if_t<!__has_destroy<allocator_type, _Tp*>::value, int> = 0>
0328 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static void destroy(allocator_type&, _Tp* __p) {
0329 std::__destroy_at(__p);
0330 }
0331
0332 template <class _Ap = _Alloc, __enable_if_t<__has_max_size<const _Ap>::value, int> = 0>
0333 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type max_size(const allocator_type& __a) _NOEXCEPT {
0334 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0335 return __a.max_size();
0336 _LIBCPP_SUPPRESS_DEPRECATED_POP
0337 }
0338 template <class _Ap = _Alloc, class = void, __enable_if_t<!__has_max_size<const _Ap>::value, int> = 0>
0339 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static size_type max_size(const allocator_type&) _NOEXCEPT {
0340 return numeric_limits<size_type>::max() / sizeof(value_type);
0341 }
0342
0343 template <class _Ap = _Alloc, __enable_if_t<__has_select_on_container_copy_construction<const _Ap>::value, int> = 0>
0344 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
0345 select_on_container_copy_construction(const allocator_type& __a) {
0346 return __a.select_on_container_copy_construction();
0347 }
0348 template <class _Ap = _Alloc,
0349 class = void,
0350 __enable_if_t<!__has_select_on_container_copy_construction<const _Ap>::value, int> = 0>
0351 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static allocator_type
0352 select_on_container_copy_construction(const allocator_type& __a) {
0353 return __a;
0354 }
0355 };
0356
0357 #ifndef _LIBCPP_CXX03_LANG
0358 template <class _Traits, class _Tp>
0359 using __rebind_alloc _LIBCPP_NODEBUG = typename _Traits::template rebind_alloc<_Tp>;
0360 #else
0361 template <class _Traits, class _Tp>
0362 using __rebind_alloc _LIBCPP_NODEBUG = typename _Traits::template rebind_alloc<_Tp>::other;
0363 #endif
0364
0365 template <class _Alloc>
0366 struct __check_valid_allocator : true_type {
0367 using _Traits _LIBCPP_NODEBUG = std::allocator_traits<_Alloc>;
0368 static_assert(is_same<_Alloc, __rebind_alloc<_Traits, typename _Traits::value_type> >::value,
0369 "[allocator.requirements] states that rebinding an allocator to the same type should result in the "
0370 "original allocator");
0371 };
0372
0373
0374 template <class _Tp>
0375 struct __is_default_allocator : false_type {};
0376
0377 template <class>
0378 class allocator;
0379
0380 template <class _Tp>
0381 struct __is_default_allocator<allocator<_Tp> > : true_type {};
0382
0383
0384 template <class _Alloc, class = void>
0385 struct __is_cpp17_move_insertable : is_move_constructible<typename _Alloc::value_type> {};
0386
0387 template <class _Alloc>
0388 struct __is_cpp17_move_insertable<
0389 _Alloc,
0390 __enable_if_t< !__is_default_allocator<_Alloc>::value &&
0391 __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value > >
0392 : true_type {};
0393
0394
0395 template <class _Alloc, class = void>
0396 struct __is_cpp17_copy_insertable
0397 : integral_constant<bool,
0398 is_copy_constructible<typename _Alloc::value_type>::value &&
0399 __is_cpp17_move_insertable<_Alloc>::value > {};
0400
0401 template <class _Alloc>
0402 struct __is_cpp17_copy_insertable<
0403 _Alloc,
0404 __enable_if_t< !__is_default_allocator<_Alloc>::value &&
0405 __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value > >
0406 : __is_cpp17_move_insertable<_Alloc> {};
0407
0408 #undef _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX
0409
0410 _LIBCPP_END_NAMESPACE_STD
0411
0412 _LIBCPP_POP_MACROS
0413
0414 #endif