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