372 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03___MEMORY_ALLOCATOR_TRAITS_H11#define _LIBCPP___CXX03___MEMORY_ALLOCATOR_TRAITS_H12 13#include <__cxx03/__config>14#include <__cxx03/__memory/construct_at.h>15#include <__cxx03/__memory/pointer_traits.h>16#include <__cxx03/__type_traits/enable_if.h>17#include <__cxx03/__type_traits/is_constructible.h>18#include <__cxx03/__type_traits/is_empty.h>19#include <__cxx03/__type_traits/is_same.h>20#include <__cxx03/__type_traits/make_unsigned.h>21#include <__cxx03/__type_traits/remove_reference.h>22#include <__cxx03/__type_traits/void_t.h>23#include <__cxx03/__utility/declval.h>24#include <__cxx03/__utility/forward.h>25#include <__cxx03/cstddef>26#include <__cxx03/limits>27 28#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)29# pragma GCC system_header30#endif31 32_LIBCPP_PUSH_MACROS33#include <__cxx03/__undef_macros>34 35_LIBCPP_BEGIN_NAMESPACE_STD36 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// __pointer44template <class _Tp,45 class _Alloc,46 class _RawAlloc = __libcpp_remove_reference_t<_Alloc>,47 bool = __has_pointer<_RawAlloc>::value>48struct __pointer {49 using type _LIBCPP_NODEBUG = typename _RawAlloc::pointer;50};51template <class _Tp, class _Alloc, class _RawAlloc>52struct __pointer<_Tp, _Alloc, _RawAlloc, false> {53 using type _LIBCPP_NODEBUG = _Tp*;54};55 56// __const_pointer57_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_pointer, const_pointer);58template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>59struct __const_pointer {60 using type _LIBCPP_NODEBUG = typename _Alloc::const_pointer;61};62template <class _Tp, class _Ptr, class _Alloc>63struct __const_pointer<_Tp, _Ptr, _Alloc, false> {64 using type = typename pointer_traits<_Ptr>::template rebind<const _Tp>::other;65};66 67// __void_pointer68_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_void_pointer, void_pointer);69template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>70struct __void_pointer {71 using type _LIBCPP_NODEBUG = typename _Alloc::void_pointer;72};73template <class _Ptr, class _Alloc>74struct __void_pointer<_Ptr, _Alloc, false> {75 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<void>::other;76};77 78// __const_void_pointer79_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_const_void_pointer, const_void_pointer);80template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>81struct __const_void_pointer {82 using type _LIBCPP_NODEBUG = typename _Alloc::const_void_pointer;83};84template <class _Ptr, class _Alloc>85struct __const_void_pointer<_Ptr, _Alloc, false> {86 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::template rebind<const void>::other;87};88 89// __size_type90_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_size_type, size_type);91template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>92struct __size_type : make_unsigned<_DiffType> {};93template <class _Alloc, class _DiffType>94struct __size_type<_Alloc, _DiffType, true> {95 using type _LIBCPP_NODEBUG = typename _Alloc::size_type;96};97 98// __alloc_traits_difference_type99_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_alloc_traits_difference_type, difference_type);100template <class _Alloc, class _Ptr, bool = __has_alloc_traits_difference_type<_Alloc>::value>101struct __alloc_traits_difference_type {102 using type _LIBCPP_NODEBUG = typename pointer_traits<_Ptr>::difference_type;103};104template <class _Alloc, class _Ptr>105struct __alloc_traits_difference_type<_Alloc, _Ptr, true> {106 using type _LIBCPP_NODEBUG = typename _Alloc::difference_type;107};108 109// __propagate_on_container_copy_assignment110_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_copy_assignment, propagate_on_container_copy_assignment);111template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>112struct __propagate_on_container_copy_assignment : false_type {};113template <class _Alloc>114struct __propagate_on_container_copy_assignment<_Alloc, true> {115 using type _LIBCPP_NODEBUG = typename _Alloc::propagate_on_container_copy_assignment;116};117 118// __propagate_on_container_move_assignment119_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_move_assignment, propagate_on_container_move_assignment);120template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>121struct __propagate_on_container_move_assignment : false_type {};122template <class _Alloc>123struct __propagate_on_container_move_assignment<_Alloc, true> {124 using type _LIBCPP_NODEBUG = typename _Alloc::propagate_on_container_move_assignment;125};126 127// __propagate_on_container_swap128_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_propagate_on_container_swap, propagate_on_container_swap);129template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>130struct __propagate_on_container_swap : false_type {};131template <class _Alloc>132struct __propagate_on_container_swap<_Alloc, true> {133 using type _LIBCPP_NODEBUG = typename _Alloc::propagate_on_container_swap;134};135 136// __is_always_equal137_LIBCPP_ALLOCATOR_TRAITS_HAS_XXX(__has_is_always_equal, is_always_equal);138template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>139struct __is_always_equal : is_empty<_Alloc> {};140template <class _Alloc>141struct __is_always_equal<_Alloc, true> {142 using type _LIBCPP_NODEBUG = typename _Alloc::is_always_equal;143};144 145// __allocator_traits_rebind146_LIBCPP_SUPPRESS_DEPRECATED_PUSH147template <class _Tp, class _Up, class = void>148struct __has_rebind_other : false_type {};149template <class _Tp, class _Up>150struct __has_rebind_other<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>::other> > : true_type {};151 152template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>153struct __allocator_traits_rebind {154 static_assert(__has_rebind_other<_Tp, _Up>::value, "This allocator has to implement rebind");155 using type _LIBCPP_NODEBUG = typename _Tp::template rebind<_Up>::other;156};157template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up>158struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true> {159 using type _LIBCPP_NODEBUG = typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other;160};161template <template <class, class...> class _Alloc, class _Tp, class... _Args, class _Up>162struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false> {163 using type _LIBCPP_NODEBUG = _Alloc<_Up, _Args...>;164};165_LIBCPP_SUPPRESS_DEPRECATED_POP166 167template <class _Alloc, class _Tp>168using __allocator_traits_rebind_t = typename __allocator_traits_rebind<_Alloc, _Tp>::type;169 170_LIBCPP_SUPPRESS_DEPRECATED_PUSH171 172// __has_allocate_hint173template <class _Alloc, class _SizeType, class _ConstVoidPtr, class = void>174struct __has_allocate_hint : false_type {};175 176template <class _Alloc, class _SizeType, class _ConstVoidPtr>177struct __has_allocate_hint<178 _Alloc,179 _SizeType,180 _ConstVoidPtr,181 decltype((void)std::declval<_Alloc>().allocate(std::declval<_SizeType>(), std::declval<_ConstVoidPtr>()))>182 : true_type {};183 184// __has_construct185template <class, class _Alloc, class... _Args>186struct __has_construct_impl : false_type {};187 188template <class _Alloc, class... _Args>189struct __has_construct_impl<decltype((void)std::declval<_Alloc>().construct(std::declval<_Args>()...)),190 _Alloc,191 _Args...> : true_type {};192 193template <class _Alloc, class... _Args>194struct __has_construct : __has_construct_impl<void, _Alloc, _Args...> {};195 196// __has_destroy197template <class _Alloc, class _Pointer, class = void>198struct __has_destroy : false_type {};199 200template <class _Alloc, class _Pointer>201struct __has_destroy<_Alloc, _Pointer, decltype((void)std::declval<_Alloc>().destroy(std::declval<_Pointer>()))>202 : true_type {};203 204// __has_max_size205template <class _Alloc, class = void>206struct __has_max_size : false_type {};207 208template <class _Alloc>209struct __has_max_size<_Alloc, decltype((void)std::declval<_Alloc&>().max_size())> : true_type {};210 211// __has_select_on_container_copy_construction212template <class _Alloc, class = void>213struct __has_select_on_container_copy_construction : false_type {};214 215template <class _Alloc>216struct __has_select_on_container_copy_construction<217 _Alloc,218 decltype((void)std::declval<_Alloc>().select_on_container_copy_construction())> : true_type {};219 220_LIBCPP_SUPPRESS_DEPRECATED_POP221 222template <class _Alloc>223struct _LIBCPP_TEMPLATE_VIS allocator_traits {224 using allocator_type = _Alloc;225 using value_type = typename allocator_type::value_type;226 using pointer = typename __pointer<value_type, allocator_type>::type;227 using const_pointer = typename __const_pointer<value_type, pointer, allocator_type>::type;228 using void_pointer = typename __void_pointer<pointer, allocator_type>::type;229 using const_void_pointer = typename __const_void_pointer<pointer, allocator_type>::type;230 using difference_type = typename __alloc_traits_difference_type<allocator_type, pointer>::type;231 using size_type = typename __size_type<allocator_type, difference_type>::type;232 using propagate_on_container_copy_assignment =233 typename __propagate_on_container_copy_assignment<allocator_type>::type;234 using propagate_on_container_move_assignment =235 typename __propagate_on_container_move_assignment<allocator_type>::type;236 using propagate_on_container_swap = typename __propagate_on_container_swap<allocator_type>::type;237 using is_always_equal = typename __is_always_equal<allocator_type>::type;238 239 template <class _Tp>240 struct rebind_alloc {241 using other = __allocator_traits_rebind_t<allocator_type, _Tp>;242 };243 template <class _Tp>244 struct rebind_traits {245 using other = allocator_traits<typename rebind_alloc<_Tp>::other>;246 };247 248 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI static pointer allocate(allocator_type& __a, size_type __n) {249 return __a.allocate(__n);250 }251 252 template <class _Ap = _Alloc, __enable_if_t<__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0>253 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI static pointer254 allocate(allocator_type& __a, size_type __n, const_void_pointer __hint) {255 _LIBCPP_SUPPRESS_DEPRECATED_PUSH256 return __a.allocate(__n, __hint);257 _LIBCPP_SUPPRESS_DEPRECATED_POP258 }259 template <class _Ap = _Alloc,260 class = void,261 __enable_if_t<!__has_allocate_hint<_Ap, size_type, const_void_pointer>::value, int> = 0>262 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI static pointer263 allocate(allocator_type& __a, size_type __n, const_void_pointer) {264 return __a.allocate(__n);265 }266 267 _LIBCPP_HIDE_FROM_ABI static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT {268 __a.deallocate(__p, __n);269 }270 271 template <class _Tp, class... _Args, __enable_if_t<__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0>272 _LIBCPP_HIDE_FROM_ABI static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args) {273 _LIBCPP_SUPPRESS_DEPRECATED_PUSH274 __a.construct(__p, std::forward<_Args>(__args)...);275 _LIBCPP_SUPPRESS_DEPRECATED_POP276 }277 template <class _Tp,278 class... _Args,279 class = void,280 __enable_if_t<!__has_construct<allocator_type, _Tp*, _Args...>::value, int> = 0>281 _LIBCPP_HIDE_FROM_ABI static void construct(allocator_type&, _Tp* __p, _Args&&... __args) {282 std::__construct_at(__p, std::forward<_Args>(__args)...);283 }284 285 template <class _Tp, __enable_if_t<__has_destroy<allocator_type, _Tp*>::value, int> = 0>286 _LIBCPP_HIDE_FROM_ABI static void destroy(allocator_type& __a, _Tp* __p) {287 _LIBCPP_SUPPRESS_DEPRECATED_PUSH288 __a.destroy(__p);289 _LIBCPP_SUPPRESS_DEPRECATED_POP290 }291 template <class _Tp, class = void, __enable_if_t<!__has_destroy<allocator_type, _Tp*>::value, int> = 0>292 _LIBCPP_HIDE_FROM_ABI static void destroy(allocator_type&, _Tp* __p) {293 std::__destroy_at(__p);294 }295 296 template <class _Ap = _Alloc, __enable_if_t<__has_max_size<const _Ap>::value, int> = 0>297 _LIBCPP_HIDE_FROM_ABI static size_type max_size(const allocator_type& __a) _NOEXCEPT {298 _LIBCPP_SUPPRESS_DEPRECATED_PUSH299 return __a.max_size();300 _LIBCPP_SUPPRESS_DEPRECATED_POP301 }302 template <class _Ap = _Alloc, class = void, __enable_if_t<!__has_max_size<const _Ap>::value, int> = 0>303 _LIBCPP_HIDE_FROM_ABI static size_type max_size(const allocator_type&) _NOEXCEPT {304 return numeric_limits<size_type>::max() / sizeof(value_type);305 }306 307 template <class _Ap = _Alloc, __enable_if_t<__has_select_on_container_copy_construction<const _Ap>::value, int> = 0>308 _LIBCPP_HIDE_FROM_ABI static allocator_type select_on_container_copy_construction(const allocator_type& __a) {309 return __a.select_on_container_copy_construction();310 }311 template <class _Ap = _Alloc,312 class = void,313 __enable_if_t<!__has_select_on_container_copy_construction<const _Ap>::value, int> = 0>314 _LIBCPP_HIDE_FROM_ABI static allocator_type select_on_container_copy_construction(const allocator_type& __a) {315 return __a;316 }317};318 319template <class _Traits, class _Tp>320using __rebind_alloc = typename _Traits::template rebind_alloc<_Tp>::other;321 322template <class _Alloc>323struct __check_valid_allocator : true_type {324 using _Traits = std::allocator_traits<_Alloc>;325 static_assert(is_same<_Alloc, __rebind_alloc<_Traits, typename _Traits::value_type> >::value,326 "[allocator.requirements] states that rebinding an allocator to the same type should result in the "327 "original allocator");328};329 330// __is_default_allocator331template <class _Tp>332struct __is_default_allocator : false_type {};333 334template <class>335class allocator;336 337template <class _Tp>338struct __is_default_allocator<allocator<_Tp> > : true_type {};339 340// __is_cpp17_move_insertable341template <class _Alloc, class = void>342struct __is_cpp17_move_insertable : is_move_constructible<typename _Alloc::value_type> {};343 344template <class _Alloc>345struct __is_cpp17_move_insertable<346 _Alloc,347 __enable_if_t< !__is_default_allocator<_Alloc>::value &&348 __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value > >349 : true_type {};350 351// __is_cpp17_copy_insertable352template <class _Alloc, class = void>353struct __is_cpp17_copy_insertable354 : integral_constant<bool,355 is_copy_constructible<typename _Alloc::value_type>::value &&356 __is_cpp17_move_insertable<_Alloc>::value > {};357 358template <class _Alloc>359struct __is_cpp17_copy_insertable<360 _Alloc,361 __enable_if_t< !__is_default_allocator<_Alloc>::value &&362 __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value > >363 : __is_cpp17_move_insertable<_Alloc> {};364 365#undef _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX366 367_LIBCPP_END_NAMESPACE_STD368 369_LIBCPP_POP_MACROS370 371#endif // _LIBCPP___CXX03___MEMORY_ALLOCATOR_TRAITS_H372