]>
Commit | Line | Data |
---|---|---|
1ff9402d BK |
1 | // Allocators -*- C++ -*- |
2 | ||
3 | // Copyright (C) 2001, 2002, 2003 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 2, 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 | // You should have received a copy of the GNU General Public License along | |
17 | // with this library; see the file COPYING. If not, write to the Free | |
18 | // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
19 | // USA. | |
20 | ||
21 | // As a special exception, you may use this file as part of a free software | |
22 | // library without restriction. Specifically, if other files instantiate | |
23 | // templates or use macros or inline functions from this file, or you compile | |
24 | // this file and link it with other files to produce an executable, this | |
25 | // file does not by itself cause the resulting executable to be covered by | |
26 | // the GNU General Public License. This exception does not however | |
27 | // invalidate any other reasons why the executable file might be covered by | |
28 | // the GNU General Public License. | |
29 | ||
30 | /* | |
31 | * Copyright (c) 1996-1997 | |
32 | * Silicon Graphics Computer Systems, Inc. | |
33 | * | |
34 | * Permission to use, copy, modify, distribute and sell this software | |
35 | * and its documentation for any purpose is hereby granted without fee, | |
36 | * provided that the above copyright notice appear in all copies and | |
37 | * that both that copyright notice and this permission notice appear | |
38 | * in supporting documentation. Silicon Graphics makes no | |
39 | * representations about the suitability of this software for any | |
40 | * purpose. It is provided "as is" without express or implied warranty. | |
41 | */ | |
42 | ||
43 | /** @file allocator.h | |
44 | * This is an internal header file, included by other library headers. | |
45 | * You should not attempt to use it directly. | |
46 | */ | |
47 | ||
48 | /** | |
49 | * @defgroup Allocators Memory Allocators | |
50 | * @if maint | |
51 | * allocator.h implements some node allocators. These are NOT the same as | |
52 | * allocators in the C++ standard, nor in the original H-P STL. They do not | |
53 | * encapsulate different pointer types; we assume that there is only one | |
54 | * pointer type. The C++ standard allocators are intended to allocate | |
55 | * individual objects, not pools or arenas. | |
56 | * | |
57 | * In this file allocators are of two different styles: "standard" and | |
58 | * "SGI" (quotes included). "Standard" allocators conform to 20.4. "SGI" | |
59 | * allocators differ in AT LEAST the following ways (add to this list as you | |
60 | * discover them): | |
61 | * | |
62 | * - "Standard" allocate() takes two parameters (n_count,hint=0) but "SGI" | |
63 | * allocate() takes one paramter (n_size). | |
64 | * - Likewise, "standard" deallocate()'s argument is a count, but in "SGI" | |
65 | * is a byte size. | |
66 | * - max_size(), construct(), and destroy() are missing in "SGI" allocators. | |
67 | * - reallocate(p,oldsz,newsz) is added in "SGI", and behaves as | |
68 | * if p=realloc(p,newsz). | |
69 | * | |
70 | * "SGI" allocators may be wrapped in __allocator to convert the interface | |
71 | * into a "standard" one. | |
72 | * @endif | |
73 | * | |
74 | * The canonical description of these classes is in docs/html/ext/howto.html | |
75 | * or online at http://gcc.gnu.org/onlinedocs/libstdc++/ext/howto.html#3 | |
76 | */ | |
77 | ||
78 | #ifndef _ALLOCATOR_H | |
79 | #define _ALLOCATOR_H 1 | |
80 | ||
81 | #include <bits/functexcept.h> // For __throw_bad_alloc | |
82 | #include <bits/allocator_traits.h> | |
83 | ||
84 | // Pick a default underlying allocator. | |
85 | #include <ext/pool_allocator.h> | |
86 | ||
87 | namespace std | |
88 | { | |
89 | typedef __gnu_cxx::__pool_alloc<true, 0> __alloc; | |
90 | ||
91 | /// The version for the default allocator. | |
92 | template<typename _Tp, typename _Tp1> | |
93 | struct _Alloc_traits<_Tp, allocator<_Tp1> > | |
94 | { | |
95 | static const bool _S_instanceless = true; | |
96 | typedef __simple_alloc<_Tp, __alloc> _Alloc_type; | |
97 | typedef allocator<_Tp> allocator_type; | |
98 | }; | |
99 | //@} | |
100 | } | |
101 | ||
102 | namespace std | |
103 | { | |
104 | /** | |
105 | * @brief The "standard" allocator, as per [20.4]. | |
106 | * | |
107 | * The private _Alloc is "SGI" style. (See comments at the top | |
108 | * of allocator.h.) | |
109 | * | |
110 | * The underlying allocator behaves as follows. | |
111 | * - __pool_alloc is used via two typedefs | |
112 | * - "__alloc" typedef is threadsafe via the locks | |
113 | * - __new_alloc is used for memory requests | |
114 | * | |
115 | * (See @link Allocators allocators info @endlink for more.) | |
116 | */ | |
117 | template<typename _Tp> | |
118 | class allocator | |
119 | { | |
120 | // The underlying allocator. | |
121 | typedef __alloc _Alloc; | |
122 | ||
123 | public: | |
124 | typedef size_t size_type; | |
125 | typedef ptrdiff_t difference_type; | |
126 | typedef _Tp* pointer; | |
127 | typedef const _Tp* const_pointer; | |
128 | typedef _Tp& reference; | |
129 | typedef const _Tp& const_reference; | |
130 | typedef _Tp value_type; | |
131 | ||
132 | template<typename _Tp1> | |
133 | struct rebind | |
134 | { typedef allocator<_Tp1> other; }; | |
135 | ||
136 | allocator() throw() { } | |
137 | ||
138 | allocator(const allocator&) throw() { } | |
139 | ||
140 | template<typename _Tp1> | |
141 | allocator(const allocator<_Tp1>&) throw() { } | |
142 | ||
143 | ~allocator() throw() { } | |
144 | ||
145 | pointer | |
146 | address(reference __x) const { return &__x; } | |
147 | ||
148 | const_pointer | |
149 | address(const_reference __x) const { return &__x; } | |
150 | ||
151 | // NB: __n is permitted to be 0. The C++ standard says nothing | |
152 | // about what the return value is when __n == 0. | |
153 | _Tp* | |
154 | allocate(size_type __n, const void* = 0) | |
155 | { | |
156 | _Tp* __ret = 0; | |
157 | if (__n) | |
158 | { | |
159 | if (__n <= this->max_size()) | |
160 | __ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp))); | |
161 | else | |
162 | __throw_bad_alloc(); | |
163 | } | |
164 | return __ret; | |
165 | } | |
166 | ||
167 | // __p is not permitted to be a null pointer. | |
168 | void | |
169 | deallocate(pointer __p, size_type __n) | |
170 | { _Alloc::deallocate(__p, __n * sizeof(_Tp)); } | |
171 | ||
172 | size_type | |
173 | max_size() const throw() { return size_t(-1) / sizeof(_Tp); } | |
174 | ||
175 | void construct(pointer __p, const _Tp& __val) { new(__p) _Tp(__val); } | |
176 | ||
177 | void destroy(pointer __p) { __p->~_Tp(); } | |
178 | }; | |
179 | ||
180 | template<> | |
181 | class allocator<void> | |
182 | { | |
183 | public: | |
184 | typedef size_t size_type; | |
185 | typedef ptrdiff_t difference_type; | |
186 | typedef void* pointer; | |
187 | typedef const void* const_pointer; | |
188 | typedef void value_type; | |
189 | ||
190 | template<typename _Tp1> | |
191 | struct rebind | |
192 | { typedef allocator<_Tp1> other; }; | |
193 | }; | |
194 | ||
195 | ||
196 | template<typename _T1, typename _T2> | |
197 | inline bool | |
198 | operator==(const allocator<_T1>&, const allocator<_T2>&) | |
199 | { return true; } | |
200 | ||
201 | template<typename _T1, typename _T2> | |
202 | inline bool | |
203 | operator!=(const allocator<_T1>&, const allocator<_T2>&) | |
204 | { return false; } | |
205 | ||
206 | // Inhibit implicit instantiations for required instantiations, | |
207 | // which are defined via explicit instantiations elsewhere. | |
208 | // NB: This syntax is a GNU extension. | |
3d7c150e | 209 | #if _GLIBCXX_EXTERN_TEMPLATE |
1ff9402d BK |
210 | extern template class allocator<char>; |
211 | extern template class allocator<wchar_t>; | |
212 | #endif | |
213 | } // namespace std | |
214 | ||
215 | #endif |