libstdc++
allocator.h
Go to the documentation of this file.
1// Allocators -*- C++ -*-
2
3// Copyright (C) 2001-2020 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 void value_type;
72 typedef size_t size_type;
73 typedef ptrdiff_t difference_type;
74#if __cplusplus <= 201703L
75 typedef void* pointer;
76 typedef const void* const_pointer;
77
78 template<typename _Tp1>
79 struct rebind
80 { typedef allocator<_Tp1> other; };
81#else
82 allocator() = default;
83
84 template<typename _Up>
85 constexpr
86 allocator(const allocator<_Up>&) { }
87#endif // ! C++20
88
89#if __cplusplus >= 201103L && __cplusplus <= 201703L
90 // _GLIBCXX_RESOLVE_LIB_DEFECTS
91 // 2103. std::allocator propagate_on_container_move_assignment
93
95
96 template<typename _Up, typename... _Args>
97 void
98 construct(_Up* __p, _Args&&... __args)
99 noexcept(noexcept(::new((void *)__p)
100 _Up(std::forward<_Args>(__args)...)))
101 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
102
103 template<typename _Up>
104 void
105 destroy(_Up* __p)
106 noexcept(noexcept(__p->~_Up()))
107 { __p->~_Up(); }
108#endif // C++11 to C++17
109 };
110
111 /**
112 * @brief The @a standard allocator, as per [20.4].
113 *
114 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
115 * for further details.
116 *
117 * @tparam _Tp Type of allocated object.
118 */
119 template<typename _Tp>
120 class allocator : public __allocator_base<_Tp>
121 {
122 public:
123 typedef _Tp value_type;
124 typedef size_t size_type;
125 typedef ptrdiff_t difference_type;
126#if __cplusplus <= 201703L
127 typedef _Tp* pointer;
128 typedef const _Tp* const_pointer;
129 typedef _Tp& reference;
130 typedef const _Tp& const_reference;
131
132 template<typename _Tp1>
133 struct rebind
134 { typedef allocator<_Tp1> other; };
135#endif
136
137#if __cplusplus >= 201103L
138 // _GLIBCXX_RESOLVE_LIB_DEFECTS
139 // 2103. std::allocator propagate_on_container_move_assignment
141
143#endif
144
145 // _GLIBCXX_RESOLVE_LIB_DEFECTS
146 // 3035. std::allocator's constructors should be constexpr
147 _GLIBCXX20_CONSTEXPR
148 allocator() _GLIBCXX_NOTHROW { }
149
150 _GLIBCXX20_CONSTEXPR
151 allocator(const allocator& __a) _GLIBCXX_NOTHROW
152 : __allocator_base<_Tp>(__a) { }
153
154#if __cplusplus >= 201103L
155 // Avoid implicit deprecation.
156 allocator& operator=(const allocator&) = default;
157#endif
158
159 template<typename _Tp1>
160 _GLIBCXX20_CONSTEXPR
161 allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
162
163#if __cpp_constexpr_dynamic_alloc
164 constexpr
165#endif
166 ~allocator() _GLIBCXX_NOTHROW { }
167
168#if __cplusplus > 201703L
169 [[nodiscard,__gnu__::__always_inline__]]
170 constexpr _Tp*
171 allocate(size_t __n)
172 {
173#ifdef __cpp_lib_is_constant_evaluated
174 if (std::is_constant_evaluated())
175 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
176#endif
177 return __allocator_base<_Tp>::allocate(__n, 0);
178 }
179
180 [[__gnu__::__always_inline__]]
181 constexpr void
182 deallocate(_Tp* __p, size_t __n)
183 {
184#ifdef __cpp_lib_is_constant_evaluated
185 if (std::is_constant_evaluated())
186 {
187 ::operator delete(__p);
188 return;
189 }
190#endif
192 }
193#endif // C++20
194
195 friend _GLIBCXX20_CONSTEXPR bool
196 operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
197 { return true; }
198
199 friend _GLIBCXX20_CONSTEXPR bool
200 operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
201 { return false; }
202
203 // Inherit everything else.
204 };
205
206 template<typename _T1, typename _T2>
207 inline _GLIBCXX20_CONSTEXPR bool
208 operator==(const allocator<_T1>&, const allocator<_T2>&)
209 _GLIBCXX_NOTHROW
210 { return true; }
211
212 template<typename _T1, typename _T2>
213 inline _GLIBCXX20_CONSTEXPR bool
214 operator!=(const allocator<_T1>&, const allocator<_T2>&)
215 _GLIBCXX_NOTHROW
216 { return false; }
217
218 // Invalid allocator<cv T> partial specializations.
219 // allocator_traits::rebind_alloc can be used to form a valid allocator type.
220 template<typename _Tp>
221 class allocator<const _Tp>
222 {
223 public:
224 typedef _Tp value_type;
225 template<typename _Up> allocator(const allocator<_Up>&) { }
226 };
227
228 template<typename _Tp>
229 class allocator<volatile _Tp>
230 {
231 public:
232 typedef _Tp value_type;
233 template<typename _Up> allocator(const allocator<_Up>&) { }
234 };
235
236 template<typename _Tp>
237 class allocator<const volatile _Tp>
238 {
239 public:
240 typedef _Tp value_type;
241 template<typename _Up> allocator(const allocator<_Up>&) { }
242 };
243
244 /// @} group allocator
245
246 // Inhibit implicit instantiations for required instantiations,
247 // which are defined via explicit instantiations elsewhere.
248#if _GLIBCXX_EXTERN_TEMPLATE
249 extern template class allocator<char>;
250 extern template class allocator<wchar_t>;
251#endif
252
253 // Undefine.
254#undef __allocator_base
255
256 // To implement Option 3 of DR 431.
257 template<typename _Alloc, bool = __is_empty(_Alloc)>
258 struct __alloc_swap
259 { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
260
261 template<typename _Alloc>
262 struct __alloc_swap<_Alloc, false>
263 {
264 static void
265 _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
266 {
267 // Precondition: swappable allocators.
268 if (__one != __two)
269 swap(__one, __two);
270 }
271 };
272
273 // Optimize for stateless allocators.
274 template<typename _Alloc, bool = __is_empty(_Alloc)>
275 struct __alloc_neq
276 {
277 static bool
278 _S_do_it(const _Alloc&, const _Alloc&)
279 { return false; }
280 };
281
282 template<typename _Alloc>
283 struct __alloc_neq<_Alloc, false>
284 {
285 static bool
286 _S_do_it(const _Alloc& __one, const _Alloc& __two)
287 { return __one != __two; }
288 };
289
290#if __cplusplus >= 201103L
291 template<typename _Tp, bool
292 = __or_<is_copy_constructible<typename _Tp::value_type>,
293 is_nothrow_move_constructible<typename _Tp::value_type>>::value>
294 struct __shrink_to_fit_aux
295 { static bool _S_do_it(_Tp&) noexcept { return false; } };
296
297 template<typename _Tp>
298 struct __shrink_to_fit_aux<_Tp, true>
299 {
300 static bool
301 _S_do_it(_Tp& __c) noexcept
302 {
303#if __cpp_exceptions
304 try
305 {
306 _Tp(__make_move_if_noexcept_iterator(__c.begin()),
307 __make_move_if_noexcept_iterator(__c.end()),
308 __c.get_allocator()).swap(__c);
309 return true;
310 }
311 catch(...)
312 { return false; }
313#else
314 return false;
315#endif
316 }
317 };
318#endif
319
320_GLIBCXX_END_NAMESPACE_VERSION
321} // namespace std
322
323#endif
ISO C++ entities toplevel namespace is std.
integral_constant
Definition: type_traits:58
The standard allocator, as per [20.4].
Definition: allocator.h:121
An allocator that uses global new, as per [20.4].
Definition: new_allocator.h:56