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