libstdc++
allocator.h
Go to the documentation of this file.
1 // Allocators -*- C++ -*-
2 
3 // Copyright (C) 2001-2016 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 namespace std _GLIBCXX_VISIBILITY(default)
53 {
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 
56  /**
57  * @addtogroup allocators
58  * @{
59  */
60 
61  /// allocator<void> specialization.
62  template<>
63  class allocator<void>
64  {
65  public:
66  typedef size_t size_type;
67  typedef ptrdiff_t difference_type;
68  typedef void* pointer;
69  typedef const void* const_pointer;
70  typedef void value_type;
71 
72  template<typename _Tp1>
73  struct rebind
74  { typedef allocator<_Tp1> other; };
75 
76 #if __cplusplus >= 201103L
77  // _GLIBCXX_RESOLVE_LIB_DEFECTS
78  // 2103. std::allocator propagate_on_container_move_assignment
80 
81 #define __cpp_lib_allocator_is_always_equal 201411
82  typedef true_type is_always_equal;
83 #endif
84  };
85 
86  /**
87  * @brief The @a standard allocator, as per [20.4].
88  *
89  * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
90  * for further details.
91  *
92  * @tparam _Tp Type of allocated object.
93  */
94  template<typename _Tp>
95  class allocator: public __allocator_base<_Tp>
96  {
97  public:
98  typedef size_t size_type;
99  typedef ptrdiff_t difference_type;
100  typedef _Tp* pointer;
101  typedef const _Tp* const_pointer;
102  typedef _Tp& reference;
103  typedef const _Tp& const_reference;
104  typedef _Tp value_type;
105 
106  template<typename _Tp1>
107  struct rebind
108  { typedef allocator<_Tp1> other; };
109 
110 #if __cplusplus >= 201103L
111  // _GLIBCXX_RESOLVE_LIB_DEFECTS
112  // 2103. std::allocator propagate_on_container_move_assignment
114 #endif
115 
116  allocator() throw() { }
117 
118  allocator(const allocator& __a) throw()
119  : __allocator_base<_Tp>(__a) { }
120 
121  template<typename _Tp1>
122  allocator(const allocator<_Tp1>&) throw() { }
123 
124  ~allocator() throw() { }
125 
126  // Inherit everything else.
127  };
128 
129  template<typename _T1, typename _T2>
130  inline bool
131  operator==(const allocator<_T1>&, const allocator<_T2>&)
132  _GLIBCXX_USE_NOEXCEPT
133  { return true; }
134 
135  template<typename _Tp>
136  inline bool
137  operator==(const allocator<_Tp>&, const allocator<_Tp>&)
138  _GLIBCXX_USE_NOEXCEPT
139  { return true; }
140 
141  template<typename _T1, typename _T2>
142  inline bool
143  operator!=(const allocator<_T1>&, const allocator<_T2>&)
144  _GLIBCXX_USE_NOEXCEPT
145  { return false; }
146 
147  template<typename _Tp>
148  inline bool
149  operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
150  _GLIBCXX_USE_NOEXCEPT
151  { return false; }
152 
153  /// @} group allocator
154 
155  // Inhibit implicit instantiations for required instantiations,
156  // which are defined via explicit instantiations elsewhere.
157 #if _GLIBCXX_EXTERN_TEMPLATE
158  extern template class allocator<char>;
159  extern template class allocator<wchar_t>;
160 #endif
161 
162  // Undefine.
163 #undef __allocator_base
164 
165  // To implement Option 3 of DR 431.
166  template<typename _Alloc, bool = __is_empty(_Alloc)>
167  struct __alloc_swap
168  { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
169 
170  template<typename _Alloc>
171  struct __alloc_swap<_Alloc, false>
172  {
173  static void
174  _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
175  {
176  // Precondition: swappable allocators.
177  if (__one != __two)
178  swap(__one, __two);
179  }
180  };
181 
182  // Optimize for stateless allocators.
183  template<typename _Alloc, bool = __is_empty(_Alloc)>
184  struct __alloc_neq
185  {
186  static bool
187  _S_do_it(const _Alloc&, const _Alloc&)
188  { return false; }
189  };
190 
191  template<typename _Alloc>
192  struct __alloc_neq<_Alloc, false>
193  {
194  static bool
195  _S_do_it(const _Alloc& __one, const _Alloc& __two)
196  { return __one != __two; }
197  };
198 
199 #if __cplusplus >= 201103L
200  template<typename _Tp, bool
201  = __or_<is_copy_constructible<typename _Tp::value_type>,
202  is_nothrow_move_constructible<typename _Tp::value_type>>::value>
203  struct __shrink_to_fit_aux
204  { static bool _S_do_it(_Tp&) noexcept { return false; } };
205 
206  template<typename _Tp>
207  struct __shrink_to_fit_aux<_Tp, true>
208  {
209  static bool
210  _S_do_it(_Tp& __c) noexcept
211  {
212 #if __cpp_exceptions
213  try
214  {
215  _Tp(__make_move_if_noexcept_iterator(__c.begin()),
216  __make_move_if_noexcept_iterator(__c.end()),
217  __c.get_allocator()).swap(__c);
218  return true;
219  }
220  catch(...)
221  { return false; }
222 #else
223  return false;
224 #endif
225  }
226  };
227 #endif
228 
229 _GLIBCXX_END_NAMESPACE_VERSION
230 } // namespace std
231 
232 #endif
An allocator that uses global new, as per [20.4].This is precisely the allocator defined in the C++ S...
Definition: new_allocator.h:58
The standard allocator, as per [20.4].
Definition: allocator.h:95
void swap(basic_filebuf< _CharT, _Traits > &__x, basic_filebuf< _CharT, _Traits > &__y)
Swap specialization for filebufs.
Definition: fstream:1052
integral_constant
Definition: type_traits:69