Branch data Line data Source code
1 : : // Allocators -*- C++ -*-
2 : :
3 : : // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 : : // Free Software Foundation, Inc.
5 : : //
6 : : // This file is part of the GNU ISO C++ Library. This library is free
7 : : // software; you can redistribute it and/or modify it under the
8 : : // terms of the GNU General Public License as published by the
9 : : // Free Software Foundation; either version 2, or (at your option)
10 : : // any later version.
11 : :
12 : : // This library is distributed in the hope that it will be useful,
13 : : // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : : // GNU General Public License for more details.
16 : :
17 : : // You should have received a copy of the GNU General Public License along
18 : : // with this library; see the file COPYING. If not, write to the Free
19 : : // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 : : // USA.
21 : :
22 : : // As a special exception, you may use this file as part of a free software
23 : : // library without restriction. Specifically, if other files instantiate
24 : : // templates or use macros or inline functions from this file, or you compile
25 : : // this file and link it with other files to produce an executable, this
26 : : // file does not by itself cause the resulting executable to be covered by
27 : : // the GNU General Public License. This exception does not however
28 : : // invalidate any other reasons why the executable file might be covered by
29 : : // the GNU General Public License.
30 : :
31 : : /*
32 : : * Copyright (c) 1996-1997
33 : : * Silicon Graphics Computer Systems, Inc.
34 : : *
35 : : * Permission to use, copy, modify, distribute and sell this software
36 : : * and its documentation for any purpose is hereby granted without fee,
37 : : * provided that the above copyright notice appear in all copies and
38 : : * that both that copyright notice and this permission notice appear
39 : : * in supporting documentation. Silicon Graphics makes no
40 : : * representations about the suitability of this software for any
41 : : * purpose. It is provided "as is" without express or implied warranty.
42 : : */
43 : :
44 : : /** @file allocator.h
45 : : * This is an internal header file, included by other library headers.
46 : : * You should not attempt to use it directly.
47 : : */
48 : :
49 : : #ifndef _ALLOCATOR_H
50 : : #define _ALLOCATOR_H 1
51 : :
52 : : // Define the base class to std::allocator.
53 : : #include <bits/c++allocator.h>
54 : :
55 : : _GLIBCXX_BEGIN_NAMESPACE(std)
56 : :
57 : : template<typename _Tp>
58 : : class allocator;
59 : :
60 : : /// allocator<void> specialization.
61 : : template<>
62 : : class allocator<void>
63 : : {
64 : : public:
65 : : typedef size_t size_type;
66 : : typedef ptrdiff_t difference_type;
67 : : typedef void* pointer;
68 : : typedef const void* const_pointer;
69 : : typedef void value_type;
70 : :
71 : : template<typename _Tp1>
72 : : struct rebind
73 : : { typedef allocator<_Tp1> other; };
74 : : };
75 : :
76 : : /**
77 : : * @brief The "standard" allocator, as per [20.4].
78 : : *
79 : : * Further details:
80 : : * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
81 : : */
82 : : template<typename _Tp>
83 : : class allocator: public __glibcxx_base_allocator<_Tp>
84 : : {
85 : : public:
86 : : typedef size_t size_type;
87 : : typedef ptrdiff_t difference_type;
88 : : typedef _Tp* pointer;
89 : : typedef const _Tp* const_pointer;
90 : : typedef _Tp& reference;
91 : : typedef const _Tp& const_reference;
92 : : typedef _Tp value_type;
93 : :
94 : : template<typename _Tp1>
95 : : struct rebind
96 : : { typedef allocator<_Tp1> other; };
97 : :
98 : 1045634 : allocator() throw() { }
99 : :
100 : : allocator(const allocator& __a) throw()
101 : 1988303 : : __glibcxx_base_allocator<_Tp>(__a) { }
102 : :
103 : : template<typename _Tp1>
104 : 3130 : allocator(const allocator<_Tp1>&) throw() { }
105 : :
106 : 1168749 : ~allocator() throw() { }
107 : :
108 : : // Inherit everything else.
109 : : };
110 : :
111 : : template<typename _T1, typename _T2>
112 : : inline bool
113 : : operator==(const allocator<_T1>&, const allocator<_T2>&)
114 : : { return true; }
115 : :
116 : : template<typename _Tp>
117 : : inline bool
118 : : operator==(const allocator<_Tp>&, const allocator<_Tp>&)
119 : 76536 : { return true; }
120 : :
121 : : template<typename _T1, typename _T2>
122 : : inline bool
123 : : operator!=(const allocator<_T1>&, const allocator<_T2>&)
124 : : { return false; }
125 : :
126 : : template<typename _Tp>
127 : : inline bool
128 : : operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
129 : : { return false; }
130 : :
131 : : // Inhibit implicit instantiations for required instantiations,
132 : : // which are defined via explicit instantiations elsewhere.
133 : : // NB: This syntax is a GNU extension.
134 : : #if _GLIBCXX_EXTERN_TEMPLATE
135 : : extern template class allocator<char>;
136 : : extern template class allocator<wchar_t>;
137 : : #endif
138 : :
139 : : // Undefine.
140 : : #undef __glibcxx_base_allocator
141 : :
142 : : // To implement Option 3 of DR 431.
143 : : template<typename _Alloc, bool = __is_empty(_Alloc)>
144 : : struct __alloc_swap
145 : : { static void _S_do_it(_Alloc&, _Alloc&) { } };
146 : :
147 : : template<typename _Alloc>
148 : : struct __alloc_swap<_Alloc, false>
149 : : {
150 : : static void
151 : : _S_do_it(_Alloc& __one, _Alloc& __two)
152 : : {
153 : : // Precondition: swappable allocators.
154 : : if (__one != __two)
155 : : swap(__one, __two);
156 : : }
157 : : };
158 : :
159 : : // Optimize for stateless allocators.
160 : : template<typename _Alloc, bool = __is_empty(_Alloc)>
161 : : struct __alloc_neq
162 : : {
163 : : static bool
164 : : _S_do_it(const _Alloc&, const _Alloc&)
165 : : { return false; }
166 : : };
167 : :
168 : : template<typename _Alloc>
169 : : struct __alloc_neq<_Alloc, false>
170 : : {
171 : : static bool
172 : : _S_do_it(const _Alloc& __one, const _Alloc& __two)
173 : : { return __one != __two; }
174 : : };
175 : :
176 : : _GLIBCXX_END_NAMESPACE
177 : :
178 : : #endif
|