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