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