1 // Allocator traits -*- C++ -*-
3 // Copyright (C) 2011 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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
35 #include <bits/ptr_traits.h>
36 #include <ext/numeric_traits.h>
38 namespace std _GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 template<typename _Alloc, typename _Tp>
43 class __alloctr_rebind_helper
45 template<typename _Alloc2, typename _Tp2>
47 _S_chk(typename _Alloc2::template rebind<_Tp2>::other*)
50 template<typename, typename>
56 static const bool __value = _S_chk<_Alloc, _Tp>(nullptr);
59 template<typename _Alloc, typename _Tp,
60 bool = __alloctr_rebind_helper<_Alloc, _Tp>::__value>
61 struct __alloctr_rebind;
63 template<typename _Alloc, typename _Tp>
64 struct __alloctr_rebind<_Alloc, _Tp, true>
66 typedef typename _Alloc::template rebind<_Tp>::other __type;
69 template<template<typename, typename...> class _Alloc, typename _Tp,
70 typename _Up, typename... _Args>
71 struct __alloctr_rebind<_Alloc<_Up, _Args...>, _Tp, false>
73 typedef _Alloc<_Tp, _Args...> __type;
77 * @brief Uniform interface to all allocator types.
80 template<typename _Alloc>
81 struct allocator_traits
83 /// The allocator type
84 typedef _Alloc allocator_type;
85 /// The allocated type
86 typedef typename _Alloc::value_type value_type;
88 #define _GLIBCXX_ALLOC_TR_NESTED_TYPE(_NTYPE, _ALT) \
90 template<typename _Tp> \
91 static typename _Tp::_NTYPE _S_##_NTYPE##_helper(_Tp*); \
92 static _ALT _S_##_NTYPE##_helper(...); \
93 typedef decltype(_S_##_NTYPE##_helper((_Alloc*)0)) __##_NTYPE; \
96 _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer, value_type*)
99 * @brief The allocator's pointer type.
101 * @c Alloc::pointer if that type exists, otherwise @c value_type*
103 typedef __pointer pointer;
105 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_pointer,
106 typename pointer_traits<pointer>::template rebind<const value_type>)
109 * @brief The allocator's const pointer type.
111 * @c Alloc::const_pointer if that type exists, otherwise
112 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
114 typedef __const_pointer const_pointer;
116 _GLIBCXX_ALLOC_TR_NESTED_TYPE(void_pointer,
117 typename pointer_traits<pointer>::template rebind<void>)
120 * @brief The allocator's void pointer type.
122 * @c Alloc::void_pointer if that type exists, otherwise
123 * <tt> pointer_traits<pointer>::rebind<void> </tt>
125 typedef __void_pointer void_pointer;
127 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer,
128 typename pointer_traits<pointer>::template rebind<const void>)
131 * @brief The allocator's const void pointer type.
133 * @c Alloc::const_void_pointer if that type exists, otherwise
134 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
136 typedef __const_void_pointer const_void_pointer;
138 _GLIBCXX_ALLOC_TR_NESTED_TYPE(difference_type,
139 typename pointer_traits<pointer>::difference_type)
142 * @brief The allocator's difference type
144 * @c Alloc::difference_type if that type exists, otherwise
145 * <tt> pointer_traits<pointer>::difference_type </tt>
147 typedef __difference_type difference_type;
149 _GLIBCXX_ALLOC_TR_NESTED_TYPE(size_type,
150 typename make_unsigned<difference_type>::type)
153 * @brief The allocator's size type
155 * @c Alloc::size_type if that type exists, otherwise
156 * <tt> make_unsigned<difference_type>::type </tt>
158 typedef __size_type size_type;
160 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_copy_assignment,
164 * @brief How the allocator is propagated on copy assignment
166 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
167 * otherwise @c false_type
169 typedef __propagate_on_container_copy_assignment
170 propagate_on_container_copy_assignment;
172 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_move_assignment,
176 * @brief How the allocator is propagated on move assignment
178 * @c Alloc::propagate_on_container_move_assignment if that type exists,
179 * otherwise @c false_type
181 typedef __propagate_on_container_move_assignment
182 propagate_on_container_move_assignment;
184 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap,
188 * @brief How the allocator is propagated on swap
190 * @c Alloc::propagate_on_container_swap if that type exists,
191 * otherwise @c false_type
193 typedef __propagate_on_container_swap propagate_on_container_swap;
195 #undef _GLIBCXX_ALLOC_TR_NESTED_TYPE
197 template<typename _Tp>
198 using rebind_alloc = typename __alloctr_rebind<_Alloc, _Tp>::__type;
199 template<typename _Tp>
200 using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
203 template<typename _Alloc2>
204 struct __allocate_helper
206 template<typename _Alloc3,
207 typename = decltype(std::declval<_Alloc3*>()->allocate(
208 std::declval<size_type>(),
209 std::declval<const_void_pointer>()))>
210 static true_type __test(int);
213 static false_type __test(...);
215 typedef decltype(__test<_Alloc>(0)) type;
216 static const bool value = type::value;
219 template<typename _Alloc2>
221 enable_if<__allocate_helper<_Alloc2>::value, pointer>::type
222 _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer __hint)
223 { return __a.allocate(__n, __hint); }
225 template<typename _Alloc2>
227 enable_if<!__allocate_helper<_Alloc2>::value, pointer>::type
228 _S_allocate(_Alloc2& __a, size_type __n, ...)
229 { return __a.allocate(__n); }
231 template<typename _Tp, typename... _Args>
232 struct __construct_helper
234 template<typename _Alloc2,
235 typename = decltype(std::declval<_Alloc2*>()->construct(
236 std::declval<_Tp*>(), std::declval<_Args>()...))>
237 static true_type __test(int);
240 static false_type __test(...);
242 typedef decltype(__test<_Alloc>(0)) type;
243 static const bool value = type::value;
246 template<typename _Tp, typename... _Args>
248 enable_if<__construct_helper<_Tp, _Args...>::value, void>::type
249 _S_construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
250 { __a.construct(__p, std::forward<_Args>(__args)...); }
252 template<typename _Tp, typename... _Args>
254 enable_if<!__construct_helper<_Tp, _Args...>::value, void>::type
255 _S_construct(_Alloc&, _Tp* __p, _Args&&... __args)
256 { ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); }
258 template<typename _Tp>
259 struct __destroy_helper
261 template<typename _Alloc2,
262 typename = decltype(std::declval<_Alloc2*>()->destroy(
263 std::declval<_Tp*>()))>
264 static true_type __test(int);
267 static false_type __test(...);
269 typedef decltype(__test<_Alloc>(0)) type;
270 static const bool value = type::value;
273 template<typename _Tp>
274 static typename enable_if<__destroy_helper<_Tp>::value, void>::type
275 _S_destroy(_Alloc& __a, _Tp* __p)
276 { __a.destroy(__p); }
278 template<typename _Tp>
279 static typename enable_if<!__destroy_helper<_Tp>::value, void>::type
280 _S_destroy(_Alloc&, _Tp* __p)
283 template<typename _Alloc2>
284 struct __maxsize_helper
286 template<typename _Alloc3,
287 typename = decltype(std::declval<_Alloc3*>()->max_size())>
288 static true_type __test(int);
291 static false_type __test(...);
293 typedef decltype(__test<_Alloc2>(0)) type;
294 static const bool value = type::value;
297 template<typename _Alloc2>
299 enable_if<__maxsize_helper<_Alloc2>::value, size_type>::type
300 _S_max_size(_Alloc2& __a)
301 { return __a.max_size(); }
303 template<typename _Alloc2>
305 enable_if<!__maxsize_helper<_Alloc2>::value, size_type>::type
306 _S_max_size(_Alloc2&)
307 { return __gnu_cxx::__numeric_traits<size_type>::__max; }
309 template<typename _Alloc2>
310 struct __select_helper
312 template<typename _Alloc3, typename
313 = decltype(std::declval<_Alloc3*>()
314 ->select_on_container_copy_construction())>
315 static true_type __test(int);
318 static false_type __test(...);
320 typedef decltype(__test<_Alloc2>(0)) type;
321 static const bool value = type::value;
323 template<typename _Alloc2>
325 enable_if<__select_helper<_Alloc2>::value, _Alloc2>::type
326 _S_select(_Alloc2& __a)
327 { return __a.select_on_container_copy_construction(); }
329 template<typename _Alloc2>
331 enable_if<!__select_helper<_Alloc2>::value, _Alloc2>::type
332 _S_select(_Alloc2& __a)
338 * @brief Allocate memory.
339 * @param __a An allocator.
340 * @param __n The number of objects to allocate space for.
342 * Calls @c a.allocate(n)
345 allocate(_Alloc& __a, size_type __n)
346 { return __a.allocate(__n); }
349 * @brief Allocate memory.
350 * @param __a An allocator.
351 * @param __n The number of objects to allocate space for.
352 * @param __hint Aid to locality.
353 * @return Memory of suitable size and alignment for @a n objects
354 * of type @c value_type
356 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
357 * well-formed, otherwise returns @c a.allocate(n)
360 allocate(_Alloc& __a, size_type __n, const_void_pointer __hint)
361 { return _S_allocate(__a, __n, __hint); }
364 * @brief Deallocate memory.
365 * @param __a An allocator.
366 * @param __p Pointer to the memory to deallocate.
367 * @param __n The number of objects space was allocated for.
369 * Calls <tt> a.deallocate(p, n) </tt>
371 static void deallocate(_Alloc& __a, pointer __p, size_type __n)
372 { __a.deallocate(__p, __n); }
375 * @brief Construct an object of type @a _Tp
376 * @param __a An allocator.
377 * @param __p Pointer to memory of suitable size and alignment for Tp
378 * @param __args Constructor arguments.
380 * Calls <tt> __a.construct(__p, std::forward<Args>(__args)...) </tt>
381 * if that expression is well-formed, otherwise uses placement-new
382 * to construct an object of type @a _Tp at location @a __p from the
383 * arguments @a __args...
385 template<typename _Tp, typename... _Args>
386 static void construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
387 { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
390 * @brief Destroy an object of type @a _Tp
391 * @param __a An allocator.
392 * @param __p Pointer to the object to destroy
394 * Calls @c __a.destroy(__p) if that expression is well-formed,
395 * otherwise calls @c __p->~_Tp()
398 static void destroy(_Alloc& __a, _Tp* __p)
399 { _S_destroy(__a, __p); }
402 * @brief The maximum supported allocation size
403 * @param __a An allocator.
404 * @return @c __a.max_size() or @c numeric_limits<size_type>::max()
406 * Returns @c __a.max_size() if that expression is well-formed,
407 * otherwise returns @c numeric_limits<size_type>::max()
409 static size_type max_size(const _Alloc& __a)
410 { return _S_max_size(__a); }
413 * @brief Obtain an allocator to use when copying a container.
414 * @param __rhs An allocator.
415 * @return @c __rhs.select_on_container_copy_construction() or @a __rhs
417 * Returns @c __rhs.select_on_container_copy_construction() if that
418 * expression is well-formed, otherwise returns @a __rhs
421 select_on_container_copy_construction(const _Alloc& __rhs)
422 { return _S_select(__rhs); }
425 template<typename _Alloc>
427 __do_alloc_on_copy(_Alloc& __one, const _Alloc& __two, true_type)
430 template<typename _Alloc>
432 __do_alloc_on_copy(_Alloc&, const _Alloc&, false_type)
435 template<typename _Alloc>
436 inline void __alloc_on_copy(_Alloc& __one, const _Alloc& __two)
438 typedef allocator_traits<_Alloc> __traits;
439 typedef typename __traits::propagate_on_container_copy_assignment __pocca;
440 __do_alloc_on_copy(__one, __two, __pocca());
443 template<typename _Alloc>
444 inline _Alloc __alloc_on_copy(const _Alloc& __a)
446 typedef allocator_traits<_Alloc> __traits;
447 return __traits::select_on_container_copy_construction(__a);
450 template<typename _Alloc>
451 inline void __do_alloc_on_move(_Alloc& __one, _Alloc& __two, true_type)
452 { __one = std::move(__two); }
454 template<typename _Alloc>
455 inline void __do_alloc_on_move(_Alloc&, _Alloc&, false_type)
458 template<typename _Alloc>
459 inline void __alloc_on_move(_Alloc& __one, _Alloc& __two)
461 typedef allocator_traits<_Alloc> __traits;
462 typedef typename __traits::propagate_on_container_move_assignment __pocma;
463 __do_alloc_on_move(__one, __two, __pocma());
466 template<typename _Alloc>
467 inline void __do_alloc_on_swap(_Alloc& __one, _Alloc& __two, true_type)
473 template<typename _Alloc>
474 inline void __do_alloc_on_swap(_Alloc&, _Alloc&, false_type)
477 template<typename _Alloc>
478 inline void __alloc_on_swap(_Alloc& __one, _Alloc& __two)
480 typedef allocator_traits<_Alloc> __traits;
481 typedef typename __traits::propagate_on_container_swap __pocs;
482 __do_alloc_on_swap(__one, __two, __pocs());
485 _GLIBCXX_END_NAMESPACE_VERSION