1 // Allocator traits -*- C++ -*-
3 // Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/alloc_traits.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
30 #ifndef _ALLOC_TRAITS_H
31 #define _ALLOC_TRAITS_H 1
33 #if __cplusplus >= 201103L
35 #include <bits/memoryfwd.h>
36 #include <bits/ptr_traits.h>
37 #include <ext/numeric_traits.h>
39 namespace std
_GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 template<typename _Alloc
, typename _Tp
>
44 class __alloctr_rebind_helper
46 template<typename _Alloc2
, typename _Tp2
>
47 static constexpr true_type
48 _S_chk(typename
_Alloc2::template rebind
<_Tp2
>::other
*);
50 template<typename
, typename
>
51 static constexpr false_type
55 using __type
= decltype(_S_chk
<_Alloc
, _Tp
>(nullptr));
58 template<typename _Alloc
, typename _Tp
,
59 bool = __alloctr_rebind_helper
<_Alloc
, _Tp
>::__type::value
>
60 struct __alloctr_rebind
;
62 template<typename _Alloc
, typename _Tp
>
63 struct __alloctr_rebind
<_Alloc
, _Tp
, true>
65 typedef typename
_Alloc::template rebind
<_Tp
>::other __type
;
68 template<template<typename
, typename
...> class _Alloc
, typename _Tp
,
69 typename _Up
, typename
... _Args
>
70 struct __alloctr_rebind
<_Alloc
<_Up
, _Args
...>, _Tp
, false>
72 typedef _Alloc
<_Tp
, _Args
...> __type
;
75 template<typename _Alloc
, typename _Tp
>
76 using __alloc_rebind
= typename __alloctr_rebind
<_Alloc
, _Tp
>::__type
;
79 * @brief Uniform interface to all allocator types.
82 template<typename _Alloc
>
83 struct allocator_traits
85 /// The allocator type
86 typedef _Alloc allocator_type
;
87 /// The allocated type
88 typedef typename
_Alloc::value_type value_type
;
90 #define _GLIBCXX_ALLOC_TR_NESTED_TYPE(_NTYPE, _ALT) \
92 template<typename _Tp> \
93 static typename _Tp::_NTYPE _S_##_NTYPE##_helper(_Tp*); \
94 static _ALT _S_##_NTYPE##_helper(...); \
95 typedef decltype(_S_##_NTYPE##_helper((_Alloc*)0)) __##_NTYPE; \
98 _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer
, value_type
*)
101 * @brief The allocator's pointer type.
103 * @c Alloc::pointer if that type exists, otherwise @c value_type*
105 typedef __pointer pointer
;
107 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_pointer
,
108 typename pointer_traits
<pointer
>::template rebind
<const value_type
>)
111 * @brief The allocator's const pointer type.
113 * @c Alloc::const_pointer if that type exists, otherwise
114 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
116 typedef __const_pointer const_pointer
;
118 _GLIBCXX_ALLOC_TR_NESTED_TYPE(void_pointer
,
119 typename pointer_traits
<pointer
>::template rebind
<void>)
122 * @brief The allocator's void pointer type.
124 * @c Alloc::void_pointer if that type exists, otherwise
125 * <tt> pointer_traits<pointer>::rebind<void> </tt>
127 typedef __void_pointer void_pointer
;
129 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer
,
130 typename pointer_traits
<pointer
>::template rebind
<const void>)
133 * @brief The allocator's const void pointer type.
135 * @c Alloc::const_void_pointer if that type exists, otherwise
136 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
138 typedef __const_void_pointer const_void_pointer
;
140 _GLIBCXX_ALLOC_TR_NESTED_TYPE(difference_type
,
141 typename pointer_traits
<pointer
>::difference_type
)
144 * @brief The allocator's difference type
146 * @c Alloc::difference_type if that type exists, otherwise
147 * <tt> pointer_traits<pointer>::difference_type </tt>
149 typedef __difference_type difference_type
;
151 _GLIBCXX_ALLOC_TR_NESTED_TYPE(size_type
,
152 typename make_unsigned
<difference_type
>::type
)
155 * @brief The allocator's size type
157 * @c Alloc::size_type if that type exists, otherwise
158 * <tt> make_unsigned<difference_type>::type </tt>
160 typedef __size_type size_type
;
162 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_copy_assignment
,
166 * @brief How the allocator is propagated on copy assignment
168 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
169 * otherwise @c false_type
171 typedef __propagate_on_container_copy_assignment
172 propagate_on_container_copy_assignment
;
174 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_move_assignment
,
178 * @brief How the allocator is propagated on move assignment
180 * @c Alloc::propagate_on_container_move_assignment if that type exists,
181 * otherwise @c false_type
183 typedef __propagate_on_container_move_assignment
184 propagate_on_container_move_assignment
;
186 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap
,
190 * @brief How the allocator is propagated on swap
192 * @c Alloc::propagate_on_container_swap if that type exists,
193 * otherwise @c false_type
195 typedef __propagate_on_container_swap propagate_on_container_swap
;
197 _GLIBCXX_ALLOC_TR_NESTED_TYPE(is_always_equal
,
198 typename is_empty
<_Alloc
>::type
)
201 * @brief Whether all instances of the allocator type compare equal.
203 * @c Alloc::is_always_equal if that type exists,
204 * otherwise @c is_empty<Alloc>::type
206 typedef __is_always_equal is_always_equal
;
208 #undef _GLIBCXX_ALLOC_TR_NESTED_TYPE
210 template<typename _Tp
>
211 using rebind_alloc
= typename __alloctr_rebind
<_Alloc
, _Tp
>::__type
;
212 template<typename _Tp
>
213 using rebind_traits
= allocator_traits
<rebind_alloc
<_Tp
>>;
216 template<typename _Alloc2
>
217 struct __allocate_helper
219 template<typename _Alloc3
,
220 typename
= decltype(std::declval
<_Alloc3
*>()->allocate(
221 std::declval
<size_type
>(),
222 std::declval
<const_void_pointer
>()))>
223 static true_type
__test(int);
226 static false_type
__test(...);
228 using type
= decltype(__test
<_Alloc
>(0));
231 template<typename _Alloc2
>
232 using __has_allocate
= typename __allocate_helper
<_Alloc2
>::type
;
234 template<typename _Alloc2
,
235 typename
= _Require
<__has_allocate
<_Alloc2
>>>
237 _S_allocate(_Alloc2
& __a
, size_type __n
, const_void_pointer __hint
)
238 { return __a
.allocate(__n
, __hint
); }
240 template<typename _Alloc2
, typename _UnusedHint
,
241 typename
= _Require
<__not_
<__has_allocate
<_Alloc2
>>>>
243 _S_allocate(_Alloc2
& __a
, size_type __n
, _UnusedHint
)
244 { return __a
.allocate(__n
); }
246 template<typename _Tp
, typename
... _Args
>
247 struct __construct_helper
249 template<typename _Alloc2
,
250 typename
= decltype(std::declval
<_Alloc2
*>()->construct(
251 std::declval
<_Tp
*>(), std::declval
<_Args
>()...))>
252 static true_type
__test(int);
255 static false_type
__test(...);
257 using type
= decltype(__test
<_Alloc
>(0));
260 template<typename _Tp
, typename
... _Args
>
261 using __has_construct
262 = typename __construct_helper
<_Tp
, _Args
...>::type
;
264 template<typename _Tp
, typename
... _Args
>
265 static _Require
<__has_construct
<_Tp
, _Args
...>>
266 _S_construct(_Alloc
& __a
, _Tp
* __p
, _Args
&&... __args
)
267 { __a
.construct(__p
, std::forward
<_Args
>(__args
)...); }
269 template<typename _Tp
, typename
... _Args
>
271 _Require
<__and_
<__not_
<__has_construct
<_Tp
, _Args
...>>,
272 is_constructible
<_Tp
, _Args
...>>>
273 _S_construct(_Alloc
&, _Tp
* __p
, _Args
&&... __args
)
274 { ::new((void*)__p
) _Tp(std::forward
<_Args
>(__args
)...); }
276 template<typename _Tp
>
277 struct __destroy_helper
279 template<typename _Alloc2
,
280 typename
= decltype(std::declval
<_Alloc2
*>()->destroy(
281 std::declval
<_Tp
*>()))>
282 static true_type
__test(int);
285 static false_type
__test(...);
287 using type
= decltype(__test
<_Alloc
>(0));
290 template<typename _Tp
>
291 using __has_destroy
= typename __destroy_helper
<_Tp
>::type
;
293 template<typename _Tp
>
294 static _Require
<__has_destroy
<_Tp
>>
295 _S_destroy(_Alloc
& __a
, _Tp
* __p
)
296 { __a
.destroy(__p
); }
298 template<typename _Tp
>
299 static _Require
<__not_
<__has_destroy
<_Tp
>>>
300 _S_destroy(_Alloc
&, _Tp
* __p
)
303 template<typename _Alloc2
>
304 struct __maxsize_helper
306 template<typename _Alloc3
,
307 typename
= decltype(std::declval
<_Alloc3
*>()->max_size())>
308 static true_type
__test(int);
311 static false_type
__test(...);
313 using type
= decltype(__test
<_Alloc2
>(0));
316 template<typename _Alloc2
>
317 using __has_max_size
= typename __maxsize_helper
<_Alloc2
>::type
;
319 template<typename _Alloc2
,
320 typename
= _Require
<__has_max_size
<_Alloc2
>>>
322 _S_max_size(_Alloc2
& __a
, int)
323 { return __a
.max_size(); }
325 template<typename _Alloc2
,
326 typename
= _Require
<__not_
<__has_max_size
<_Alloc2
>>>>
328 _S_max_size(_Alloc2
&, ...)
330 // _GLIBCXX_RESOLVE_LIB_DEFECTS
331 // 2466. allocator_traits::max_size() default behavior is incorrect
332 return __gnu_cxx::__numeric_traits
<size_type
>::__max
333 / sizeof(value_type
);
336 template<typename _Alloc2
>
337 struct __select_helper
339 template<typename _Alloc3
, typename
340 = decltype(std::declval
<_Alloc3
*>()
341 ->select_on_container_copy_construction())>
342 static true_type
__test(int);
345 static false_type
__test(...);
347 using type
= decltype(__test
<_Alloc2
>(0));
350 template<typename _Alloc2
>
351 using __has_soccc
= typename __select_helper
<_Alloc2
>::type
;
353 template<typename _Alloc2
,
354 typename
= _Require
<__has_soccc
<_Alloc2
>>>
356 _S_select(_Alloc2
& __a
, int)
357 { return __a
.select_on_container_copy_construction(); }
359 template<typename _Alloc2
,
360 typename
= _Require
<__not_
<__has_soccc
<_Alloc2
>>>>
362 _S_select(_Alloc2
& __a
, ...)
368 * @brief Allocate memory.
369 * @param __a An allocator.
370 * @param __n The number of objects to allocate space for.
372 * Calls @c a.allocate(n)
375 allocate(_Alloc
& __a
, size_type __n
)
376 { return __a
.allocate(__n
); }
379 * @brief Allocate memory.
380 * @param __a An allocator.
381 * @param __n The number of objects to allocate space for.
382 * @param __hint Aid to locality.
383 * @return Memory of suitable size and alignment for @a n objects
384 * of type @c value_type
386 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
387 * well-formed, otherwise returns @c a.allocate(n)
390 allocate(_Alloc
& __a
, size_type __n
, const_void_pointer __hint
)
391 { return _S_allocate(__a
, __n
, __hint
); }
394 * @brief Deallocate memory.
395 * @param __a An allocator.
396 * @param __p Pointer to the memory to deallocate.
397 * @param __n The number of objects space was allocated for.
399 * Calls <tt> a.deallocate(p, n) </tt>
401 static void deallocate(_Alloc
& __a
, pointer __p
, size_type __n
)
402 { __a
.deallocate(__p
, __n
); }
405 * @brief Construct an object of type @a _Tp
406 * @param __a An allocator.
407 * @param __p Pointer to memory of suitable size and alignment for Tp
408 * @param __args Constructor arguments.
410 * Calls <tt> __a.construct(__p, std::forward<Args>(__args)...) </tt>
411 * if that expression is well-formed, otherwise uses placement-new
412 * to construct an object of type @a _Tp at location @a __p from the
413 * arguments @a __args...
415 template<typename _Tp
, typename
... _Args
>
416 static auto construct(_Alloc
& __a
, _Tp
* __p
, _Args
&&... __args
)
417 -> decltype(_S_construct(__a
, __p
, std::forward
<_Args
>(__args
)...))
418 { _S_construct(__a
, __p
, std::forward
<_Args
>(__args
)...); }
421 * @brief Destroy an object of type @a _Tp
422 * @param __a An allocator.
423 * @param __p Pointer to the object to destroy
425 * Calls @c __a.destroy(__p) if that expression is well-formed,
426 * otherwise calls @c __p->~_Tp()
429 static void destroy(_Alloc
& __a
, _Tp
* __p
)
430 { _S_destroy(__a
, __p
); }
433 * @brief The maximum supported allocation size
434 * @param __a An allocator.
435 * @return @c __a.max_size() or @c numeric_limits<size_type>::max()
437 * Returns @c __a.max_size() if that expression is well-formed,
438 * otherwise returns @c numeric_limits<size_type>::max()
440 static size_type
max_size(const _Alloc
& __a
) noexcept
441 { return _S_max_size(__a
, 0); }
444 * @brief Obtain an allocator to use when copying a container.
445 * @param __rhs An allocator.
446 * @return @c __rhs.select_on_container_copy_construction() or @a __rhs
448 * Returns @c __rhs.select_on_container_copy_construction() if that
449 * expression is well-formed, otherwise returns @a __rhs
452 select_on_container_copy_construction(const _Alloc
& __rhs
)
453 { return _S_select(__rhs
, 0); }
456 template<typename _Alloc
>
458 __do_alloc_on_copy(_Alloc
& __one
, const _Alloc
& __two
, true_type
)
461 template<typename _Alloc
>
463 __do_alloc_on_copy(_Alloc
&, const _Alloc
&, false_type
)
466 template<typename _Alloc
>
467 inline void __alloc_on_copy(_Alloc
& __one
, const _Alloc
& __two
)
469 typedef allocator_traits
<_Alloc
> __traits
;
470 typedef typename
__traits::propagate_on_container_copy_assignment __pocca
;
471 __do_alloc_on_copy(__one
, __two
, __pocca());
474 template<typename _Alloc
>
475 inline _Alloc
__alloc_on_copy(const _Alloc
& __a
)
477 typedef allocator_traits
<_Alloc
> __traits
;
478 return __traits::select_on_container_copy_construction(__a
);
481 template<typename _Alloc
>
482 inline void __do_alloc_on_move(_Alloc
& __one
, _Alloc
& __two
, true_type
)
483 { __one
= std::move(__two
); }
485 template<typename _Alloc
>
486 inline void __do_alloc_on_move(_Alloc
&, _Alloc
&, false_type
)
489 template<typename _Alloc
>
490 inline void __alloc_on_move(_Alloc
& __one
, _Alloc
& __two
)
492 typedef allocator_traits
<_Alloc
> __traits
;
493 typedef typename
__traits::propagate_on_container_move_assignment __pocma
;
494 __do_alloc_on_move(__one
, __two
, __pocma());
497 template<typename _Alloc
>
498 inline void __do_alloc_on_swap(_Alloc
& __one
, _Alloc
& __two
, true_type
)
504 template<typename _Alloc
>
505 inline void __do_alloc_on_swap(_Alloc
&, _Alloc
&, false_type
)
508 template<typename _Alloc
>
509 inline void __alloc_on_swap(_Alloc
& __one
, _Alloc
& __two
)
511 typedef allocator_traits
<_Alloc
> __traits
;
512 typedef typename
__traits::propagate_on_container_swap __pocs
;
513 __do_alloc_on_swap(__one
, __two
, __pocs());
516 template<typename _Alloc
>
517 class __is_copy_insertable_impl
519 typedef allocator_traits
<_Alloc
> _Traits
;
521 template<typename _Up
, typename
522 = decltype(_Traits::construct(std::declval
<_Alloc
&>(),
523 std::declval
<_Up
*>(),
524 std::declval
<const _Up
&>()))>
528 template<typename _Up
>
533 typedef decltype(_M_select
<typename
_Alloc::value_type
>(0)) type
;
536 // true if _Alloc::value_type is CopyInsertable into containers using _Alloc
537 template<typename _Alloc
>
538 struct __is_copy_insertable
539 : __is_copy_insertable_impl
<_Alloc
>::type
542 // std::allocator<_Tp> just requires CopyConstructible
543 template<typename _Tp
>
544 struct __is_copy_insertable
<allocator
<_Tp
>>
545 : is_copy_constructible
<_Tp
>
548 _GLIBCXX_END_NAMESPACE_VERSION