1// Allocators -*- C++ -*-
2
3// Copyright (C) 2001-2019 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
52#define __cpp_lib_incomplete_container_elements 201505
53#if __cplusplus >= 201103L
54# define __cpp_lib_allocator_is_always_equal 201411
55#endif
56
57namespace std _GLIBCXX_VISIBILITY(default)
58{
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
60
61 /**
62 * @addtogroup allocators
63 * @{
64 */
65
66 /// allocator<void> specialization.
67 template<>
68 class allocator<void>
69 {
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>
78 struct rebind
79 { typedef allocator<_Tp1> other; };
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;
85
86 typedef true_type is_always_equal;
87
88 template<typename _Up, typename... _Args>
89 void
90 construct(_Up* __p, _Args&&... __args)
91 noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
92 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
93
94 template<typename _Up>
95 void
96 destroy(_Up* __p)
97 noexcept(std::is_nothrow_destructible<_Up>::value)
98 { __p->~_Up(); }
99#endif
100 };
101
102 /**
103 * @brief The @a standard allocator, as per [20.4].
104 *
105 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
106 * for further details.
107 *
108 * @tparam _Tp Type of allocated object.
109 */
110 template<typename _Tp>
111 class allocator : public __allocator_base<_Tp>
112 {
113 public:
114 typedef size_t size_type;
115 typedef ptrdiff_t difference_type;
116 typedef _Tp* pointer;
117 typedef const _Tp* const_pointer;
118 typedef _Tp& reference;
119 typedef const _Tp& const_reference;
120 typedef _Tp value_type;
121
122 template<typename _Tp1>
123 struct rebind
124 { typedef allocator<_Tp1> other; };
125
126#if __cplusplus >= 201103L
127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
128 // 2103. std::allocator propagate_on_container_move_assignment
129 typedef true_type propagate_on_container_move_assignment;
130
131 typedef true_type is_always_equal;
132#endif
133
134 // _GLIBCXX_RESOLVE_LIB_DEFECTS
135 // 3035. std::allocator's constructors should be constexpr
136 _GLIBCXX20_CONSTEXPR
137 allocator() _GLIBCXX_NOTHROW { }
138
139 _GLIBCXX20_CONSTEXPR
140 allocator(const allocator& __a) _GLIBCXX_NOTHROW
141 : __allocator_base<_Tp>(__a) { }
142
143#if __cplusplus >= 201103L
144 // Avoid implicit deprecation.
145 allocator& operator=(const allocator&) = default;
146#endif
147
148 template<typename _Tp1>
149 _GLIBCXX20_CONSTEXPR
150 allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
151
152 ~allocator() _GLIBCXX_NOTHROW { }
153
154 friend bool
155 operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
156 { return true; }
157
158 friend bool
159 operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
160 { return false; }
161
162 // Inherit everything else.
163 };
164
165 template<typename _T1, typename _T2>
166 inline bool
167 operator==(const allocator<_T1>&, const allocator<_T2>&)
168 _GLIBCXX_NOTHROW
169 { return true; }
170
171 template<typename _T1, typename _T2>
172 inline bool
173 operator!=(const allocator<_T1>&, const allocator<_T2>&)
174 _GLIBCXX_NOTHROW
175 { return false; }
176
177 // Invalid allocator<cv T> partial specializations.
178 // allocator_traits::rebind_alloc can be used to form a valid allocator type.
179 template<typename _Tp>
180 class allocator<const _Tp>
181 {
182 public:
183 typedef _Tp value_type;
184 template<typename _Up> allocator(const allocator<_Up>&) { }
185 };
186
187 template<typename _Tp>
188 class allocator<volatile _Tp>
189 {
190 public:
191 typedef _Tp value_type;
192 template<typename _Up> allocator(const allocator<_Up>&) { }
193 };
194
195 template<typename _Tp>
196 class allocator<const volatile _Tp>
197 {
198 public:
199 typedef _Tp value_type;
200 template<typename _Up> allocator(const allocator<_Up>&) { }
201 };
202
203 /// @} group allocator
204
205 // Inhibit implicit instantiations for required instantiations,
206 // which are defined via explicit instantiations elsewhere.
207#if _GLIBCXX_EXTERN_TEMPLATE
208 extern template class allocator<char>;
209 extern template class allocator<wchar_t>;
210#endif
211
212 // Undefine.
213#undef __allocator_base
214
215 // To implement Option 3 of DR 431.
216 template<typename _Alloc, bool = __is_empty(_Alloc)>
217 struct __alloc_swap
218 { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
219
220 template<typename _Alloc>
221 struct __alloc_swap<_Alloc, false>
222 {
223 static void
224 _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
225 {
226 // Precondition: swappable allocators.
227 if (__one != __two)
228 swap(__one, __two);
229 }
230 };
231
232 // Optimize for stateless allocators.
233 template<typename _Alloc, bool = __is_empty(_Alloc)>
234 struct __alloc_neq
235 {
236 static bool
237 _S_do_it(const _Alloc&, const _Alloc&)
238 { return false; }
239 };
240
241 template<typename _Alloc>
242 struct __alloc_neq<_Alloc, false>
243 {
244 static bool
245 _S_do_it(const _Alloc& __one, const _Alloc& __two)
246 { return __one != __two; }
247 };
248
249#if __cplusplus >= 201103L
250 template<typename _Tp, bool
251 = __or_<is_copy_constructible<typename _Tp::value_type>,
252 is_nothrow_move_constructible<typename _Tp::value_type>>::value>
253 struct __shrink_to_fit_aux
254 { static bool _S_do_it(_Tp&) noexcept { return false; } };
255
256 template<typename _Tp>
257 struct __shrink_to_fit_aux<_Tp, true>
258 {
259 static bool
260 _S_do_it(_Tp& __c) noexcept
261 {
262#if __cpp_exceptions
263 try
264 {
265 _Tp(__make_move_if_noexcept_iterator(__c.begin()),
266 __make_move_if_noexcept_iterator(__c.end()),
267 __c.get_allocator()).swap(__c);
268 return true;
269 }
270 catch(...)
271 { return false; }
272#else
273 return false;
274#endif
275 }
276 };
277#endif
278
279_GLIBCXX_END_NAMESPACE_VERSION
280} // namespace std
281
282#endif
283