1 // Allocators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // 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 <type_traits> // For _GLIBCXX_HAS_NESTED_TYPE
54 _GLIBCXX_BEGIN_NAMESPACE(std
)
57 * @defgroup allocators Allocators
60 * Classes encapsulating memory operations.
63 template<typename _Tp
>
66 /// allocator<void> specialization.
71 typedef size_t size_type
;
72 typedef ptrdiff_t difference_type
;
73 typedef void* pointer
;
74 typedef const void* const_pointer
;
75 typedef void value_type
;
77 template<typename _Tp1
>
79 { typedef allocator
<_Tp1
> other
; };
83 * @brief The @a standard allocator, as per [20.4].
87 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
89 template<typename _Tp
>
90 class allocator
: public __glibcxx_base_allocator
<_Tp
>
93 typedef size_t size_type
;
94 typedef ptrdiff_t difference_type
;
96 typedef const _Tp
* const_pointer
;
97 typedef _Tp
& reference
;
98 typedef const _Tp
& const_reference
;
99 typedef _Tp value_type
;
101 template<typename _Tp1
>
103 { typedef allocator
<_Tp1
> other
; };
105 allocator() throw() { }
107 allocator(const allocator
& __a
) throw()
108 : __glibcxx_base_allocator
<_Tp
>(__a
) { }
110 template<typename _Tp1
>
111 allocator(const allocator
<_Tp1
>&) throw() { }
113 ~allocator() throw() { }
115 // Inherit everything else.
118 template<typename _T1
, typename _T2
>
120 operator==(const allocator
<_T1
>&, const allocator
<_T2
>&)
123 template<typename _Tp
>
125 operator==(const allocator
<_Tp
>&, const allocator
<_Tp
>&)
128 template<typename _T1
, typename _T2
>
130 operator!=(const allocator
<_T1
>&, const allocator
<_T2
>&)
133 template<typename _Tp
>
135 operator!=(const allocator
<_Tp
>&, const allocator
<_Tp
>&)
138 // Inhibit implicit instantiations for required instantiations,
139 // which are defined via explicit instantiations elsewhere.
140 // NB: This syntax is a GNU extension.
141 #if _GLIBCXX_EXTERN_TEMPLATE
142 extern template class allocator
<char>;
143 extern template class allocator
<wchar_t>;
147 #undef __glibcxx_base_allocator
149 // To implement Option 3 of DR 431.
150 template<typename _Alloc
, bool = __is_empty(_Alloc
)>
152 { static void _S_do_it(_Alloc
&, _Alloc
&) { } };
154 template<typename _Alloc
>
155 struct __alloc_swap
<_Alloc
, false>
158 _S_do_it(_Alloc
& __one
, _Alloc
& __two
)
160 // Precondition: swappable allocators.
166 // Optimize for stateless allocators.
167 template<typename _Alloc
, bool = __is_empty(_Alloc
)>
171 _S_do_it(const _Alloc
&, const _Alloc
&)
175 template<typename _Alloc
>
176 struct __alloc_neq
<_Alloc
, false>
179 _S_do_it(const _Alloc
& __one
, const _Alloc
& __two
)
180 { return __one
!= __two
; }
183 #ifdef __GXX_EXPERIMENTAL_CXX0X__
184 // A very basic implementation for now. In general we have to wait for
185 // the availability of the infrastructure described in N2983: we should
186 // try when either T has a move constructor which cannot throw or T is
188 // NB: This code doesn't properly belong here, we should find a more
189 // suited place common to std::vector and std::deque.
190 template<typename _Tp
,
191 bool = __has_trivial_copy(typename
_Tp::value_type
)>
192 struct __shrink_to_fit
193 { static void _S_do_it(_Tp
&) { } };
195 template<typename _Tp
>
196 struct __shrink_to_fit
<_Tp
, true>
202 { _Tp(__v
).swap(__v
); }
209 struct allocator_arg_t
{ };
211 constexpr allocator_arg_t allocator_arg
= allocator_arg_t();
213 _GLIBCXX_HAS_NESTED_TYPE(allocator_type
)
215 template<typename _Tp
, typename _Alloc
,
216 bool = __has_allocator_type
<_Tp
>::value
>
217 struct __uses_allocator_helper
218 : public false_type
{ };
220 template<typename _Tp
, typename _Alloc
>
221 struct __uses_allocator_helper
<_Tp
, _Alloc
, true>
222 : public integral_constant
<bool, is_convertible
<_Alloc
,
223 typename
_Tp::allocator_type
>::value
>
226 /// [allocator.uses.trait]
227 template<typename _Tp
, typename _Alloc
>
228 struct uses_allocator
229 : public integral_constant
<bool,
230 __uses_allocator_helper
<_Tp
, _Alloc
>::value
>
235 _GLIBCXX_END_NAMESPACE