libstdc++: allocator.h Source File

libstdc++

allocator.h
Go to the documentation of this file.
1// Allocators -*- C++ -*-
2
3// Copyright (C) 2001-2026 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#if __cplusplus <= 202302L
99 using is_always_equal
100 _GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
101 = true_type;
102#endif
103
104#if __cplusplus >= 202002L
105 // As noted above, these members are present for C++20 to provide the
106 // same API as the primary template, but still trivial as in pre-C++20.
107 allocator() = default;
108 ~allocator() = default;
109
110 template<typename _Up>
111 __attribute__((__always_inline__))
112 constexpr
113 allocator(const allocator<_Up>&) noexcept { }
114
115 // No allocate member because it's ill-formed by LWG 3307.
116 // No deallocate member because it would be undefined to call it
117 // with any pointer which wasn't obtained from allocate.
118#endif // C++20
119#endif // C++11
120 };
121
122 /**
123 * @brief The @a standard allocator, as per C++03 [20.4.1].
124 *
125 * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
126 * for further details.
127 *
128 * @tparam _Tp Type of allocated object.
129 *
130 * @headerfile memory
131 */
132 template<typename _Tp>
133 class allocator : public __allocator_base<_Tp>
134 {
135 public:
136 typedef _Tp value_type;
137 typedef size_t size_type;
138 typedef ptrdiff_t difference_type;
139
140#if __cplusplus <= 201703L
141 // These were removed for C++20.
142 typedef _Tp* pointer;
143 typedef const _Tp* const_pointer;
144 typedef _Tp& reference;
145 typedef const _Tp& const_reference;
146
147 template<typename _Tp1>
148 struct rebind
149 { typedef allocator<_Tp1> other; };
150#endif
151
152#if __cplusplus >= 201103L
153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
154 // 2103. std::allocator propagate_on_container_move_assignment
155 using propagate_on_container_move_assignment = true_type;
156
157#if __cplusplus <= 202302L
158 using is_always_equal
159 _GLIBCXX20_DEPRECATED_SUGGEST("std::allocator_traits::is_always_equal")
160 = true_type;
161#endif
162#endif
163
164 // _GLIBCXX_RESOLVE_LIB_DEFECTS
165 // 3035. std::allocator's constructors should be constexpr
166 __attribute__((__always_inline__))
167 _GLIBCXX20_CONSTEXPR
168 allocator() _GLIBCXX_NOTHROW { }
169
170 __attribute__((__always_inline__))
171 _GLIBCXX20_CONSTEXPR
172 allocator(const allocator& __a) _GLIBCXX_NOTHROW
173 : __allocator_base<_Tp>(__a) { }
174
175#if __cplusplus >= 201103L
176 // Avoid implicit deprecation.
177 allocator& operator=(const allocator&) = default;
178#endif
179
180 template<typename _Tp1>
181 __attribute__((__always_inline__))
182 _GLIBCXX20_CONSTEXPR
183 allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
184
185 __attribute__((__always_inline__))
186#if __cpp_constexpr_dynamic_alloc
187 constexpr
188#endif
189 ~allocator() _GLIBCXX_NOTHROW { }
190
191#if __cpp_constexpr_dynamic_alloc // >= C++20
192 [[nodiscard,__gnu__::__always_inline__]]
193 constexpr _Tp*
194 allocate(size_t __n)
195 {
196# if __cpp_concepts
197 if constexpr (requires { sizeof(_Tp); })
198# endif
199 if (std::__is_constant_evaluated())
200 {
201 if (__builtin_mul_overflow(__n, sizeof(_Tp), &__n))
202 std::__throw_bad_array_new_length();
203 return static_cast<_Tp*>(::operator new(__n));
204 }
205
206 return __allocator_base<_Tp>::allocate(__n, 0);
207 }
208#endif
209
210#ifdef __glibcxx_allocate_at_least // C++23
211 [[nodiscard,__gnu__::__always_inline__]]
212 constexpr allocation_result<_Tp*, size_t>
213 allocate_at_least(size_t __n)
214 {
215 if consteval
216 { return { allocate(__n), __n }; }
217 else
218 {
219 if constexpr (requires
220 { __allocator_base<_Tp>::allocate_at_least(__n); })
221 return __allocator_base<_Tp>::allocate_at_least(__n);
222 else
223 return { __allocator_base<_Tp>::allocate(__n), __n };
224 }
225 }
226#endif
227
228#if __cpp_constexpr_dynamic_alloc // >= C++20
229 [[__gnu__::__always_inline__]]
230 constexpr void
231 deallocate(_Tp* __p, size_t __n)
232 {
233 if (std::__is_constant_evaluated())
234 {
235 ::operator delete(__p);
236 return;
237 }
238 __allocator_base<_Tp>::deallocate(__p, __n);
239 }
240#endif // C++20
241
242 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
243 bool
244 operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
245 { return true; }
246
247#if __cpp_impl_three_way_comparison < 201907L
248 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
249 bool
250 operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
251 { return false; }
252#endif
253
254 // Inherit everything else.
255 };
256
257 /** Equality comparison for std::allocator objects
258 *
259 * @return true, for all std::allocator objects.
260 * @relates std::allocator
261 */
262 template<typename _T1, typename _T2>
263 __attribute__((__always_inline__))
264 inline _GLIBCXX20_CONSTEXPR bool
265 operator==(const allocator<_T1>&, const allocator<_T2>&)
266 _GLIBCXX_NOTHROW
267 { return true; }
268
269#if __cpp_impl_three_way_comparison < 201907L
270 template<typename _T1, typename _T2>
271 __attribute__((__always_inline__))
272 inline _GLIBCXX20_CONSTEXPR bool
273 operator!=(const allocator<_T1>&, const allocator<_T2>&)
274 _GLIBCXX_NOTHROW
275 { return false; }
276#endif
277
278 /// @cond undocumented
279
280 // Invalid allocator<cv T> partial specializations.
281 // allocator_traits::rebind_alloc can be used to form a valid allocator type.
282 template<typename _Tp>
283 class allocator<const _Tp>
284 {
285 public:
286 typedef _Tp value_type;
287 allocator() { }
288 template<typename _Up> allocator(const allocator<_Up>&) { }
289 };
290
291 template<typename _Tp>
292 class allocator<volatile _Tp>
293 {
294 public:
295 typedef _Tp value_type;
296 allocator() { }
297 template<typename _Up> allocator(const allocator<_Up>&) { }
298 };
299
300 template<typename _Tp>
301 class allocator<const volatile _Tp>
302 {
303 public:
304 typedef _Tp value_type;
305 allocator() { }
306 template<typename _Up> allocator(const allocator<_Up>&) { }
307 };
308 /// @endcond
309
310 /// @} group allocator
311
312 // Inhibit implicit instantiations for required instantiations,
313 // which are defined via explicit instantiations elsewhere.
314#if _GLIBCXX_EXTERN_TEMPLATE
315 extern template class allocator<char>;
316 extern template class allocator<wchar_t>;
317#endif
318
319 // Undefine.
320#undef __allocator_base
321
322_GLIBCXX_END_NAMESPACE_VERSION
323} // namespace std
324
325#pragma GCC diagnostic pop
326#endif
type_traits
memoryfwd.h
c++allocator.h
std::true_type
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
std::__allocator_base
__new_allocator< _Tp > __allocator_base
An alias to the base class for std::allocator.
Definition c++allocator.h:47
std::allocator::operator==
constexpr bool operator==(const allocator< _T1 > &, const allocator< _T2 > &) noexcept
Definition allocator.h:265
std
ISO C++ entities toplevel namespace is std.
std::allocator
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
__gnu_cxx::pointer