libstdc++
allocator.h
Go to the documentation of this file.
1 // Allocators -*- C++ -*-
2 
3 // Copyright (C) 2001-2021 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 
54 namespace std _GLIBCXX_VISIBILITY(default)
55 {
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57 
58  /**
59  * @addtogroup allocators
60  * @{
61  */
62 
63  // Since C++20 the primary template should be used for allocator<void>,
64  // but then it would have a non-trivial default ctor and dtor, which
65  // would be an ABI change. So C++20 still uses the allocator<void> explicit
66  // specialization, with the historical ABI properties, but with the same
67  // members that are present in the primary template.
68 
69 #if ! _GLIBCXX_INLINE_VERSION
70  /// allocator<void> specialization.
71  template<>
72  class allocator<void>
73  {
74  public:
75  typedef void value_type;
76  typedef size_t size_type;
77  typedef ptrdiff_t difference_type;
78 
79 #if __cplusplus <= 201703L
80  // These were removed for C++20.
81  typedef void* pointer;
82  typedef const void* const_pointer;
83 
84  template<typename _Tp1>
85  struct rebind
86  { typedef allocator<_Tp1> other; };
87 #endif
88 
89 #if __cplusplus >= 201103L
90  // _GLIBCXX_RESOLVE_LIB_DEFECTS
91  // 2103. std::allocator propagate_on_container_move_assignment
93 
94  using is_always_equal
95  _GLIBCXX20_DEPRECATED_SUGGEST("allocator_traits::is_always_equal")
96  = true_type;
97 
98 #if __cplusplus >= 202002L
99  allocator() = default;
100 
101  template<typename _Up>
102  constexpr
103  allocator(const allocator<_Up>&) noexcept { }
104 
105  // No allocate member because it's ill-formed by LWG 3307.
106  // No deallocate member because it would be undefined to call it
107  // with any pointer which wasn't obtained from allocate.
108 
109 #else // ! C++20
110  private:
111  // This uses construct and destroy in C++11/14/17 modes.
113 
114  template<typename _Up, typename... _Args>
115  void
116  construct(_Up* __p, _Args&&... __args)
118  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
119 
120  template<typename _Up>
121  void
122  destroy(_Up* __p)
124  { __p->~_Up(); }
125 #endif // C++17
126 #endif // C++11
127 
128  };
129 #endif // ! _GLIBCXX_INLINE_VERSION
130 
131  /**
132  * @brief The @a standard allocator, as per C++03 [20.4.1].
133  *
134  * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
135  * for further details.
136  *
137  * @tparam _Tp Type of allocated object.
138  */
139  template<typename _Tp>
140  class allocator : public __allocator_base<_Tp>
141  {
142  public:
143  typedef _Tp value_type;
144  typedef size_t size_type;
145  typedef ptrdiff_t difference_type;
146 
147 #if __cplusplus <= 201703L
148  // These were removed for C++20.
149  typedef _Tp* pointer;
150  typedef const _Tp* const_pointer;
151  typedef _Tp& reference;
152  typedef const _Tp& const_reference;
153 
154  template<typename _Tp1>
155  struct rebind
156  { typedef allocator<_Tp1> other; };
157 #endif
158 
159 #if __cplusplus >= 201103L
160  // _GLIBCXX_RESOLVE_LIB_DEFECTS
161  // 2103. std::allocator propagate_on_container_move_assignment
163 
164  using is_always_equal
165  _GLIBCXX20_DEPRECATED_SUGGEST("allocator_traits::is_always_equal")
166  = true_type;
167 #endif
168 
169  // _GLIBCXX_RESOLVE_LIB_DEFECTS
170  // 3035. std::allocator's constructors should be constexpr
171  _GLIBCXX20_CONSTEXPR
172  allocator() _GLIBCXX_NOTHROW { }
173 
174  _GLIBCXX20_CONSTEXPR
175  allocator(const allocator& __a) _GLIBCXX_NOTHROW
176  : __allocator_base<_Tp>(__a) { }
177 
178 #if __cplusplus >= 201103L
179  // Avoid implicit deprecation.
180  allocator& operator=(const allocator&) = default;
181 #endif
182 
183  template<typename _Tp1>
184  _GLIBCXX20_CONSTEXPR
185  allocator(const allocator<_Tp1>&) _GLIBCXX_NOTHROW { }
186 
187 #if __cpp_constexpr_dynamic_alloc
188  constexpr
189 #endif
190  ~allocator() _GLIBCXX_NOTHROW { }
191 
192 #if __cplusplus > 201703L
193  [[nodiscard,__gnu__::__always_inline__]]
194  constexpr _Tp*
195  allocate(size_t __n)
196  {
197 #ifdef __cpp_lib_is_constant_evaluated
198  if (std::is_constant_evaluated())
199  return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
200 #endif
201  return __allocator_base<_Tp>::allocate(__n, 0);
202  }
203 
204  [[__gnu__::__always_inline__]]
205  constexpr void
206  deallocate(_Tp* __p, size_t __n)
207  {
208 #ifdef __cpp_lib_is_constant_evaluated
209  if (std::is_constant_evaluated())
210  {
211  ::operator delete(__p);
212  return;
213  }
214 #endif
216  }
217 #endif // C++20
218 
219  friend _GLIBCXX20_CONSTEXPR bool
220  operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
221  { return true; }
222 
223 #if __cpp_impl_three_way_comparison < 201907L
224  friend _GLIBCXX20_CONSTEXPR bool
225  operator!=(const allocator&, const allocator&) _GLIBCXX_NOTHROW
226  { return false; }
227 #endif
228 
229  // Inherit everything else.
230  };
231 
232  template<typename _T1, typename _T2>
233  inline _GLIBCXX20_CONSTEXPR bool
234  operator==(const allocator<_T1>&, const allocator<_T2>&)
235  _GLIBCXX_NOTHROW
236  { return true; }
237 
238 #if __cpp_impl_three_way_comparison < 201907L
239  template<typename _T1, typename _T2>
240  inline _GLIBCXX20_CONSTEXPR bool
241  operator!=(const allocator<_T1>&, const allocator<_T2>&)
242  _GLIBCXX_NOTHROW
243  { return false; }
244 #endif
245 
246  // Invalid allocator<cv T> partial specializations.
247  // allocator_traits::rebind_alloc can be used to form a valid allocator type.
248  template<typename _Tp>
249  class allocator<const _Tp>
250  {
251  public:
252  typedef _Tp value_type;
253  template<typename _Up> allocator(const allocator<_Up>&) { }
254  };
255 
256  template<typename _Tp>
257  class allocator<volatile _Tp>
258  {
259  public:
260  typedef _Tp value_type;
261  template<typename _Up> allocator(const allocator<_Up>&) { }
262  };
263 
264  template<typename _Tp>
265  class allocator<const volatile _Tp>
266  {
267  public:
268  typedef _Tp value_type;
269  template<typename _Up> allocator(const allocator<_Up>&) { }
270  };
271 
272  /// @} group allocator
273 
274  // Inhibit implicit instantiations for required instantiations,
275  // which are defined via explicit instantiations elsewhere.
276 #if _GLIBCXX_EXTERN_TEMPLATE
277  extern template class allocator<char>;
278  extern template class allocator<wchar_t>;
279 #endif
280 
281  // Undefine.
282 #undef __allocator_base
283 
284  // To implement Option 3 of DR 431.
285  template<typename _Alloc, bool = __is_empty(_Alloc)>
286  struct __alloc_swap
287  { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
288 
289  template<typename _Alloc>
290  struct __alloc_swap<_Alloc, false>
291  {
292  static void
293  _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
294  {
295  // Precondition: swappable allocators.
296  if (__one != __two)
297  swap(__one, __two);
298  }
299  };
300 
301  // Optimize for stateless allocators.
302  template<typename _Alloc, bool = __is_empty(_Alloc)>
303  struct __alloc_neq
304  {
305  static bool
306  _S_do_it(const _Alloc&, const _Alloc&)
307  { return false; }
308  };
309 
310  template<typename _Alloc>
311  struct __alloc_neq<_Alloc, false>
312  {
313  static bool
314  _S_do_it(const _Alloc& __one, const _Alloc& __two)
315  { return __one != __two; }
316  };
317 
318 #if __cplusplus >= 201103L
319  template<typename _Tp, bool
320  = __or_<is_copy_constructible<typename _Tp::value_type>,
321  is_nothrow_move_constructible<typename _Tp::value_type>>::value>
322  struct __shrink_to_fit_aux
323  { static bool _S_do_it(_Tp&) noexcept { return false; } };
324 
325  template<typename _Tp>
326  struct __shrink_to_fit_aux<_Tp, true>
327  {
328  static bool
329  _S_do_it(_Tp& __c) noexcept
330  {
331 #if __cpp_exceptions
332  try
333  {
334  _Tp(__make_move_if_noexcept_iterator(__c.begin()),
335  __make_move_if_noexcept_iterator(__c.end()),
336  __c.get_allocator()).swap(__c);
337  return true;
338  }
339  catch(...)
340  { return false; }
341 #else
342  return false;
343 #endif
344  }
345  };
346 #endif
347 
348 _GLIBCXX_END_NAMESPACE_VERSION
349 } // namespace std
350 
351 #endif
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:83
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:422
ISO C++ entities toplevel namespace is std.
integral_constant
Definition: type_traits:66
is_nothrow_destructible
Definition: type_traits:938
is_nothrow_constructible
Definition: type_traits:1024
Uniform interface to all allocator types.
The standard allocator, as per C++03 [20.4.1].
Definition: allocator.h:141
An allocator that uses global new, as per C++03 [20.4.1].
Definition: new_allocator.h:56