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