libstdc++
allocator.h
Go to the documentation of this file.
1// Allocators -*- C++ -*-
2
3// Copyright (C) 2001-2024 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#pragma GCC diagnostic push
53#pragma GCC diagnostic ignored "-Wc++11-extensions"
54
55namespace std _GLIBCXX_VISIBILITY(default)
56{
57_GLIBCXX_BEGIN_NAMESPACE_VERSION
58
59 /**
60 * @addtogroup allocators
61 * @{
62 */
63
64 // Since C++20 the primary template should be used for allocator<void>,
65 // but then it would have a non-trivial default ctor and dtor for C++20,
66 // but trivial for C++98-17, which would be an ABI incompatibility between
67 // different standard dialects. So C++20 still uses the allocator<void>
68 // explicit specialization, with the historical ABI properties, but with
69 // the same members that are present in the primary template.
70
71 /** std::allocator<void> specialization.
72 *
73 * @headerfile memory
74 */
75 template<>
76 class allocator<void>
77 {
78 public:
79 typedef void value_type;
80 typedef size_t size_type;
81 typedef ptrdiff_t difference_type;
82
83#if __cplusplus <= 201703L
84 // These were removed for C++20, allocator_traits does the right thing.
85 typedef void* pointer;
86 typedef const void* const_pointer;
87
88 template<typename _Tp1>
89 struct rebind
90 { typedef allocator<_Tp1> other; };
91#endif
92
93#if __cplusplus >= 201103L
94 // _GLIBCXX_RESOLVE_LIB_DEFECTS
95 // 2103. std::allocator propagate_on_container_move_assignment
96 using propagate_on_container_move_assignment = true_type;
97
98 using is_always_equal
99 _GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
100 = true_type;
101
102#if __cplusplus >= 202002L
103 // As noted above, these members are present for C++20 to provide the
104 // same API as the primary template, but still trivial as in pre-C++20.
105 allocator() = default;
106 ~allocator() = default;
107
108 template<typename _Up>
109 __attribute__((__always_inline__))
110 constexpr
111 allocator(const allocator<_Up>&) noexcept { }
112
113 // No allocate member because it's ill-formed by LWG 3307.
114 // No deallocate member because it would be undefined to call it
115 // with any pointer which wasn't obtained from allocate.
116#endif // C++20
117#endif // C++11
118 };
119
120 /**
121 * @brief The @a standard allocator, as per C++03 [20.4.1].
122 *
123 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
124 * for further details.
125 *
126 * @tparam _Tp Type of allocated object.
127 *
128 * @headerfile memory
129 */
130 template<typename _Tp>
131 class allocator : public __allocator_base<_Tp>
132 {
133 public:
134 typedef _Tp value_type;
135 typedef size_t size_type;
136 typedef ptrdiff_t difference_type;
137
138#if __cplusplus <= 201703L
139 // These were removed for C++20.
140 typedef _Tp* pointer;
141 typedef const _Tp* const_pointer;
142 typedef _Tp& reference;
143 typedef const _Tp& const_reference;
144
145 template<typename _Tp1>
146 struct rebind
147 { typedef allocator<_Tp1> other; };
148#endif
149
150#if __cplusplus >= 201103L
151 // _GLIBCXX_RESOLVE_LIB_DEFECTS
152 // 2103. std::allocator propagate_on_container_move_assignment
153 using propagate_on_container_move_assignment = true_type;
154
155 using is_always_equal
156 _GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
157 = true_type;
158#endif
159
160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
161 // 3035. std::allocator's constructors should be constexpr
162 __attribute__((__always_inline__))
163 _GLIBCXX20_CONSTEXPR
164 allocator() _GLIBCXX_NOTHROW { }
165
166 __attribute__((__always_inline__))
167 _GLIBCXX20_CONSTEXPR
168 allocator(const allocator& __a) _GLIBCXX_NOTHROW
169 : __allocator_base<_Tp>(__a) { }
170
171#if __cplusplus >= 201103L
172 // Avoid implicit deprecation.
173 allocator& operator=(const allocator&) = default;
174#endif
175
176 template<typename _Tp1>
177 __attribute__((__always_inline__))
178 _GLIBCXX20_CONSTEXPR
179 allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
180
181 __attribute__((__always_inline__))
182#if __cpp_constexpr_dynamic_alloc
183 constexpr
184#endif
185 ~allocator() _GLIBCXX_NOTHROW { }
186
187#if __cplusplus > 201703L
188 [[nodiscard,__gnu__::__always_inline__]]
189 constexpr _Tp*
190 allocate(size_t __n)
191 {
192 if (std::__is_constant_evaluated())
193 {
194 if (__builtin_mul_overflow(__n, sizeof(_Tp), &__n))
195 std::__throw_bad_array_new_length();
196 return static_cast<_Tp*>(::operator new(__n));
197 }
198
199 return __allocator_base<_Tp>::allocate(__n, 0);
200 }
201
202 [[__gnu__::__always_inline__]]
203 constexpr void
204 deallocate(_Tp* __p, size_t __n)
205 {
206 if (std::__is_constant_evaluated())
207 {
208 ::operator delete(__p);
209 return;
210 }
211 __allocator_base<_Tp>::deallocate(__p, __n);
212 }
213#endif // C++20
214
215 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
216 bool
217 operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
218 { return true; }
219
220#if __cpp_impl_three_way_comparison < 201907L
221 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
222 bool
223 operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
224 { return false; }
225#endif
226
227 // Inherit everything else.
228 };
229
230 /** Equality comparison for std::allocator objects
231 *
232 * @return true, for all std::allocator objects.
233 * @relates std::allocator
234 */
235 template<typename _T1, typename _T2>
236 __attribute__((__always_inline__))
237 inline _GLIBCXX20_CONSTEXPR bool
239 _GLIBCXX_NOTHROW
240 { return true; }
241
242#if __cpp_impl_three_way_comparison < 201907L
243 template<typename _T1, typename _T2>
244 __attribute__((__always_inline__))
245 inline _GLIBCXX20_CONSTEXPR bool
246 operator!=(const allocator<_T1>&, const allocator<_T2>&)
247 _GLIBCXX_NOTHROW
248 { return false; }
249#endif
250
251 /// @cond undocumented
252
253 // Invalid allocator<cv T> partial specializations.
254 // allocator_traits::rebind_alloc can be used to form a valid allocator type.
255 template<typename _Tp>
256 class allocator<const _Tp>
257 {
258 public:
259 typedef _Tp value_type;
260 allocator() { }
261 template<typename _Up> allocator(const allocator<_Up>&) { }
262 };
263
264 template<typename _Tp>
265 class allocator<volatile _Tp>
266 {
267 public:
268 typedef _Tp value_type;
269 allocator() { }
270 template<typename _Up> allocator(const allocator<_Up>&) { }
271 };
272
273 template<typename _Tp>
274 class allocator<const volatile _Tp>
275 {
276 public:
277 typedef _Tp value_type;
278 allocator() { }
279 template<typename _Up> allocator(const allocator<_Up>&) { }
280 };
281 /// @endcond
282
283 /// @} group allocator
284
285 // Inhibit implicit instantiations for required instantiations,
286 // which are defined via explicit instantiations elsewhere.
287#if _GLIBCXX_EXTERN_TEMPLATE
288 extern template class allocator<char>;
289 extern template class allocator<wchar_t>;
290#endif
291
292 // Undefine.
293#undef __allocator_base
294
295_GLIBCXX_END_NAMESPACE_VERSION
296} // namespace std
297
298#pragma GCC diagnostic pop
299#endif
strong_ordering operator(const error_code &__lhs, const error_code &__rhs) noexcept
Definition system_error:318
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:116
__new_allocator< _Tp > __allocator_base
An alias to the base class for std::allocator.
constexpr bool operator==(const allocator< _T1 > &, const allocator< _T2 > &) noexcept
Definition allocator.h:238
ISO C++ entities toplevel namespace is std.
The standard allocator, as per C++03 [20.4.1].
Definition memoryfwd.h:67
An allocator that uses global new, as per C++03 [20.4.1].