]>
Commit | Line | Data |
---|---|---|
1ff9402d BK |
1 | // Allocators -*- C++ -*- |
2 | ||
a5544970 | 3 | // Copyright (C) 2001-2019 Free Software Foundation, Inc. |
1ff9402d BK |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
1ff9402d BK |
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 | ||
748086b7 JJ |
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. | |
1ff9402d | 19 | |
748086b7 JJ |
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/>. | |
1ff9402d BK |
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 | ||
f910786b | 38 | /** @file bits/allocator.h |
1ff9402d | 39 | * This is an internal header file, included by other library headers. |
f910786b | 40 | * Do not attempt to use it directly. @headername{memory} |
1ff9402d BK |
41 | */ |
42 | ||
1ff9402d BK |
43 | #ifndef _ALLOCATOR_H |
44 | #define _ALLOCATOR_H 1 | |
45 | ||
cd1e6665 BK |
46 | #include <bits/c++allocator.h> // Define the base class to std::allocator. |
47 | #include <bits/memoryfwd.h> | |
1b5dc776 JW |
48 | #if __cplusplus >= 201103L |
49 | #include <type_traits> | |
50 | #endif | |
1ff9402d | 51 | |
068c8ac1 | 52 | #define __cpp_lib_incomplete_container_elements 201505 |
46942c81 JW |
53 | #if __cplusplus >= 201103L |
54 | # define __cpp_lib_allocator_is_always_equal 201411 | |
55 | #endif | |
068c8ac1 | 56 | |
12ffa228 BK |
57 | namespace std _GLIBCXX_VISIBILITY(default) |
58 | { | |
59 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 60 | |
5b9daa7e | 61 | /** |
cd1e6665 BK |
62 | * @addtogroup allocators |
63 | * @{ | |
5b9daa7e BK |
64 | */ |
65 | ||
0aa06b18 | 66 | /// allocator<void> specialization. |
ba9d552e BK |
67 | template<> |
68 | class allocator<void> | |
1ff9402d | 69 | { |
ba9d552e BK |
70 | public: |
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; | |
76 | ||
77 | template<typename _Tp1> | |
88b3e631 JW |
78 | struct rebind |
79 | { typedef allocator<_Tp1> other; }; | |
1b5dc776 JW |
80 | |
81 | #if __cplusplus >= 201103L | |
82 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
83 | // 2103. std::allocator propagate_on_container_move_assignment | |
84 | typedef true_type propagate_on_container_move_assignment; | |
a2b5fdcb JW |
85 | |
86 | typedef true_type is_always_equal; | |
1d6360c8 JW |
87 | |
88 | template<typename _Up, typename... _Args> | |
88b3e631 JW |
89 | void |
90 | construct(_Up* __p, _Args&&... __args) | |
0f317ef7 MG |
91 | noexcept(noexcept(::new((void *)__p) |
92 | _Up(std::forward<_Args>(__args)...))) | |
1d6360c8 JW |
93 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } |
94 | ||
95 | template<typename _Up> | |
88b3e631 | 96 | void |
0f317ef7 MG |
97 | destroy(_Up* __p) |
98 | noexcept(noexcept(__p->~_Up())) | |
99 | { __p->~_Up(); } | |
1b5dc776 | 100 | #endif |
1ff9402d | 101 | }; |
1ff9402d | 102 | |
0aa06b18 | 103 | /** |
2a60a9f6 | 104 | * @brief The @a standard allocator, as per [20.4]. |
0aa06b18 | 105 | * |
10d43d2f | 106 | * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator |
45ba8f9f | 107 | * for further details. |
fa754848 BK |
108 | * |
109 | * @tparam _Tp Type of allocated object. | |
0aa06b18 | 110 | */ |
1ff9402d | 111 | template<typename _Tp> |
866e4d38 | 112 | class allocator : public __allocator_base<_Tp> |
1ff9402d | 113 | { |
ba9d552e | 114 | public: |
1ff9402d BK |
115 | typedef size_t size_type; |
116 | typedef ptrdiff_t difference_type; | |
117 | typedef _Tp* pointer; | |
118 | typedef const _Tp* const_pointer; | |
119 | typedef _Tp& reference; | |
120 | typedef const _Tp& const_reference; | |
121 | typedef _Tp value_type; | |
122 | ||
123 | template<typename _Tp1> | |
88b3e631 JW |
124 | struct rebind |
125 | { typedef allocator<_Tp1> other; }; | |
1ff9402d | 126 | |
1b5dc776 JW |
127 | #if __cplusplus >= 201103L |
128 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
129 | // 2103. std::allocator propagate_on_container_move_assignment | |
130 | typedef true_type propagate_on_container_move_assignment; | |
46942c81 JW |
131 | |
132 | typedef true_type is_always_equal; | |
1b5dc776 JW |
133 | #endif |
134 | ||
3be9ded2 JW |
135 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
136 | // 3035. std::allocator's constructors should be constexpr | |
137 | _GLIBCXX20_CONSTEXPR | |
138 | allocator() _GLIBCXX_NOTHROW { } | |
1ff9402d | 139 | |
3be9ded2 JW |
140 | _GLIBCXX20_CONSTEXPR |
141 | allocator(const allocator& __a) _GLIBCXX_NOTHROW | |
fa754848 | 142 | : __allocator_base<_Tp>(__a) { } |
3be9ded2 | 143 | |
f07c2237 JM |
144 | #if __cplusplus >= 201103L |
145 | // Avoid implicit deprecation. | |
146 | allocator& operator=(const allocator&) = default; | |
147 | #endif | |
1ff9402d BK |
148 | |
149 | template<typename _Tp1> | |
3be9ded2 JW |
150 | _GLIBCXX20_CONSTEXPR |
151 | allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { } | |
1ff9402d | 152 | |
3be9ded2 | 153 | ~allocator() _GLIBCXX_NOTHROW { } |
1ff9402d | 154 | |
c7790bdb JW |
155 | friend bool |
156 | operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW | |
157 | { return true; } | |
158 | ||
159 | friend bool | |
160 | operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW | |
161 | { return false; } | |
162 | ||
ba9d552e | 163 | // Inherit everything else. |
1ff9402d BK |
164 | }; |
165 | ||
1ff9402d BK |
166 | template<typename _T1, typename _T2> |
167 | inline bool | |
168 | operator==(const allocator<_T1>&, const allocator<_T2>&) | |
3be9ded2 | 169 | _GLIBCXX_NOTHROW |
1ff9402d BK |
170 | { return true; } |
171 | ||
172 | template<typename _T1, typename _T2> | |
173 | inline bool | |
174 | operator!=(const allocator<_T1>&, const allocator<_T2>&) | |
3be9ded2 | 175 | _GLIBCXX_NOTHROW |
1ff9402d BK |
176 | { return false; } |
177 | ||
866e4d38 JW |
178 | // Invalid allocator<cv T> partial specializations. |
179 | // allocator_traits::rebind_alloc can be used to form a valid allocator type. | |
180 | template<typename _Tp> | |
181 | class allocator<const _Tp> | |
182 | { | |
183 | public: | |
184 | typedef _Tp value_type; | |
185 | template<typename _Up> allocator(const allocator<_Up>&) { } | |
186 | }; | |
187 | ||
188 | template<typename _Tp> | |
189 | class allocator<volatile _Tp> | |
190 | { | |
191 | public: | |
192 | typedef _Tp value_type; | |
193 | template<typename _Up> allocator(const allocator<_Up>&) { } | |
194 | }; | |
195 | ||
196 | template<typename _Tp> | |
197 | class allocator<const volatile _Tp> | |
198 | { | |
199 | public: | |
200 | typedef _Tp value_type; | |
201 | template<typename _Up> allocator(const allocator<_Up>&) { } | |
202 | }; | |
203 | ||
cd1e6665 | 204 | /// @} group allocator |
e8eb60bd | 205 | |
1ff9402d BK |
206 | // Inhibit implicit instantiations for required instantiations, |
207 | // which are defined via explicit instantiations elsewhere. | |
3d7c150e | 208 | #if _GLIBCXX_EXTERN_TEMPLATE |
1ff9402d BK |
209 | extern template class allocator<char>; |
210 | extern template class allocator<wchar_t>; | |
211 | #endif | |
ba9d552e BK |
212 | |
213 | // Undefine. | |
fa754848 | 214 | #undef __allocator_base |
3cbc7af0 | 215 | |
f7ace77f | 216 | // To implement Option 3 of DR 431. |
cb68ec50 | 217 | template<typename _Alloc, bool = __is_empty(_Alloc)> |
f7ace77f | 218 | struct __alloc_swap |
d15ac9d9 | 219 | { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } }; |
f7ace77f PC |
220 | |
221 | template<typename _Alloc> | |
222 | struct __alloc_swap<_Alloc, false> | |
223 | { | |
224 | static void | |
d15ac9d9 | 225 | _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT |
f7ace77f PC |
226 | { |
227 | // Precondition: swappable allocators. | |
228 | if (__one != __two) | |
229 | swap(__one, __two); | |
230 | } | |
231 | }; | |
232 | ||
67202da4 PC |
233 | // Optimize for stateless allocators. |
234 | template<typename _Alloc, bool = __is_empty(_Alloc)> | |
235 | struct __alloc_neq | |
236 | { | |
237 | static bool | |
238 | _S_do_it(const _Alloc&, const _Alloc&) | |
239 | { return false; } | |
240 | }; | |
241 | ||
242 | template<typename _Alloc> | |
243 | struct __alloc_neq<_Alloc, false> | |
244 | { | |
245 | static bool | |
246 | _S_do_it(const _Alloc& __one, const _Alloc& __two) | |
247 | { return __one != __two; } | |
248 | }; | |
249 | ||
734f5023 | 250 | #if __cplusplus >= 201103L |
8a752dfe FD |
251 | template<typename _Tp, bool |
252 | = __or_<is_copy_constructible<typename _Tp::value_type>, | |
253 | is_nothrow_move_constructible<typename _Tp::value_type>>::value> | |
254 | struct __shrink_to_fit_aux | |
d15ac9d9 | 255 | { static bool _S_do_it(_Tp&) noexcept { return false; } }; |
8a752dfe | 256 | |
aa2b7414 | 257 | template<typename _Tp> |
8a752dfe | 258 | struct __shrink_to_fit_aux<_Tp, true> |
aa2b7414 | 259 | { |
8a752dfe | 260 | static bool |
d15ac9d9 | 261 | _S_do_it(_Tp& __c) noexcept |
8a752dfe | 262 | { |
99f04955 JW |
263 | #if __cpp_exceptions |
264 | try | |
8a752dfe FD |
265 | { |
266 | _Tp(__make_move_if_noexcept_iterator(__c.begin()), | |
5ed56cbb JW |
267 | __make_move_if_noexcept_iterator(__c.end()), |
268 | __c.get_allocator()).swap(__c); | |
8a752dfe FD |
269 | return true; |
270 | } | |
99f04955 | 271 | catch(...) |
8a752dfe | 272 | { return false; } |
99f04955 JW |
273 | #else |
274 | return false; | |
275 | #endif | |
8a752dfe FD |
276 | } |
277 | }; | |
79667f82 PC |
278 | #endif |
279 | ||
12ffa228 | 280 | _GLIBCXX_END_NAMESPACE_VERSION |
ed4f96af | 281 | } // namespace std |
1ff9402d BK |
282 | |
283 | #endif |