1 // Allocators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 * Copyright (c) 1996-1997
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 /** @file bits/allocator.h
40 * This is an internal header file, included by other library headers.
41 * Do not attempt to use it directly. @headername{memory}
45 #define _ALLOCATOR_H 1
47 // Define the base class to std::allocator.
48 #include <bits/c++allocator.h>
50 #ifdef __GXX_EXPERIMENTAL_CXX0X__
51 #include <bits/ptr_traits.h>
52 #include <type_traits> // For _GLIBCXX_HAS_NESTED_TYPE
56 namespace std _GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 * @defgroup allocators Allocators
64 * Classes encapsulating memory operations.
67 template<typename _Tp>
70 /// allocator<void> specialization.
75 typedef size_t size_type;
76 typedef ptrdiff_t difference_type;
77 typedef void* pointer;
78 typedef const void* const_pointer;
79 typedef void value_type;
81 template<typename _Tp1>
83 { typedef allocator<_Tp1> other; };
87 * @brief The @a standard allocator, as per [20.4].
90 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
91 * for further details.
93 template<typename _Tp>
94 class allocator: public __glibcxx_base_allocator<_Tp>
97 typedef size_t size_type;
98 typedef ptrdiff_t difference_type;
100 typedef const _Tp* const_pointer;
101 typedef _Tp& reference;
102 typedef const _Tp& const_reference;
103 typedef _Tp value_type;
105 template<typename _Tp1>
107 { typedef allocator<_Tp1> other; };
109 allocator() throw() { }
111 allocator(const allocator& __a) throw()
112 : __glibcxx_base_allocator<_Tp>(__a) { }
114 template<typename _Tp1>
115 allocator(const allocator<_Tp1>&) throw() { }
117 ~allocator() throw() { }
119 // Inherit everything else.
122 template<typename _T1, typename _T2>
124 operator==(const allocator<_T1>&, const allocator<_T2>&)
127 template<typename _Tp>
129 operator==(const allocator<_Tp>&, const allocator<_Tp>&)
132 template<typename _T1, typename _T2>
134 operator!=(const allocator<_T1>&, const allocator<_T2>&)
137 template<typename _Tp>
139 operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
142 // Inhibit implicit instantiations for required instantiations,
143 // which are defined via explicit instantiations elsewhere.
144 #if _GLIBCXX_EXTERN_TEMPLATE
145 extern template class allocator<char>;
146 extern template class allocator<wchar_t>;
150 #undef __glibcxx_base_allocator
152 // To implement Option 3 of DR 431.
153 template<typename _Alloc, bool = __is_empty(_Alloc)>
155 { static void _S_do_it(_Alloc&, _Alloc&) { } };
157 template<typename _Alloc>
158 struct __alloc_swap<_Alloc, false>
161 _S_do_it(_Alloc& __one, _Alloc& __two)
163 // Precondition: swappable allocators.
169 // Optimize for stateless allocators.
170 template<typename _Alloc, bool = __is_empty(_Alloc)>
174 _S_do_it(const _Alloc&, const _Alloc&)
178 template<typename _Alloc>
179 struct __alloc_neq<_Alloc, false>
182 _S_do_it(const _Alloc& __one, const _Alloc& __two)
183 { return __one != __two; }
186 #ifdef __GXX_EXPERIMENTAL_CXX0X__
187 // A very basic implementation for now. In general we have to wait for
188 // the availability of the infrastructure described in N2983: we should
189 // try when either T has a move constructor which cannot throw or T is
190 // CopyConstructible.
191 // NB: This code doesn't properly belong here, we should find a more
192 // suited place common to std::vector and std::deque.
193 template<typename _Tp,
194 bool = __has_trivial_copy(typename _Tp::value_type)>
195 struct __shrink_to_fit
196 { static void _S_do_it(_Tp&) { } };
198 template<typename _Tp>
199 struct __shrink_to_fit<_Tp, true>
205 { _Tp(__v).swap(__v); }
212 struct allocator_arg_t { };
214 constexpr allocator_arg_t allocator_arg = allocator_arg_t();
216 _GLIBCXX_HAS_NESTED_TYPE(allocator_type)
218 template<typename _Tp, typename _Alloc,
219 bool = __has_allocator_type<_Tp>::value>
220 struct __uses_allocator_helper
221 : public false_type { };
223 template<typename _Tp, typename _Alloc>
224 struct __uses_allocator_helper<_Tp, _Alloc, true>
225 : public integral_constant<bool, is_convertible<_Alloc,
226 typename _Tp::allocator_type>::value>
229 /// [allocator.uses.trait]
230 template<typename _Tp, typename _Alloc>
231 struct uses_allocator
232 : public integral_constant<bool,
233 __uses_allocator_helper<_Tp, _Alloc>::value>
236 template<typename _Alloc, typename _Tp>
237 class __alloctr_rebind_helper
239 template<typename _Alloc2, typename _Tp2>
240 static constexpr bool
241 _S_chk(typename _Alloc2::template rebind<_Tp2>::other*)
244 template<typename, typename>
245 static constexpr bool
250 static const bool __value = _S_chk<_Alloc, _Tp>(nullptr);
253 template<typename _Alloc, typename _Tp,
254 bool = __alloctr_rebind_helper<_Alloc, _Tp>::__value>
255 struct __alloctr_rebind;
257 template<typename _Alloc, typename _Tp>
258 struct __alloctr_rebind<_Alloc, _Tp, true>
260 typedef typename _Alloc::template rebind<_Tp>::other __type;
263 template<template<typename, typename...> class _Alloc, typename _Tp,
264 typename _Up, typename... _Args>
265 struct __alloctr_rebind<_Alloc<_Up, _Args...>, _Tp, false>
267 typedef _Alloc<_Tp, _Args...> __type;
271 * @brief Uniform interface to all allocator types.
272 * @ingroup allocators
274 template<typename _Alloc>
275 struct allocator_traits
277 /// The allocator type
278 typedef _Alloc allocator_type;
279 /// The allocated type
280 typedef typename _Alloc::value_type value_type;
282 #define _GLIBCXX_ALLOC_TR_NESTED_TYPE(_NTYPE, _ALT) \
284 template<typename _Tp> \
285 static typename _Tp::_NTYPE _S_##_NTYPE##_helper(_Tp*); \
286 static _ALT _S_##_NTYPE##_helper(...); \
287 typedef decltype(_S_##_NTYPE##_helper((_Alloc*)0)) __##_NTYPE; \
290 _GLIBCXX_ALLOC_TR_NESTED_TYPE(pointer, value_type*)
293 * @brief The allocator's pointer type.
295 * @c Alloc::pointer if that type exists, otherwise @c value_type*
297 typedef __pointer pointer;
299 // TODO: Use pointer_traits::rebind alias template.
301 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_pointer,
302 typename pointer_traits<pointer>::template __rebind<const value_type>::__type)
305 * @brief The allocator's const pointer type.
307 * @c Alloc::const_pointer if that type exists, otherwise
308 * <tt> pointer_traits<pointer>::rebind<const value_type> </tt>
310 typedef __const_pointer const_pointer;
312 _GLIBCXX_ALLOC_TR_NESTED_TYPE(void_pointer,
313 typename pointer_traits<pointer>::template __rebind<void>::__type)
316 * @brief The allocator's void pointer type.
318 * @c Alloc::void_pointer if that type exists, otherwise
319 * <tt> pointer_traits<pointer>::rebind<void> </tt>
321 typedef __void_pointer void_pointer;
323 _GLIBCXX_ALLOC_TR_NESTED_TYPE(const_void_pointer,
324 typename pointer_traits<pointer>::template __rebind<const void>::__type)
327 * @brief The allocator's const void pointer type.
329 * @c Alloc::const_void_pointer if that type exists, otherwise
330 * <tt> pointer_traits<pointer>::rebind<const void> </tt>
332 typedef __const_void_pointer const_void_pointer;
334 _GLIBCXX_ALLOC_TR_NESTED_TYPE(difference_type,
335 typename pointer_traits<pointer>::difference_type)
338 * @brief The allocator's difference type
340 * @c Alloc::difference_type if that type exists, otherwise
341 * <tt> pointer_traits<pointer>::difference_type </tt>
343 typedef __difference_type difference_type;
345 _GLIBCXX_ALLOC_TR_NESTED_TYPE(size_type,
346 typename make_unsigned<difference_type>::type)
349 * @brief The allocator's size type
351 * @c Alloc::size_type if that type exists, otherwise
352 * <tt> make_unsigned<difference_type>::type </tt>
354 typedef __size_type size_type;
356 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_copy_assignment,
360 * @brief How the allocator is propagated on copy assignment
362 * @c Alloc::propagate_on_container_copy_assignment if that type exists,
363 * otherwise @c false_type
365 typedef __propagate_on_container_copy_assignment
366 propagate_on_container_copy_assignment;
368 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_move_assignment,
372 * @brief How the allocator is propagated on move assignment
374 * @c Alloc::propagate_on_container_move_assignment if that type exists,
375 * otherwise @c false_type
377 typedef __propagate_on_container_move_assignment
378 propagate_on_container_move_assignment;
380 _GLIBCXX_ALLOC_TR_NESTED_TYPE(propagate_on_container_swap,
384 * @brief How the allocator is propagated on swap
386 * @c Alloc::propagate_on_container_swap if that type exists,
387 * otherwise @c false_type
389 typedef __propagate_on_container_swap propagate_on_container_swap;
391 #undef _GLIBCXX_ALLOC_TR_NESTED_TYPE
393 /* TODO: use template alias
394 template<typename _Tp>
395 using rebind_alloc = __alloctr_rebind<_Alloc, _Tp>::__type;
396 template<typename _Tp>
397 using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
399 template<typename _Tp>
400 struct __rebind_alloc
402 typedef typename __alloctr_rebind<_Alloc, _Tp>::__type __type;
405 template<typename _Tp>
406 struct __rebind_traits
408 typedef allocator_traits<typename __rebind_alloc<_Tp>::__type> __type;
412 template<typename _Alloc2>
413 struct __allocate_helper
415 template<typename _Alloc3,
416 typename = decltype(std::declval<_Alloc3*>()->allocate(
417 std::declval<size_type>(),
418 std::declval<const_void_pointer>()))>
419 static true_type __test(int);
422 static false_type __test(...);
424 typedef decltype(__test<_Alloc>(0)) type;
425 static const bool value = type::value;
428 template<typename _Alloc2>
430 enable_if<__allocate_helper<_Alloc2>::value, pointer>::type
431 _S_allocate(_Alloc2& __a, size_type __n, const_void_pointer __hint)
432 { return __a.allocate(__n, __hint); }
434 template<typename _Alloc2>
436 enable_if<!__allocate_helper<_Alloc2>::value, pointer>::type
437 _S_allocate(_Alloc2& __a, size_type __n, ...)
438 { return __a.allocate(__n); }
440 template<typename _Tp, typename... _Args>
441 struct __construct_helper
443 template<typename _Alloc2,
444 typename = decltype(std::declval<_Alloc2*>()->construct(
445 std::declval<_Tp*>(), std::declval<_Args>()...))>
446 static true_type __test(int);
449 static false_type __test(...);
451 typedef decltype(__test<_Alloc>(0)) type;
452 static const bool value = type::value;
455 template<typename _Tp, typename... _Args>
457 enable_if<__construct_helper<_Tp, _Args...>::value, void>::type
458 _S_construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
459 { __a.construct(__p, std::forward<_Args>(__args)...); }
461 template<typename _Tp, typename... _Args>
463 enable_if<!__construct_helper<_Tp, _Args...>::value, void>::type
464 _S_construct(_Alloc&, _Tp* __p, _Args&&... __args)
465 { ::new((void*)__p) _Tp(std::forward<_Args>(__args)...); }
467 template<typename _Tp>
468 struct __destroy_helper
470 template<typename _Alloc2,
471 typename = decltype(std::declval<_Alloc2*>()->destroy(
472 std::declval<_Tp*>()))>
473 static true_type __test(int);
476 static false_type __test(...);
478 typedef decltype(__test<_Alloc>(0)) type;
479 static const bool value = type::value;
482 template<typename _Tp>
483 static typename enable_if<__destroy_helper<_Tp>::value, void>::type
484 _S_destroy(_Alloc& __a, _Tp* __p)
485 { __a.destroy(__p); }
487 template<typename _Tp>
488 static typename enable_if<!__destroy_helper<_Tp>::value, void>::type
489 _S_destroy(_Alloc&, _Tp* __p)
492 template<typename _Alloc2>
493 struct __maxsize_helper
495 template<typename _Alloc3,
496 typename = decltype(std::declval<_Alloc3*>()->max_size())>
497 static true_type __test(int);
500 static false_type __test(...);
502 typedef decltype(__test<_Alloc2>(0)) type;
503 static const bool value = type::value;
506 template<typename _Alloc2>
508 enable_if<__maxsize_helper<_Alloc2>::value, size_type>::type
509 _S_max_size(_Alloc2& __a)
510 { return __a.max_size(); }
512 template<typename _Alloc2>
514 enable_if<!__maxsize_helper<_Alloc2>::value, size_type>::type
515 _S_max_size(_Alloc2&)
516 { return numeric_limits<size_type>::max(); }
518 template<typename _Alloc2>
519 struct __select_helper
521 template<typename _Alloc3, typename
522 = decltype(std::declval<_Alloc3*>()
523 ->select_on_container_copy_construction())>
524 static true_type __test(int);
527 static false_type __test(...);
529 typedef decltype(__test<_Alloc2>(0)) type;
530 static const bool value = type::value;
532 template<typename _Alloc2>
534 enable_if<__select_helper<_Alloc2>::value, _Alloc2>::type
535 _S_select(_Alloc2& __a)
536 { return __a.select_on_container_copy_construction(); }
538 template<typename _Alloc2>
540 enable_if<!__select_helper<_Alloc2>::value, _Alloc2>::type
541 _S_select(_Alloc2& __a)
547 * @brief Allocate memory.
548 * @param a An allocator.
549 * @param n The number of objects to allocate space for.
551 * Calls @c a.allocate(n)
554 allocate(_Alloc& __a, size_type __n)
555 { return __a.allocate(__n); }
558 * @brief Allocate memory.
559 * @param a An allocator.
560 * @param n The number of objects to allocate space for.
561 * @param hint Aid to locality.
562 * @return Memory of suitable size and alignment for @a n objects
563 * of type @c value_type
565 * Returns <tt> a.allocate(n, hint) </tt> if that expression is
566 * well-formed, otherwise returns @c a.allocate(n)
569 allocate(_Alloc& __a, size_type __n, const_void_pointer __hint)
570 { return _S_allocate(__a, __n, __hint); }
573 * @brief Deallocate memory.
574 * @param a An allocator.
575 * @param p Pointer to the memory to deallocate.
576 * @param n The number of objects space was allocated for.
578 * Calls <tt> a.deallocate(p, n) </tt>
580 static void deallocate(_Alloc& __a, pointer __p, size_type __n)
581 { __a.deallocate(__p, __n); }
584 * @brief Construct an object of type @a Tp
585 * @param a An allocator.
586 * @param p Pointer to memory of suitable size and alignment for Tp
587 * @param args Constructor arguments.
589 * Calls <tt> a.construct(p, std::forward<Args>(args)...) </tt>
590 * if that expression is well-formed, otherwise uses placement-new
591 * to construct an object of type @a Tp at location @a p from the
592 * arguments @a args...
594 template<typename _Tp, typename... _Args>
595 static void construct(_Alloc& __a, _Tp* __p, _Args&&... __args)
596 { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
599 * @brief Destroy an object of type @a Tp
600 * @param a An allocator.
601 * @param p Pointer to the object to destroy
603 * Calls @c a.destroy(p) if that expression is well-formed,
604 * otherwise calls @c p->~Tp()
607 static void destroy(_Alloc& __a, _Tp* __p)
608 { _S_destroy(__a, __p); }
611 * @brief The maximum supported allocation size
612 * @param a An allocator.
613 * @return @c a.max_size() or @c %numeric_limits<size_type>::max()
615 * Returns @c a.max_size() if that expression is well-formed,
616 * otherwise returns @c %numeric_limits<size_type>::max()
618 static size_type max_size(const _Alloc& __a)
619 { return _S_max_size(__a); }
622 * @brief Obtain an allocator to use when copying a container.
623 * @param rhs An allocator.
624 * @return @c rhs.select_on_container_copy_construction() or @a rhs
626 * Returns @c rhs.select_on_container_copy_construction() if that
627 * expression is well-formed, otherwise returns @a rhs
630 select_on_container_copy_construction(const _Alloc& __rhs)
631 { return _S_select(__rhs); }
636 _GLIBCXX_END_NAMESPACE_VERSION