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 
55 namespace 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 
209  [[__gnu__::__always_inline__]]
210  constexpr void
211  deallocate(_Tp* __p, size_t __n)
212  {
213  if (std::__is_constant_evaluated())
214  {
215  ::operator delete(__p);
216  return;
217  }
219  }
220 #endif // C++20
221 
222 #ifdef __glibcxx_allocate_at_least // C++23
223  [[nodiscard]] constexpr allocation_result<_Tp*, size_t>
224  allocate_at_least(size_t __n)
225  { return { this->allocate(__n), __n }; }
226 #endif
227 
228  friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
229  bool
230  operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
231  { return true; }
232 
233 #if __cpp_impl_three_way_comparison < 201907L
234  friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR
235  bool
236  operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
237  { return false; }
238 #endif
239 
240  // Inherit everything else.
241  };
242 
243  /** Equality comparison for std::allocator objects
244  *
245  * @return true, for all std::allocator objects.
246  * @relates std::allocator
247  */
248  template<typename _T1, typename _T2>
249  __attribute__((__always_inline__))
250  inline _GLIBCXX20_CONSTEXPR bool
252  _GLIBCXX_NOTHROW
253  { return true; }
254 
255 #if __cpp_impl_three_way_comparison < 201907L
256  template<typename _T1, typename _T2>
257  __attribute__((__always_inline__))
258  inline _GLIBCXX20_CONSTEXPR bool
259  operator!=(const allocator<_T1>&, const allocator<_T2>&)
260  _GLIBCXX_NOTHROW
261  { return false; }
262 #endif
263 
264  /// @cond undocumented
265 
266  // Invalid allocator<cv T> partial specializations.
267  // allocator_traits::rebind_alloc can be used to form a valid allocator type.
268  template<typename _Tp>
269  class allocator<const _Tp>
270  {
271  public:
272  typedef _Tp value_type;
273  allocator() { }
274  template<typename _Up> allocator(const allocator<_Up>&) { }
275  };
276 
277  template<typename _Tp>
278  class allocator<volatile _Tp>
279  {
280  public:
281  typedef _Tp value_type;
282  allocator() { }
283  template<typename _Up> allocator(const allocator<_Up>&) { }
284  };
285 
286  template<typename _Tp>
287  class allocator<const volatile _Tp>
288  {
289  public:
290  typedef _Tp value_type;
291  allocator() { }
292  template<typename _Up> allocator(const allocator<_Up>&) { }
293  };
294  /// @endcond
295 
296  /// @} group allocator
297 
298  // Inhibit implicit instantiations for required instantiations,
299  // which are defined via explicit instantiations elsewhere.
300 #if _GLIBCXX_EXTERN_TEMPLATE
301  extern template class allocator<char>;
302  extern template class allocator<wchar_t>;
303 #endif
304 
305  // Undefine.
306 #undef __allocator_base
307 
308 _GLIBCXX_END_NAMESPACE_VERSION
309 } // namespace std
310 
311 #pragma GCC diagnostic pop
312 #endif
requires requires
Definition: complex:1948
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:119
constexpr bool operator==(const allocator< _T1 > &, const allocator< _T2 > &) noexcept
Definition: allocator.h:251
ISO C++ entities toplevel namespace is std.
The standard allocator, as per C++03 [20.4.1].
Definition: allocator.h:134
An allocator that uses global new, as per C++03 [20.4.1].