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