1*e4b17023SJohn Marino // Allocators -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4*e4b17023SJohn Marino // 2011 Free Software Foundation, Inc.
5*e4b17023SJohn Marino //
6*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
7*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
8*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
9*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
10*e4b17023SJohn Marino // any later version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
13*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*e4b17023SJohn Marino // GNU General Public License for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
18*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
19*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
22*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
23*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
25*e4b17023SJohn Marino
26*e4b17023SJohn Marino /*
27*e4b17023SJohn Marino * Copyright (c) 1996-1997
28*e4b17023SJohn Marino * Silicon Graphics Computer Systems, Inc.
29*e4b17023SJohn Marino *
30*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software
31*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee,
32*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and
33*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear
34*e4b17023SJohn Marino * in supporting documentation. Silicon Graphics makes no
35*e4b17023SJohn Marino * representations about the suitability of this software for any
36*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty.
37*e4b17023SJohn Marino */
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino /** @file bits/allocator.h
40*e4b17023SJohn Marino * This is an internal header file, included by other library headers.
41*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{memory}
42*e4b17023SJohn Marino */
43*e4b17023SJohn Marino
44*e4b17023SJohn Marino #ifndef _ALLOCATOR_H
45*e4b17023SJohn Marino #define _ALLOCATOR_H 1
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino // Define the base class to std::allocator.
48*e4b17023SJohn Marino #include <bits/c++allocator.h>
49*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)50*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
51*e4b17023SJohn Marino {
52*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
53*e4b17023SJohn Marino
54*e4b17023SJohn Marino /**
55*e4b17023SJohn Marino * @defgroup allocators Allocators
56*e4b17023SJohn Marino * @ingroup memory
57*e4b17023SJohn Marino *
58*e4b17023SJohn Marino * Classes encapsulating memory operations.
59*e4b17023SJohn Marino *
60*e4b17023SJohn Marino * @{
61*e4b17023SJohn Marino */
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino template<typename _Tp>
64*e4b17023SJohn Marino class allocator;
65*e4b17023SJohn Marino
66*e4b17023SJohn Marino /// allocator<void> specialization.
67*e4b17023SJohn Marino template<>
68*e4b17023SJohn Marino class allocator<void>
69*e4b17023SJohn Marino {
70*e4b17023SJohn Marino public:
71*e4b17023SJohn Marino typedef size_t size_type;
72*e4b17023SJohn Marino typedef ptrdiff_t difference_type;
73*e4b17023SJohn Marino typedef void* pointer;
74*e4b17023SJohn Marino typedef const void* const_pointer;
75*e4b17023SJohn Marino typedef void value_type;
76*e4b17023SJohn Marino
77*e4b17023SJohn Marino template<typename _Tp1>
78*e4b17023SJohn Marino struct rebind
79*e4b17023SJohn Marino { typedef allocator<_Tp1> other; };
80*e4b17023SJohn Marino };
81*e4b17023SJohn Marino
82*e4b17023SJohn Marino /**
83*e4b17023SJohn Marino * @brief The @a standard allocator, as per [20.4].
84*e4b17023SJohn Marino *
85*e4b17023SJohn Marino * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
86*e4b17023SJohn Marino * for further details.
87*e4b17023SJohn Marino */
88*e4b17023SJohn Marino template<typename _Tp>
89*e4b17023SJohn Marino class allocator: public __glibcxx_base_allocator<_Tp>
90*e4b17023SJohn Marino {
91*e4b17023SJohn Marino public:
92*e4b17023SJohn Marino typedef size_t size_type;
93*e4b17023SJohn Marino typedef ptrdiff_t difference_type;
94*e4b17023SJohn Marino typedef _Tp* pointer;
95*e4b17023SJohn Marino typedef const _Tp* const_pointer;
96*e4b17023SJohn Marino typedef _Tp& reference;
97*e4b17023SJohn Marino typedef const _Tp& const_reference;
98*e4b17023SJohn Marino typedef _Tp value_type;
99*e4b17023SJohn Marino
100*e4b17023SJohn Marino template<typename _Tp1>
101*e4b17023SJohn Marino struct rebind
102*e4b17023SJohn Marino { typedef allocator<_Tp1> other; };
103*e4b17023SJohn Marino
104*e4b17023SJohn Marino allocator() throw() { }
105*e4b17023SJohn Marino
106*e4b17023SJohn Marino allocator(const allocator& __a) throw()
107*e4b17023SJohn Marino : __glibcxx_base_allocator<_Tp>(__a) { }
108*e4b17023SJohn Marino
109*e4b17023SJohn Marino template<typename _Tp1>
110*e4b17023SJohn Marino allocator(const allocator<_Tp1>&) throw() { }
111*e4b17023SJohn Marino
112*e4b17023SJohn Marino ~allocator() throw() { }
113*e4b17023SJohn Marino
114*e4b17023SJohn Marino // Inherit everything else.
115*e4b17023SJohn Marino };
116*e4b17023SJohn Marino
117*e4b17023SJohn Marino template<typename _T1, typename _T2>
118*e4b17023SJohn Marino inline bool
119*e4b17023SJohn Marino operator==(const allocator<_T1>&, const allocator<_T2>&)
120*e4b17023SJohn Marino { return true; }
121*e4b17023SJohn Marino
122*e4b17023SJohn Marino template<typename _Tp>
123*e4b17023SJohn Marino inline bool
124*e4b17023SJohn Marino operator==(const allocator<_Tp>&, const allocator<_Tp>&)
125*e4b17023SJohn Marino { return true; }
126*e4b17023SJohn Marino
127*e4b17023SJohn Marino template<typename _T1, typename _T2>
128*e4b17023SJohn Marino inline bool
129*e4b17023SJohn Marino operator!=(const allocator<_T1>&, const allocator<_T2>&)
130*e4b17023SJohn Marino { return false; }
131*e4b17023SJohn Marino
132*e4b17023SJohn Marino template<typename _Tp>
133*e4b17023SJohn Marino inline bool
134*e4b17023SJohn Marino operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
135*e4b17023SJohn Marino { return false; }
136*e4b17023SJohn Marino
137*e4b17023SJohn Marino /**
138*e4b17023SJohn Marino * @}
139*e4b17023SJohn Marino */
140*e4b17023SJohn Marino
141*e4b17023SJohn Marino // Inhibit implicit instantiations for required instantiations,
142*e4b17023SJohn Marino // which are defined via explicit instantiations elsewhere.
143*e4b17023SJohn Marino #if _GLIBCXX_EXTERN_TEMPLATE
144*e4b17023SJohn Marino extern template class allocator<char>;
145*e4b17023SJohn Marino extern template class allocator<wchar_t>;
146*e4b17023SJohn Marino #endif
147*e4b17023SJohn Marino
148*e4b17023SJohn Marino // Undefine.
149*e4b17023SJohn Marino #undef __glibcxx_base_allocator
150*e4b17023SJohn Marino
151*e4b17023SJohn Marino // To implement Option 3 of DR 431.
152*e4b17023SJohn Marino template<typename _Alloc, bool = __is_empty(_Alloc)>
153*e4b17023SJohn Marino struct __alloc_swap
154*e4b17023SJohn Marino { static void _S_do_it(_Alloc&, _Alloc&) { } };
155*e4b17023SJohn Marino
156*e4b17023SJohn Marino template<typename _Alloc>
157*e4b17023SJohn Marino struct __alloc_swap<_Alloc, false>
158*e4b17023SJohn Marino {
159*e4b17023SJohn Marino static void
160*e4b17023SJohn Marino _S_do_it(_Alloc& __one, _Alloc& __two)
161*e4b17023SJohn Marino {
162*e4b17023SJohn Marino // Precondition: swappable allocators.
163*e4b17023SJohn Marino if (__one != __two)
164*e4b17023SJohn Marino swap(__one, __two);
165*e4b17023SJohn Marino }
166*e4b17023SJohn Marino };
167*e4b17023SJohn Marino
168*e4b17023SJohn Marino // Optimize for stateless allocators.
169*e4b17023SJohn Marino template<typename _Alloc, bool = __is_empty(_Alloc)>
170*e4b17023SJohn Marino struct __alloc_neq
171*e4b17023SJohn Marino {
172*e4b17023SJohn Marino static bool
173*e4b17023SJohn Marino _S_do_it(const _Alloc&, const _Alloc&)
174*e4b17023SJohn Marino { return false; }
175*e4b17023SJohn Marino };
176*e4b17023SJohn Marino
177*e4b17023SJohn Marino template<typename _Alloc>
178*e4b17023SJohn Marino struct __alloc_neq<_Alloc, false>
179*e4b17023SJohn Marino {
180*e4b17023SJohn Marino static bool
181*e4b17023SJohn Marino _S_do_it(const _Alloc& __one, const _Alloc& __two)
182*e4b17023SJohn Marino { return __one != __two; }
183*e4b17023SJohn Marino };
184*e4b17023SJohn Marino
185*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
186*e4b17023SJohn Marino template<typename _Tp, bool
187*e4b17023SJohn Marino = __or_<is_copy_constructible<typename _Tp::value_type>,
188*e4b17023SJohn Marino is_nothrow_move_constructible<typename _Tp::value_type>>::value>
189*e4b17023SJohn Marino struct __shrink_to_fit_aux
190*e4b17023SJohn Marino { static bool _S_do_it(_Tp&) { return false; } };
191*e4b17023SJohn Marino
192*e4b17023SJohn Marino template<typename _Tp>
193*e4b17023SJohn Marino struct __shrink_to_fit_aux<_Tp, true>
194*e4b17023SJohn Marino {
195*e4b17023SJohn Marino static bool
196*e4b17023SJohn Marino _S_do_it(_Tp& __c)
197*e4b17023SJohn Marino {
198*e4b17023SJohn Marino __try
199*e4b17023SJohn Marino {
200*e4b17023SJohn Marino _Tp(__make_move_if_noexcept_iterator(__c.begin()),
201*e4b17023SJohn Marino __make_move_if_noexcept_iterator(__c.end()),
202*e4b17023SJohn Marino __c.get_allocator()).swap(__c);
203*e4b17023SJohn Marino return true;
204*e4b17023SJohn Marino }
205*e4b17023SJohn Marino __catch(...)
206*e4b17023SJohn Marino { return false; }
207*e4b17023SJohn Marino }
208*e4b17023SJohn Marino };
209*e4b17023SJohn Marino
210*e4b17023SJohn Marino // Declare uses_allocator so it can be specialized in <queue> etc.
211*e4b17023SJohn Marino template<typename, typename>
212*e4b17023SJohn Marino struct uses_allocator;
213*e4b17023SJohn Marino #endif
214*e4b17023SJohn Marino
215*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
216*e4b17023SJohn Marino } // namespace std
217*e4b17023SJohn Marino
218*e4b17023SJohn Marino #endif
219