1 // Allocators -*- C++ -*-
2
3 // Copyright (C) 2001-2015 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 #include <bits/c++allocator.h> // Define the base class to std::allocator.
47 #include <bits/memoryfwd.h>
48 #if __cplusplus >= 201103L
49 #include <type_traits>
50 #endif
51
_GLIBCXX_VISIBILITY(default)52 namespace std _GLIBCXX_VISIBILITY(default)
53 {
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55
56 /**
57 * @addtogroup allocators
58 * @{
59 */
60
61 /// allocator<void> specialization.
62 template<>
63 class allocator<void>
64 {
65 public:
66 typedef size_t size_type;
67 typedef ptrdiff_t difference_type;
68 typedef void* pointer;
69 typedef const void* const_pointer;
70 typedef void value_type;
71
72 template<typename _Tp1>
73 struct rebind
74 { typedef allocator<_Tp1> other; };
75
76 #if __cplusplus >= 201103L
77 // _GLIBCXX_RESOLVE_LIB_DEFECTS
78 // 2103. std::allocator propagate_on_container_move_assignment
79 typedef true_type propagate_on_container_move_assignment;
80
81 template<typename _Up, typename... _Args>
82 void
83 construct(_Up* __p, _Args&&... __args)
84 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
85
86 template<typename _Up>
87 void
88 destroy(_Up* __p) { __p->~_Up(); }
89 #endif
90 };
91
92 /**
93 * @brief The @a standard allocator, as per [20.4].
94 *
95 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
96 * for further details.
97 *
98 * @tparam _Tp Type of allocated object.
99 */
100 template<typename _Tp>
101 class allocator: public __allocator_base<_Tp>
102 {
103 public:
104 typedef size_t size_type;
105 typedef ptrdiff_t difference_type;
106 typedef _Tp* pointer;
107 typedef const _Tp* const_pointer;
108 typedef _Tp& reference;
109 typedef const _Tp& const_reference;
110 typedef _Tp value_type;
111
112 template<typename _Tp1>
113 struct rebind
114 { typedef allocator<_Tp1> other; };
115
116 #if __cplusplus >= 201103L
117 // _GLIBCXX_RESOLVE_LIB_DEFECTS
118 // 2103. std::allocator propagate_on_container_move_assignment
119 typedef true_type propagate_on_container_move_assignment;
120 #endif
121
122 allocator() throw() { }
123
124 allocator(const allocator& __a) throw()
125 : __allocator_base<_Tp>(__a) { }
126
127 template<typename _Tp1>
128 allocator(const allocator<_Tp1>&) throw() { }
129
130 ~allocator() throw() { }
131
132 // Inherit everything else.
133 };
134
135 template<typename _T1, typename _T2>
136 inline bool
137 operator==(const allocator<_T1>&, const allocator<_T2>&)
138 _GLIBCXX_USE_NOEXCEPT
139 { return true; }
140
141 template<typename _Tp>
142 inline bool
143 operator==(const allocator<_Tp>&, const allocator<_Tp>&)
144 _GLIBCXX_USE_NOEXCEPT
145 { return true; }
146
147 template<typename _T1, typename _T2>
148 inline bool
149 operator!=(const allocator<_T1>&, const allocator<_T2>&)
150 _GLIBCXX_USE_NOEXCEPT
151 { return false; }
152
153 template<typename _Tp>
154 inline bool
155 operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
156 _GLIBCXX_USE_NOEXCEPT
157 { return false; }
158
159 /// @} group allocator
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&) _GLIBCXX_NOEXCEPT { } };
175
176 template<typename _Alloc>
177 struct __alloc_swap<_Alloc, false>
178 {
179 static void
180 _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
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&) noexcept { 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) noexcept
217 {
218 #if __cpp_exceptions
219 try
220 {
221 _Tp(__make_move_if_noexcept_iterator(__c.begin()),
222 __make_move_if_noexcept_iterator(__c.end()),
223 __c.get_allocator()).swap(__c);
224 return true;
225 }
226 catch(...)
227 { return false; }
228 #else
229 return false;
230 #endif
231 }
232 };
233 #endif
234
235 _GLIBCXX_END_NAMESPACE_VERSION
236 } // namespace std
237
238 #endif
239