libstdc++

allocator.h
Go to the documentation of this file.
1 // Allocators -*- C++ -*-
2
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 /*
26 * Copyright (c) 1996-1997
27 * Silicon Graphics Computer Systems, Inc.
28 *
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
36 */
37
38 /** @file bits/allocator.h
39 * This is an internal header file, included by other library headers.
40 * Do not attempt to use it directly. @headername{memory}
41 */
42
43 #ifndef _ALLOCATOR_H
44 #define _ALLOCATOR_H 1
45
46 // Define the base class to std::allocator.
47 #include <bits/c++allocator.h>
48 #if __cplusplus >= 201103L
49 #include <type_traits>
50 #endif
51
52 namespace std _GLIBCXX_VISIBILITY(default)
53 {
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55
56 /**
57 * @defgroup allocators Allocators
58 * @ingroup memory
59 *
60 * Classes encapsulating memory operations.
61 *
62 * @{
63 */
64
65 template<typename _Tp>
66 class allocator;
67
68 /// allocator<void> specialization.
69 template<>
70 class allocator<void>
71 {
72 public:
73 typedef size_t size_type;
74 typedef ptrdiff_t difference_type;
75 typedef void* pointer;
76 typedef const void* const_pointer;
77 typedef void value_type;
78
79 template<typename _Tp1>
80 struct rebind
81 { typedef allocator<_Tp1> other; };
82
83 #if __cplusplus >= 201103L
84 // _GLIBCXX_RESOLVE_LIB_DEFECTS
85 // 2103. std::allocator propagate_on_container_move_assignment
86 typedef true_type propagate_on_container_move_assignment;
87 #endif
88 };
89
90 /**
91 * @brief The @a standard allocator, as per [20.4].
92 *
93 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
94 * for further details.
95 *
96 * @tparam _Tp Type of allocated object.
97 */
98 template<typename _Tp>
99 class allocator: public __allocator_base<_Tp>
100 {
101 public:
102 typedef size_t size_type;
103 typedef ptrdiff_t difference_type;
104 typedef _Tp* pointer;
105 typedef const _Tp* const_pointer;
106 typedef _Tp& reference;
107 typedef const _Tp& const_reference;
108 typedef _Tp value_type;
109
110 template<typename _Tp1>
111 struct rebind
112 { typedef allocator<_Tp1> other; };
113
114 #if __cplusplus >= 201103L
115 // _GLIBCXX_RESOLVE_LIB_DEFECTS
116 // 2103. std::allocator propagate_on_container_move_assignment
117 typedef true_type propagate_on_container_move_assignment;
118 #endif
119
120 allocator() throw() { }
121
122 allocator(const allocator& __a) throw()
123 : __allocator_base<_Tp>(__a) { }
124
125 template<typename _Tp1>
126 allocator(const allocator<_Tp1>&) throw() { }
127
128 ~allocator() throw() { }
129
130 // Inherit everything else.
131 };
132
133 template<typename _T1, typename _T2>
134 inline bool
135 operator==(const allocator<_T1>&, const allocator<_T2>&)
136 { return true; }
137
138 template<typename _Tp>
139 inline bool
140 operator==(const allocator<_Tp>&, const allocator<_Tp>&)
141 { return true; }
142
143 template<typename _T1, typename _T2>
144 inline bool
145 operator!=(const allocator<_T1>&, const allocator<_T2>&)
146 { return false; }
147
148 template<typename _Tp>
149 inline bool
150 operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
151 { return false; }
152
153 /// Declare uses_allocator so it can be specialized in <queue> etc.
154 template<typename, typename>
155 struct uses_allocator;
156
157 /**
158 * @}
159 */
160
161 // Inhibit implicit instantiations for required instantiations,
162 // which are defined via explicit instantiations elsewhere.
163 #if _GLIBCXX_EXTERN_TEMPLATE
164 extern template class allocator<char>;
165 extern template class allocator<wchar_t>;
166 #endif
167
168 // Undefine.
169 #undef __allocator_base
170
171 // To implement Option 3 of DR 431.
172 template<typename _Alloc, bool = __is_empty(_Alloc)>
173 struct __alloc_swap
174 { static void _S_do_it(_Alloc&, _Alloc&) { } };
175
176 template<typename _Alloc>
177 struct __alloc_swap<_Alloc, false>
178 {
179 static void
180 _S_do_it(_Alloc& __one, _Alloc& __two)
181 {
182 // Precondition: swappable allocators.
183 if (__one != __two)
184 swap(__one, __two);
185 }
186 };
187
188 // Optimize for stateless allocators.
189 template<typename _Alloc, bool = __is_empty(_Alloc)>
190 struct __alloc_neq
191 {
192 static bool
193 _S_do_it(const _Alloc&, const _Alloc&)
194 { return false; }
195 };
196
197 template<typename _Alloc>
198 struct __alloc_neq<_Alloc, false>
199 {
200 static bool
201 _S_do_it(const _Alloc& __one, const _Alloc& __two)
202 { return __one != __two; }
203 };
204
205 #if __cplusplus >= 201103L
206 template<typename _Tp, bool
207 = __or_<is_copy_constructible<typename _Tp::value_type>,
208 is_nothrow_move_constructible<typename _Tp::value_type>>::value>
209 struct __shrink_to_fit_aux
210 { static bool _S_do_it(_Tp&) { return false; } };
211
212 template<typename _Tp>
213 struct __shrink_to_fit_aux<_Tp, true>
214 {
215 static bool
216 _S_do_it(_Tp& __c)
217 {
218 __try
219 {
220 _Tp(__make_move_if_noexcept_iterator(__c.begin()),
221 __make_move_if_noexcept_iterator(__c.end()),
222 __c.get_allocator()).swap(__c);
223 return true;
224 }
225 __catch(...)
226 { return false; }
227 }
228 };
229 #endif
230
231 _GLIBCXX_END_NAMESPACE_VERSION
232 } // namespace std
233
234 #endif