1*38fd1498Szrj // Allocators -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj /*
26*38fd1498Szrj * Copyright (c) 1996-1997
27*38fd1498Szrj * Silicon Graphics Computer Systems, Inc.
28*38fd1498Szrj *
29*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software
30*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee,
31*38fd1498Szrj * provided that the above copyright notice appear in all copies and
32*38fd1498Szrj * that both that copyright notice and this permission notice appear
33*38fd1498Szrj * in supporting documentation. Silicon Graphics makes no
34*38fd1498Szrj * representations about the suitability of this software for any
35*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty.
36*38fd1498Szrj */
37*38fd1498Szrj
38*38fd1498Szrj /** @file bits/allocator.h
39*38fd1498Szrj * This is an internal header file, included by other library headers.
40*38fd1498Szrj * Do not attempt to use it directly. @headername{memory}
41*38fd1498Szrj */
42*38fd1498Szrj
43*38fd1498Szrj #ifndef _ALLOCATOR_H
44*38fd1498Szrj #define _ALLOCATOR_H 1
45*38fd1498Szrj
46*38fd1498Szrj #include <bits/c++allocator.h> // Define the base class to std::allocator.
47*38fd1498Szrj #include <bits/memoryfwd.h>
48*38fd1498Szrj #if __cplusplus >= 201103L
49*38fd1498Szrj #include <type_traits>
50*38fd1498Szrj #endif
51*38fd1498Szrj
52*38fd1498Szrj #define __cpp_lib_incomplete_container_elements 201505
53*38fd1498Szrj #if __cplusplus >= 201103L
54*38fd1498Szrj # define __cpp_lib_allocator_is_always_equal 201411
55*38fd1498Szrj #endif
56*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)57*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
58*38fd1498Szrj {
59*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
60*38fd1498Szrj
61*38fd1498Szrj /**
62*38fd1498Szrj * @addtogroup allocators
63*38fd1498Szrj * @{
64*38fd1498Szrj */
65*38fd1498Szrj
66*38fd1498Szrj /// allocator<void> specialization.
67*38fd1498Szrj template<>
68*38fd1498Szrj class allocator<void>
69*38fd1498Szrj {
70*38fd1498Szrj public:
71*38fd1498Szrj typedef size_t size_type;
72*38fd1498Szrj typedef ptrdiff_t difference_type;
73*38fd1498Szrj typedef void* pointer;
74*38fd1498Szrj typedef const void* const_pointer;
75*38fd1498Szrj typedef void value_type;
76*38fd1498Szrj
77*38fd1498Szrj template<typename _Tp1>
78*38fd1498Szrj struct rebind
79*38fd1498Szrj { typedef allocator<_Tp1> other; };
80*38fd1498Szrj
81*38fd1498Szrj #if __cplusplus >= 201103L
82*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS
83*38fd1498Szrj // 2103. std::allocator propagate_on_container_move_assignment
84*38fd1498Szrj typedef true_type propagate_on_container_move_assignment;
85*38fd1498Szrj
86*38fd1498Szrj typedef true_type is_always_equal;
87*38fd1498Szrj
88*38fd1498Szrj template<typename _Up, typename... _Args>
89*38fd1498Szrj void
90*38fd1498Szrj construct(_Up* __p, _Args&&... __args)
91*38fd1498Szrj { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
92*38fd1498Szrj
93*38fd1498Szrj template<typename _Up>
94*38fd1498Szrj void
95*38fd1498Szrj destroy(_Up* __p) { __p->~_Up(); }
96*38fd1498Szrj #endif
97*38fd1498Szrj };
98*38fd1498Szrj
99*38fd1498Szrj /**
100*38fd1498Szrj * @brief The @a standard allocator, as per [20.4].
101*38fd1498Szrj *
102*38fd1498Szrj * See https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#std.util.memory.allocator
103*38fd1498Szrj * for further details.
104*38fd1498Szrj *
105*38fd1498Szrj * @tparam _Tp Type of allocated object.
106*38fd1498Szrj */
107*38fd1498Szrj template<typename _Tp>
108*38fd1498Szrj class allocator : public __allocator_base<_Tp>
109*38fd1498Szrj {
110*38fd1498Szrj public:
111*38fd1498Szrj typedef size_t size_type;
112*38fd1498Szrj typedef ptrdiff_t difference_type;
113*38fd1498Szrj typedef _Tp* pointer;
114*38fd1498Szrj typedef const _Tp* const_pointer;
115*38fd1498Szrj typedef _Tp& reference;
116*38fd1498Szrj typedef const _Tp& const_reference;
117*38fd1498Szrj typedef _Tp value_type;
118*38fd1498Szrj
119*38fd1498Szrj template<typename _Tp1>
120*38fd1498Szrj struct rebind
121*38fd1498Szrj { typedef allocator<_Tp1> other; };
122*38fd1498Szrj
123*38fd1498Szrj #if __cplusplus >= 201103L
124*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS
125*38fd1498Szrj // 2103. std::allocator propagate_on_container_move_assignment
126*38fd1498Szrj typedef true_type propagate_on_container_move_assignment;
127*38fd1498Szrj
128*38fd1498Szrj typedef true_type is_always_equal;
129*38fd1498Szrj #endif
130*38fd1498Szrj
131*38fd1498Szrj allocator() throw() { }
132*38fd1498Szrj
133*38fd1498Szrj allocator(const allocator& __a) throw()
134*38fd1498Szrj : __allocator_base<_Tp>(__a) { }
135*38fd1498Szrj
136*38fd1498Szrj template<typename _Tp1>
137*38fd1498Szrj allocator(const allocator<_Tp1>&) throw() { }
138*38fd1498Szrj
139*38fd1498Szrj ~allocator() throw() { }
140*38fd1498Szrj
141*38fd1498Szrj // Inherit everything else.
142*38fd1498Szrj };
143*38fd1498Szrj
144*38fd1498Szrj template<typename _T1, typename _T2>
145*38fd1498Szrj inline bool
146*38fd1498Szrj operator==(const allocator<_T1>&, const allocator<_T2>&)
147*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT
148*38fd1498Szrj { return true; }
149*38fd1498Szrj
150*38fd1498Szrj template<typename _Tp>
151*38fd1498Szrj inline bool
152*38fd1498Szrj operator==(const allocator<_Tp>&, const allocator<_Tp>&)
153*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT
154*38fd1498Szrj { return true; }
155*38fd1498Szrj
156*38fd1498Szrj template<typename _T1, typename _T2>
157*38fd1498Szrj inline bool
158*38fd1498Szrj operator!=(const allocator<_T1>&, const allocator<_T2>&)
159*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT
160*38fd1498Szrj { return false; }
161*38fd1498Szrj
162*38fd1498Szrj template<typename _Tp>
163*38fd1498Szrj inline bool
164*38fd1498Szrj operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
165*38fd1498Szrj _GLIBCXX_USE_NOEXCEPT
166*38fd1498Szrj { return false; }
167*38fd1498Szrj
168*38fd1498Szrj // Invalid allocator<cv T> partial specializations.
169*38fd1498Szrj // allocator_traits::rebind_alloc can be used to form a valid allocator type.
170*38fd1498Szrj template<typename _Tp>
171*38fd1498Szrj class allocator<const _Tp>
172*38fd1498Szrj {
173*38fd1498Szrj public:
174*38fd1498Szrj typedef _Tp value_type;
175*38fd1498Szrj template<typename _Up> allocator(const allocator<_Up>&) { }
176*38fd1498Szrj };
177*38fd1498Szrj
178*38fd1498Szrj template<typename _Tp>
179*38fd1498Szrj class allocator<volatile _Tp>
180*38fd1498Szrj {
181*38fd1498Szrj public:
182*38fd1498Szrj typedef _Tp value_type;
183*38fd1498Szrj template<typename _Up> allocator(const allocator<_Up>&) { }
184*38fd1498Szrj };
185*38fd1498Szrj
186*38fd1498Szrj template<typename _Tp>
187*38fd1498Szrj class allocator<const volatile _Tp>
188*38fd1498Szrj {
189*38fd1498Szrj public:
190*38fd1498Szrj typedef _Tp value_type;
191*38fd1498Szrj template<typename _Up> allocator(const allocator<_Up>&) { }
192*38fd1498Szrj };
193*38fd1498Szrj
194*38fd1498Szrj /// @} group allocator
195*38fd1498Szrj
196*38fd1498Szrj // Inhibit implicit instantiations for required instantiations,
197*38fd1498Szrj // which are defined via explicit instantiations elsewhere.
198*38fd1498Szrj #if _GLIBCXX_EXTERN_TEMPLATE
199*38fd1498Szrj extern template class allocator<char>;
200*38fd1498Szrj extern template class allocator<wchar_t>;
201*38fd1498Szrj #endif
202*38fd1498Szrj
203*38fd1498Szrj // Undefine.
204*38fd1498Szrj #undef __allocator_base
205*38fd1498Szrj
206*38fd1498Szrj // To implement Option 3 of DR 431.
207*38fd1498Szrj template<typename _Alloc, bool = __is_empty(_Alloc)>
208*38fd1498Szrj struct __alloc_swap
209*38fd1498Szrj { static void _S_do_it(_Alloc&, _Alloc&) _GLIBCXX_NOEXCEPT { } };
210*38fd1498Szrj
211*38fd1498Szrj template<typename _Alloc>
212*38fd1498Szrj struct __alloc_swap<_Alloc, false>
213*38fd1498Szrj {
214*38fd1498Szrj static void
215*38fd1498Szrj _S_do_it(_Alloc& __one, _Alloc& __two) _GLIBCXX_NOEXCEPT
216*38fd1498Szrj {
217*38fd1498Szrj // Precondition: swappable allocators.
218*38fd1498Szrj if (__one != __two)
219*38fd1498Szrj swap(__one, __two);
220*38fd1498Szrj }
221*38fd1498Szrj };
222*38fd1498Szrj
223*38fd1498Szrj // Optimize for stateless allocators.
224*38fd1498Szrj template<typename _Alloc, bool = __is_empty(_Alloc)>
225*38fd1498Szrj struct __alloc_neq
226*38fd1498Szrj {
227*38fd1498Szrj static bool
228*38fd1498Szrj _S_do_it(const _Alloc&, const _Alloc&)
229*38fd1498Szrj { return false; }
230*38fd1498Szrj };
231*38fd1498Szrj
232*38fd1498Szrj template<typename _Alloc>
233*38fd1498Szrj struct __alloc_neq<_Alloc, false>
234*38fd1498Szrj {
235*38fd1498Szrj static bool
236*38fd1498Szrj _S_do_it(const _Alloc& __one, const _Alloc& __two)
237*38fd1498Szrj { return __one != __two; }
238*38fd1498Szrj };
239*38fd1498Szrj
240*38fd1498Szrj #if __cplusplus >= 201103L
241*38fd1498Szrj template<typename _Tp, bool
242*38fd1498Szrj = __or_<is_copy_constructible<typename _Tp::value_type>,
243*38fd1498Szrj is_nothrow_move_constructible<typename _Tp::value_type>>::value>
244*38fd1498Szrj struct __shrink_to_fit_aux
245*38fd1498Szrj { static bool _S_do_it(_Tp&) noexcept { return false; } };
246*38fd1498Szrj
247*38fd1498Szrj template<typename _Tp>
248*38fd1498Szrj struct __shrink_to_fit_aux<_Tp, true>
249*38fd1498Szrj {
250*38fd1498Szrj static bool
251*38fd1498Szrj _S_do_it(_Tp& __c) noexcept
252*38fd1498Szrj {
253*38fd1498Szrj #if __cpp_exceptions
254*38fd1498Szrj try
255*38fd1498Szrj {
256*38fd1498Szrj _Tp(__make_move_if_noexcept_iterator(__c.begin()),
257*38fd1498Szrj __make_move_if_noexcept_iterator(__c.end()),
258*38fd1498Szrj __c.get_allocator()).swap(__c);
259*38fd1498Szrj return true;
260*38fd1498Szrj }
261*38fd1498Szrj catch(...)
262*38fd1498Szrj { return false; }
263*38fd1498Szrj #else
264*38fd1498Szrj return false;
265*38fd1498Szrj #endif
266*38fd1498Szrj }
267*38fd1498Szrj };
268*38fd1498Szrj #endif
269*38fd1498Szrj
270*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
271*38fd1498Szrj } // namespace std
272*38fd1498Szrj
273*38fd1498Szrj #endif
274