williamr@2
|
1 |
/*
|
williamr@2
|
2 |
*
|
williamr@2
|
3 |
* Copyright (c) 1996,1997
|
williamr@2
|
4 |
* Silicon Graphics Computer Systems, Inc.
|
williamr@2
|
5 |
*
|
williamr@2
|
6 |
* Copyright (c) 1997
|
williamr@2
|
7 |
* Moscow Center for SPARC Technology
|
williamr@2
|
8 |
*
|
williamr@2
|
9 |
* Copyright (c) 1999
|
williamr@2
|
10 |
* Boris Fomitchev
|
williamr@2
|
11 |
*
|
williamr@2
|
12 |
* This material is provided "as is", with absolutely no warranty expressed
|
williamr@2
|
13 |
* or implied. Any use is at your own risk.
|
williamr@2
|
14 |
*
|
williamr@2
|
15 |
* Permission to use or copy this software for any purpose is hereby granted
|
williamr@2
|
16 |
* without fee, provided the above notices are retained on all copies.
|
williamr@2
|
17 |
* Permission to modify the code and to distribute modified code is granted,
|
williamr@2
|
18 |
* provided the above notices are retained, and a notice that the code was
|
williamr@2
|
19 |
* modified is included with the above copyright notice.
|
williamr@2
|
20 |
*
|
williamr@2
|
21 |
*/
|
williamr@2
|
22 |
|
williamr@2
|
23 |
/* NOTE: This is an internal header file, included by other STL headers.
|
williamr@2
|
24 |
* You should not attempt to use it directly.
|
williamr@2
|
25 |
*/
|
williamr@2
|
26 |
|
williamr@2
|
27 |
#ifndef _STLP_INTERNAL_ALLOC_H
|
williamr@2
|
28 |
#define _STLP_INTERNAL_ALLOC_H
|
williamr@2
|
29 |
|
williamr@2
|
30 |
# ifndef _STLP_CSTDDEF
|
williamr@2
|
31 |
# include <cstddef>
|
williamr@2
|
32 |
# endif
|
williamr@2
|
33 |
|
williamr@2
|
34 |
#if !defined (_STLP_DEBUG_H) && (defined (_STLP_DEBUG) || defined (_STLP_ASSERTIONS))
|
williamr@2
|
35 |
# include <stl/debug/_debug.h>
|
williamr@2
|
36 |
#endif
|
williamr@2
|
37 |
|
williamr@2
|
38 |
# ifndef _STLP_CSTDLIB
|
williamr@2
|
39 |
# include <cstdlib>
|
williamr@2
|
40 |
# endif
|
williamr@2
|
41 |
# ifndef _STLP_CSTRING
|
williamr@2
|
42 |
# include <cstring>
|
williamr@2
|
43 |
# endif
|
williamr@2
|
44 |
|
williamr@2
|
45 |
# ifndef __THROW_BAD_ALLOC
|
williamr@2
|
46 |
# if !defined(_STLP_USE_EXCEPTIONS)
|
williamr@2
|
47 |
# if !defined (_STLP_CSTDIO)
|
williamr@2
|
48 |
# include <cstdio>
|
williamr@2
|
49 |
# endif
|
williamr@2
|
50 |
# if !defined (_STLP_CSTDLIB)
|
williamr@2
|
51 |
# include <cstdlib>
|
williamr@2
|
52 |
# endif
|
williamr@2
|
53 |
# define __THROW_BAD_ALLOC puts("out of memory\n"); exit(1)
|
williamr@2
|
54 |
# else /* !defined(_STLP_USE_EXCEPTIONS) */
|
williamr@2
|
55 |
# define __THROW_BAD_ALLOC throw _STLP_STD::bad_alloc()
|
williamr@2
|
56 |
# endif /* !defined(_STLP_USE_EXCEPTIONS) */
|
williamr@2
|
57 |
# endif /* __THROW_BAD_ALLOC */
|
williamr@2
|
58 |
|
williamr@2
|
59 |
# ifndef _STLP_INTERNAL_NEW_HEADER
|
williamr@2
|
60 |
# include <stl/_new.h>
|
williamr@2
|
61 |
# endif
|
williamr@2
|
62 |
|
williamr@2
|
63 |
// #if ! defined (__SYMBIAN32__)
|
williamr@2
|
64 |
#if /* defined (_STLP_THREADS) && */ ! defined (_STLP_INTERNAL_THREADS_H)
|
williamr@2
|
65 |
# include <stl/_threads.h>
|
williamr@2
|
66 |
// #endif
|
williamr@2
|
67 |
#endif
|
williamr@2
|
68 |
|
williamr@2
|
69 |
#ifndef _STLP_INTERNAL_CONSTRUCT_H
|
williamr@2
|
70 |
# include <stl/_construct.h>
|
williamr@2
|
71 |
#endif
|
williamr@2
|
72 |
|
williamr@2
|
73 |
#ifndef __ALLOC
|
williamr@2
|
74 |
# define __ALLOC __sgi_alloc
|
williamr@2
|
75 |
#endif
|
williamr@2
|
76 |
|
williamr@2
|
77 |
# ifndef __RESTRICT
|
williamr@2
|
78 |
# define __RESTRICT
|
williamr@2
|
79 |
# endif
|
williamr@2
|
80 |
|
williamr@2
|
81 |
#if defined (_STLP_THREADS) || (defined(_STLP_OWN_IOSTREAMS) && ! defined (_STLP_NO_THREADS) && ! defined (_NOTHREADS) )
|
williamr@2
|
82 |
# define _STLP_NODE_ALLOCATOR_THREADS true
|
williamr@2
|
83 |
#else
|
williamr@2
|
84 |
# define _STLP_NODE_ALLOCATOR_THREADS false
|
williamr@2
|
85 |
#endif
|
williamr@2
|
86 |
|
williamr@2
|
87 |
_STLP_BEGIN_NAMESPACE
|
williamr@2
|
88 |
|
williamr@2
|
89 |
# if defined (_STLP_USE_RAW_SGI_ALLOCATORS)
|
williamr@2
|
90 |
template <class _Tp, class _Alloc> struct __allocator;
|
williamr@2
|
91 |
# endif
|
williamr@2
|
92 |
|
williamr@2
|
93 |
#ifndef _STLP_NO_NODE_ALLOC
|
williamr@2
|
94 |
|
williamr@2
|
95 |
// Malloc-based allocator. Typically slower than default alloc below.
|
williamr@2
|
96 |
// Typically thread-safe and more storage efficient.
|
williamr@2
|
97 |
|
williamr@2
|
98 |
typedef void (* __oom_handler_type)();
|
williamr@2
|
99 |
|
williamr@2
|
100 |
template <int __inst>
|
williamr@2
|
101 |
class __malloc_alloc {
|
williamr@2
|
102 |
private:
|
williamr@2
|
103 |
static void* _STLP_CALL _S_oom_malloc(size_t);
|
williamr@2
|
104 |
static __oom_handler_type __oom_handler;
|
williamr@2
|
105 |
public:
|
williamr@2
|
106 |
// this one is needed for proper simple_alloc wrapping
|
williamr@2
|
107 |
typedef char value_type;
|
williamr@2
|
108 |
# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
|
williamr@2
|
109 |
template <class _Tp1> struct rebind {
|
williamr@2
|
110 |
typedef __allocator<_Tp1, __malloc_alloc<__inst> > other;
|
williamr@2
|
111 |
};
|
williamr@2
|
112 |
# endif
|
williamr@2
|
113 |
static void* _STLP_CALL allocate(size_t __n) {
|
williamr@2
|
114 |
void* __result = malloc(__n);
|
williamr@2
|
115 |
if (0 == __result) __result = _S_oom_malloc(__n);
|
williamr@2
|
116 |
return __result;
|
williamr@2
|
117 |
}
|
williamr@2
|
118 |
static void _STLP_CALL deallocate(void* __p, size_t /* __n */) { free((char*)__p); }
|
williamr@2
|
119 |
static __oom_handler_type _STLP_CALL set_malloc_handler(__oom_handler_type __f) {
|
williamr@2
|
120 |
__oom_handler_type __old = __oom_handler;
|
williamr@2
|
121 |
__oom_handler = __f;
|
williamr@2
|
122 |
return(__old);
|
williamr@2
|
123 |
}
|
williamr@2
|
124 |
};
|
williamr@2
|
125 |
|
williamr@2
|
126 |
# endif
|
williamr@2
|
127 |
|
williamr@2
|
128 |
// New-based allocator. Typically slower than default alloc below.
|
williamr@2
|
129 |
// Typically thread-safe and more storage efficient.
|
williamr@2
|
130 |
class _STLP_CLASS_DECLSPEC __new_alloc {
|
williamr@2
|
131 |
public:
|
williamr@2
|
132 |
// this one is needed for proper simple_alloc wrapping
|
williamr@2
|
133 |
typedef char value_type;
|
williamr@2
|
134 |
# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined(_STLP_USE_RAW_SGI_ALLOCATORS)
|
williamr@2
|
135 |
template <class _Tp1> struct rebind {
|
williamr@2
|
136 |
typedef __allocator<_Tp1, __new_alloc > other;
|
williamr@2
|
137 |
};
|
williamr@2
|
138 |
# endif
|
williamr@2
|
139 |
static void* _STLP_CALL allocate(size_t __n) {
|
williamr@2
|
140 |
return __stl_new(__n);
|
williamr@2
|
141 |
}
|
williamr@2
|
142 |
static void _STLP_CALL deallocate(void* __p, size_t) { __stl_delete(__p); }
|
williamr@2
|
143 |
};
|
williamr@2
|
144 |
|
williamr@2
|
145 |
|
williamr@2
|
146 |
// Allocator adaptor to check size arguments for debugging.
|
williamr@2
|
147 |
// Reports errors using assert. Checking can be disabled with
|
williamr@2
|
148 |
// NDEBUG, but it's far better to just use the underlying allocator
|
williamr@2
|
149 |
// instead when no checking is desired.
|
williamr@2
|
150 |
// There is some evidence that this can confuse Purify.
|
williamr@2
|
151 |
// This adaptor can only be applied to raw allocators
|
williamr@2
|
152 |
|
williamr@2
|
153 |
template <class _Alloc>
|
williamr@2
|
154 |
class __debug_alloc : public _Alloc {
|
williamr@2
|
155 |
public:
|
williamr@2
|
156 |
typedef _Alloc __allocator_type;
|
williamr@2
|
157 |
typedef typename _Alloc::value_type value_type;
|
williamr@2
|
158 |
private:
|
williamr@2
|
159 |
struct __alloc_header {
|
williamr@2
|
160 |
size_t __magic: 16;
|
williamr@2
|
161 |
size_t __type_size:16;
|
williamr@2
|
162 |
_STLP_UINT32_T _M_size;
|
williamr@2
|
163 |
}; // that is 8 bytes for sure
|
williamr@2
|
164 |
// Sunpro CC has bug on enums, so extra_before/after set explicitly
|
williamr@2
|
165 |
enum { __pad=8, __magic=0xdeba, __deleted_magic = 0xdebd,
|
williamr@2
|
166 |
__shred_byte= _STLP_SHRED_BYTE
|
williamr@2
|
167 |
};
|
williamr@2
|
168 |
|
williamr@2
|
169 |
enum { __extra_before = 16, __extra_after = 8 };
|
williamr@2
|
170 |
// Size of space used to store size. Note
|
williamr@2
|
171 |
// that this must be large enough to preserve
|
williamr@2
|
172 |
// alignment.
|
williamr@2
|
173 |
static size_t _STLP_CALL __extra_before_chunk() {
|
williamr@2
|
174 |
return (long)__extra_before/sizeof(value_type)+
|
williamr@2
|
175 |
(size_t)((long)__extra_before%sizeof(value_type)>0);
|
williamr@2
|
176 |
}
|
williamr@2
|
177 |
static size_t _STLP_CALL __extra_after_chunk() {
|
williamr@2
|
178 |
return (long)__extra_after/sizeof(value_type)+
|
williamr@2
|
179 |
(size_t)((long)__extra_after%sizeof(value_type)>0);
|
williamr@2
|
180 |
}
|
williamr@2
|
181 |
public:
|
williamr@2
|
182 |
# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
|
williamr@2
|
183 |
template <class _Tp1> struct rebind {
|
williamr@2
|
184 |
typedef __allocator< _Tp1, __debug_alloc<_Alloc> > other;
|
williamr@2
|
185 |
};
|
williamr@2
|
186 |
# endif
|
williamr@2
|
187 |
__debug_alloc() {}
|
williamr@2
|
188 |
~__debug_alloc() {}
|
williamr@2
|
189 |
static void * _STLP_CALL allocate(size_t);
|
williamr@2
|
190 |
static void _STLP_CALL deallocate(void *, size_t);
|
williamr@2
|
191 |
};
|
williamr@2
|
192 |
|
williamr@2
|
193 |
# if defined(__OS400__)
|
williamr@2
|
194 |
enum {_ALIGN = 16, _ALIGN_SHIFT=4, _MAX_BYTES = 256};
|
williamr@2
|
195 |
# define _STLP_NFREELISTS 16
|
williamr@2
|
196 |
# else
|
williamr@2
|
197 |
enum {_ALIGN = 8, _ALIGN_SHIFT=3, _MAX_BYTES = 128};
|
williamr@2
|
198 |
# define _STLP_NFREELISTS 16
|
williamr@2
|
199 |
# endif /* __OS400__ */
|
williamr@2
|
200 |
|
williamr@2
|
201 |
#ifndef _STLP_NO_NODE_ALLOC
|
williamr@2
|
202 |
|
williamr@2
|
203 |
// Default node allocator.
|
williamr@2
|
204 |
// With a reasonable compiler, this should be roughly as fast as the
|
williamr@2
|
205 |
// original STL class-specific allocators, but with less fragmentation.
|
williamr@2
|
206 |
// Default_alloc_template parameters are experimental and MAY
|
williamr@2
|
207 |
// DISAPPEAR in the future. Clients should just use alloc for now.
|
williamr@2
|
208 |
//
|
williamr@2
|
209 |
// Important implementation properties:
|
williamr@2
|
210 |
// 1. If the client request an object of size > _MAX_BYTES, the resulting
|
williamr@2
|
211 |
// object will be obtained directly from malloc.
|
williamr@2
|
212 |
// 2. In all other cases, we allocate an object of size exactly
|
williamr@2
|
213 |
// _S_round_up(requested_size). Thus the client has enough size
|
williamr@2
|
214 |
// information that we can return the object to the proper free list
|
williamr@2
|
215 |
// without permanently losing part of the object.
|
williamr@2
|
216 |
//
|
williamr@2
|
217 |
|
williamr@2
|
218 |
// The first template parameter specifies whether more than one thread
|
williamr@2
|
219 |
// may use this allocator. It is safe to allocate an object from
|
williamr@2
|
220 |
// one instance of a default_alloc and deallocate it with another
|
williamr@2
|
221 |
// one. This effectively transfers its ownership to the second one.
|
williamr@2
|
222 |
// This may have undesirable effects on reference locality.
|
williamr@2
|
223 |
// The second parameter is unreferenced and serves only to allow the
|
williamr@2
|
224 |
// creation of multiple default_alloc instances.
|
williamr@2
|
225 |
|
williamr@2
|
226 |
class _STLP_CLASS_DECLSPEC _Node_alloc_obj {
|
williamr@2
|
227 |
public:
|
williamr@2
|
228 |
_Node_alloc_obj * _M_free_list_link;
|
williamr@2
|
229 |
};
|
williamr@2
|
230 |
|
williamr@2
|
231 |
template <bool __threads, int __inst>
|
williamr@2
|
232 |
class __node_alloc {
|
williamr@2
|
233 |
_STLP_PRIVATE:
|
williamr@2
|
234 |
static inline size_t _STLP_CALL _S_round_up(size_t __bytes) { return (((__bytes) + (size_t)_ALIGN-1) & ~((size_t)_ALIGN - 1)); }
|
williamr@2
|
235 |
typedef _Node_alloc_obj _Obj;
|
williamr@2
|
236 |
private:
|
williamr@2
|
237 |
// Returns an object of size __n, and optionally adds to size __n free list.
|
williamr@2
|
238 |
static void* _STLP_CALL _S_refill(size_t __n);
|
williamr@2
|
239 |
// Allocates a chunk for nobjs of size size. nobjs may be reduced
|
williamr@2
|
240 |
// if it is inconvenient to allocate the requested number.
|
williamr@2
|
241 |
static char* _STLP_CALL _S_chunk_alloc(size_t __p_size, int& __nobjs);
|
williamr@2
|
242 |
// Chunk allocation state.
|
williamr@2
|
243 |
static _Node_alloc_obj * _STLP_VOLATILE _S_free_list[_STLP_NFREELISTS];
|
williamr@2
|
244 |
static char* _S_start_free;
|
williamr@2
|
245 |
static char* _S_end_free;
|
williamr@2
|
246 |
static size_t _S_heap_size;
|
williamr@2
|
247 |
static void * _STLP_CALL _M_allocate(size_t __n);
|
williamr@2
|
248 |
/* __p may not be 0 */
|
williamr@2
|
249 |
static void _STLP_CALL _M_deallocate(void *__p, size_t __n);
|
williamr@2
|
250 |
public:
|
williamr@2
|
251 |
// this one is needed for proper simple_alloc wrapping
|
williamr@2
|
252 |
typedef char value_type;
|
williamr@2
|
253 |
# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_USE_RAW_SGI_ALLOCATORS)
|
williamr@2
|
254 |
template <class _Tp1> struct rebind {
|
williamr@2
|
255 |
typedef __allocator<_Tp1, __node_alloc<__threads, __inst> > other;
|
williamr@2
|
256 |
};
|
williamr@2
|
257 |
# endif
|
williamr@2
|
258 |
/* __n must be > 0 */
|
williamr@2
|
259 |
static void * _STLP_CALL allocate(size_t __n) {
|
williamr@2
|
260 |
return (__n > (size_t)_MAX_BYTES) ? __stl_new(__n) : _M_allocate(__n); }
|
williamr@2
|
261 |
/* __p may not be 0 */
|
williamr@2
|
262 |
static void _STLP_CALL deallocate(void *__p, size_t __n) {
|
williamr@2
|
263 |
if (__n > (size_t)_MAX_BYTES) __stl_delete(__p); else _M_deallocate(__p, __n); }
|
williamr@2
|
264 |
};
|
williamr@2
|
265 |
|
williamr@2
|
266 |
# if defined (_STLP_USE_TEMPLATE_EXPORT)
|
williamr@2
|
267 |
_STLP_EXPORT_TEMPLATE_CLASS __malloc_alloc<0>;
|
williamr@2
|
268 |
_STLP_EXPORT_TEMPLATE_CLASS __node_alloc<_STLP_NODE_ALLOCATOR_THREADS, 0>;
|
williamr@2
|
269 |
# endif /* _STLP_USE_TEMPLATE_EXPORT */
|
williamr@2
|
270 |
typedef __node_alloc<_STLP_NODE_ALLOCATOR_THREADS, 0> _Node_alloc;
|
williamr@2
|
271 |
# if defined (_STLP_USE_TEMPLATE_EXPORT)
|
williamr@2
|
272 |
_STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<_Node_alloc>;
|
williamr@2
|
273 |
_STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__new_alloc>;
|
williamr@2
|
274 |
_STLP_EXPORT_TEMPLATE_CLASS __debug_alloc<__malloc_alloc<0> >;
|
williamr@2
|
275 |
# endif
|
williamr@2
|
276 |
|
williamr@2
|
277 |
#endif
|
williamr@2
|
278 |
|
williamr@2
|
279 |
# if defined (_STLP_USE_PERTHREAD_ALLOC)
|
williamr@2
|
280 |
|
williamr@2
|
281 |
_STLP_END_NAMESPACE
|
williamr@2
|
282 |
// include additional header here
|
williamr@2
|
283 |
# include <stl/_pthread_alloc.h>
|
williamr@2
|
284 |
_STLP_BEGIN_NAMESPACE
|
williamr@2
|
285 |
|
williamr@2
|
286 |
# if defined ( _STLP_DEBUG_ALLOC )
|
williamr@2
|
287 |
typedef __debug_alloc<__pthread_alloc> __sgi_alloc;
|
williamr@2
|
288 |
# else
|
williamr@2
|
289 |
typedef __pthread_alloc __sgi_alloc;
|
williamr@2
|
290 |
# endif /* _STLP_DEBUG_ALLOC */
|
williamr@2
|
291 |
|
williamr@2
|
292 |
typedef __pthread_alloc __single_client_alloc;
|
williamr@2
|
293 |
typedef __pthread_alloc __multithreaded_alloc;
|
williamr@2
|
294 |
|
williamr@2
|
295 |
# else
|
williamr@2
|
296 |
|
williamr@2
|
297 |
# if defined ( _STLP_USE_NEWALLOC )
|
williamr@2
|
298 |
|
williamr@2
|
299 |
# if defined ( _STLP_DEBUG_ALLOC )
|
williamr@2
|
300 |
typedef __debug_alloc<__new_alloc> __sgi_alloc;
|
williamr@2
|
301 |
# else
|
williamr@2
|
302 |
typedef __new_alloc __sgi_alloc;
|
williamr@2
|
303 |
# endif /* _STLP_DEBUG_ALLOC */
|
williamr@2
|
304 |
|
williamr@2
|
305 |
typedef __new_alloc __single_client_alloc;
|
williamr@2
|
306 |
typedef __new_alloc __multithreaded_alloc;
|
williamr@2
|
307 |
|
williamr@2
|
308 |
# elif defined (_STLP_USE_MALLOC)
|
williamr@2
|
309 |
|
williamr@2
|
310 |
# if defined ( _STLP_DEBUG_ALLOC )
|
williamr@2
|
311 |
typedef __debug_alloc<__malloc_alloc<0> > __sgi_alloc;
|
williamr@2
|
312 |
# else
|
williamr@2
|
313 |
typedef __malloc_alloc<0> __sgi_alloc;
|
williamr@2
|
314 |
# endif /* _STLP_DEBUG_ALLOC */
|
williamr@2
|
315 |
|
williamr@2
|
316 |
typedef __malloc_alloc<0> __single_client_alloc;
|
williamr@2
|
317 |
typedef __malloc_alloc<0> __multithreaded_alloc;
|
williamr@2
|
318 |
|
williamr@2
|
319 |
# else
|
williamr@2
|
320 |
|
williamr@2
|
321 |
# if defined ( _STLP_DEBUG_ALLOC )
|
williamr@2
|
322 |
typedef __debug_alloc<_Node_alloc> __sgi_alloc;
|
williamr@2
|
323 |
# else
|
williamr@2
|
324 |
typedef _Node_alloc __sgi_alloc;
|
williamr@2
|
325 |
# endif
|
williamr@2
|
326 |
|
williamr@2
|
327 |
typedef __node_alloc<false, 0> __single_client_alloc;
|
williamr@2
|
328 |
typedef __node_alloc<true, 0> __multithreaded_alloc;
|
williamr@2
|
329 |
|
williamr@2
|
330 |
# endif /* _STLP_USE_NEWALLOC */
|
williamr@2
|
331 |
# endif /* PTHREAD_ALLOC */
|
williamr@2
|
332 |
|
williamr@2
|
333 |
// This implements allocators as specified in the C++ standard.
|
williamr@2
|
334 |
//
|
williamr@2
|
335 |
// Note that standard-conforming allocators use many language features
|
williamr@2
|
336 |
// that are not yet widely implemented. In particular, they rely on
|
williamr@2
|
337 |
// member templates, partial specialization, partial ordering of function
|
williamr@2
|
338 |
// templates, the typename keyword, and the use of the template keyword
|
williamr@2
|
339 |
// to refer to a template member of a dependent type.
|
williamr@2
|
340 |
|
williamr@2
|
341 |
template <class _Tp>
|
williamr@2
|
342 |
class allocator {
|
williamr@2
|
343 |
public:
|
williamr@2
|
344 |
|
williamr@2
|
345 |
typedef _Tp value_type;
|
williamr@2
|
346 |
typedef value_type * pointer;
|
williamr@2
|
347 |
typedef const _Tp* const_pointer;
|
williamr@2
|
348 |
typedef _Tp& reference;
|
williamr@2
|
349 |
typedef const _Tp& const_reference;
|
williamr@2
|
350 |
typedef size_t size_type;
|
williamr@2
|
351 |
typedef ptrdiff_t difference_type;
|
williamr@2
|
352 |
# if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
|
williamr@2
|
353 |
template <class _Tp1> struct rebind {
|
williamr@2
|
354 |
typedef allocator<_Tp1> other;
|
williamr@2
|
355 |
};
|
williamr@2
|
356 |
# endif
|
williamr@2
|
357 |
allocator() _STLP_NOTHROW {}
|
williamr@2
|
358 |
# if defined (_STLP_MEMBER_TEMPLATES)
|
williamr@2
|
359 |
template <class _Tp1> allocator(const allocator<_Tp1>&) _STLP_NOTHROW {}
|
williamr@2
|
360 |
# endif
|
williamr@2
|
361 |
allocator(const allocator<_Tp>&) _STLP_NOTHROW {}
|
williamr@2
|
362 |
~allocator() _STLP_NOTHROW {}
|
williamr@2
|
363 |
pointer address(reference __x) const { return &__x; }
|
williamr@2
|
364 |
const_pointer address(const_reference __x) const { return &__x; }
|
williamr@2
|
365 |
// __n is permitted to be 0. The C++ standard says nothing about what the return value is when __n == 0.
|
williamr@2
|
366 |
_Tp* allocate(size_type __n, const void* = 0) {
|
williamr@2
|
367 |
return __n != 0 ? __REINTERPRET_CAST(value_type*,__sgi_alloc::allocate(__n * sizeof(value_type))) : 0;
|
williamr@2
|
368 |
}
|
williamr@2
|
369 |
// __p is permitted to be a null pointer, only if n==0.
|
williamr@2
|
370 |
void deallocate(pointer __p, size_type __n) {
|
williamr@2
|
371 |
_STLP_ASSERT( (__p == 0) == (__n == 0) )
|
williamr@2
|
372 |
if (__p != 0) __sgi_alloc::deallocate((void*)__p, __n * sizeof(value_type));
|
williamr@2
|
373 |
}
|
williamr@2
|
374 |
// backwards compatibility
|
williamr@2
|
375 |
void deallocate(pointer __p) const { if (__p != 0) __sgi_alloc::deallocate((void*)__p, sizeof(value_type)); }
|
williamr@2
|
376 |
size_type max_size() const _STLP_NOTHROW { return size_t(-1) / sizeof(value_type); }
|
williamr@2
|
377 |
void construct(pointer __p, const _Tp& __val) { _STLP_STD::_Construct(__p, __val); }
|
williamr@2
|
378 |
void destroy(pointer __p) { _STLP_STD::_Destroy(__p); }
|
williamr@2
|
379 |
# if defined(__MRC__)||(defined(__SC__) && !defined(__DMC__))
|
williamr@2
|
380 |
template <class _T2> bool operator==(const allocator<_T2>&) const _STLP_NOTHROW { return true; }
|
williamr@2
|
381 |
template <class _T2> bool operator!=(const allocator<_T2>&) const _STLP_NOTHROW { return false; }
|
williamr@2
|
382 |
# endif
|
williamr@2
|
383 |
};
|
williamr@2
|
384 |
|
williamr@2
|
385 |
_STLP_TEMPLATE_NULL
|
williamr@2
|
386 |
class _STLP_CLASS_DECLSPEC allocator<void> {
|
williamr@2
|
387 |
public:
|
williamr@2
|
388 |
typedef size_t size_type;
|
williamr@2
|
389 |
typedef ptrdiff_t difference_type;
|
williamr@2
|
390 |
typedef void* pointer;
|
williamr@2
|
391 |
typedef const void* const_pointer;
|
williamr@2
|
392 |
# if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
williamr@2
|
393 |
typedef void value_type;
|
williamr@2
|
394 |
# endif
|
williamr@2
|
395 |
# if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
|
williamr@2
|
396 |
template <class _Tp1> struct rebind {
|
williamr@2
|
397 |
typedef allocator<_Tp1> other;
|
williamr@2
|
398 |
};
|
williamr@2
|
399 |
# endif
|
williamr@2
|
400 |
# if defined(__MRC__)||(defined(__SC__)&&!defined(__DMC__)) //*ty 03/24/2001 - MPW compilers get confused on these operator definitions
|
williamr@2
|
401 |
template <class _T2> bool operator==(const allocator<_T2>&) const _STLP_NOTHROW { return true; }
|
williamr@2
|
402 |
template <class _T2> bool operator!=(const allocator<_T2>&) const _STLP_NOTHROW { return false; }
|
williamr@2
|
403 |
# endif
|
williamr@2
|
404 |
};
|
williamr@2
|
405 |
|
williamr@2
|
406 |
#if !(defined(__MRC__)||(defined(__SC__)&&!defined(__DMC__))) //*ty 03/24/2001 - MPW compilers get confused on these operator definitions
|
williamr@2
|
407 |
template <class _T1, class _T2> inline bool _STLP_CALL operator==(const allocator<_T1>&, const allocator<_T2>&) _STLP_NOTHROW { return true; }
|
williamr@2
|
408 |
template <class _T1, class _T2> inline bool _STLP_CALL operator!=(const allocator<_T1>&, const allocator<_T2>&) _STLP_NOTHROW { return false; }
|
williamr@2
|
409 |
#endif
|
williamr@2
|
410 |
|
williamr@2
|
411 |
# if defined (_STLP_USE_TEMPLATE_EXPORT)
|
williamr@2
|
412 |
_STLP_EXPORT_TEMPLATE_CLASS allocator<char>;
|
williamr@2
|
413 |
# if defined (_STLP_HAS_WCHAR_T)
|
williamr@2
|
414 |
_STLP_EXPORT_TEMPLATE_CLASS allocator<wchar_t>;
|
williamr@2
|
415 |
# endif
|
williamr@2
|
416 |
# endif /* _STLP_USE_TEMPLATE_EXPORT */
|
williamr@2
|
417 |
|
williamr@2
|
418 |
// Another allocator adaptor: _Alloc_traits. This serves two
|
williamr@2
|
419 |
// purposes. First, make it possible to write containers that can use
|
williamr@2
|
420 |
// either SGI-style allocators or standard-conforming allocator.
|
williamr@2
|
421 |
|
williamr@2
|
422 |
// The fully general version.
|
williamr@2
|
423 |
template <class _Tp, class _Allocator>
|
williamr@2
|
424 |
struct _Alloc_traits
|
williamr@2
|
425 |
{
|
williamr@2
|
426 |
typedef _Allocator _Orig;
|
williamr@2
|
427 |
# if defined (_STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM)
|
williamr@2
|
428 |
typedef typename _Allocator::_STLP_TEMPLATE rebind<_Tp> _Rebind_type;
|
williamr@2
|
429 |
typedef typename _Rebind_type::other allocator_type;
|
williamr@2
|
430 |
static allocator_type create_allocator(const _Orig& __a) { return allocator_type(__a); }
|
williamr@2
|
431 |
# else
|
williamr@2
|
432 |
// this is not actually true, used only to pass this type through
|
williamr@2
|
433 |
// to dynamic overload selection in _STLP_alloc_proxy methods
|
williamr@2
|
434 |
typedef _Allocator allocator_type;
|
williamr@2
|
435 |
# endif /* _STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM */
|
williamr@2
|
436 |
};
|
williamr@2
|
437 |
|
williamr@2
|
438 |
#ifndef _STLP_FORCE_ALLOCATORS
|
williamr@2
|
439 |
#define _STLP_FORCE_ALLOCATORS(a,y)
|
williamr@2
|
440 |
#endif
|
williamr@2
|
441 |
|
williamr@2
|
442 |
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && ! defined (_STLP_MEMBER_TEMPLATE_CLASSES)
|
williamr@2
|
443 |
// The version for the default allocator, for rare occasion when we have partial spec w/o member template classes
|
williamr@2
|
444 |
template <class _Tp, class _Tp1>
|
williamr@2
|
445 |
struct _Alloc_traits<_Tp, allocator<_Tp1> > {
|
williamr@2
|
446 |
typedef allocator<_Tp1> _Orig;
|
williamr@2
|
447 |
typedef allocator<_Tp> allocator_type;
|
williamr@2
|
448 |
static allocator_type create_allocator(const allocator<_Tp1 >& __a) { return allocator_type(__a); }
|
williamr@2
|
449 |
};
|
williamr@2
|
450 |
#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
|
williamr@2
|
451 |
|
williamr@2
|
452 |
/* macro to convert the allocator for initialization
|
williamr@2
|
453 |
* not using MEMBER_TEMPLATE_CLASSES as it should work given template constructor */
|
williamr@2
|
454 |
#if defined (_STLP_MEMBER_TEMPLATES) || ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
williamr@2
|
455 |
/* if _STLP_NO_TEMPLATE_CONVERSIONS is set, the member template constructor is
|
williamr@2
|
456 |
* not used implicitly to convert allocator parameter, so let us do it explicitly */
|
williamr@2
|
457 |
# if defined (_STLP_MEMBER_TEMPLATE_CLASSES) && defined (_STLP_NO_TEMPLATE_CONVERSIONS)
|
williamr@2
|
458 |
# define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)
|
williamr@2
|
459 |
# else
|
williamr@2
|
460 |
# define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __a
|
williamr@2
|
461 |
# endif
|
williamr@2
|
462 |
/* else convert, but only if partial specialization works, since else
|
williamr@2
|
463 |
* Container::allocator_type won't be different */
|
williamr@2
|
464 |
#else
|
williamr@2
|
465 |
# define _STLP_CONVERT_ALLOCATOR(__a, _Tp) __stl_alloc_create(__a,(_Tp*)0)
|
williamr@2
|
466 |
#endif
|
williamr@2
|
467 |
|
williamr@2
|
468 |
# if defined (_STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM)
|
williamr@2
|
469 |
template <class _Tp, class _Alloc>
|
williamr@2
|
470 |
inline _STLP_TYPENAME_ON_RETURN_TYPE _Alloc_traits<_Tp, _Alloc>::allocator_type _STLP_CALL
|
williamr@2
|
471 |
__stl_alloc_create(const _Alloc& __a, const _Tp*) {
|
williamr@2
|
472 |
typedef typename _Alloc::_STLP_TEMPLATE rebind<_Tp>::other _Rebound_type;
|
williamr@2
|
473 |
return _Rebound_type(__a);
|
williamr@2
|
474 |
}
|
williamr@2
|
475 |
#else
|
williamr@2
|
476 |
// If custom allocators are being used without member template classes support :
|
williamr@2
|
477 |
// user (on purpose) is forced to define rebind/get operations !!!
|
williamr@2
|
478 |
template <class _Tp1, class _Tp2>
|
williamr@2
|
479 |
inline allocator<_Tp2>& _STLP_CALL
|
williamr@2
|
480 |
__stl_alloc_rebind(allocator<_Tp1>& __a, const _Tp2*) { return (allocator<_Tp2>&)(__a); }
|
williamr@2
|
481 |
template <class _Tp1, class _Tp2>
|
williamr@2
|
482 |
inline allocator<_Tp2> _STLP_CALL
|
williamr@2
|
483 |
__stl_alloc_create(const allocator<_Tp1>&, const _Tp2*) { return allocator<_Tp2>(); }
|
williamr@2
|
484 |
#endif /* _STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM */
|
williamr@2
|
485 |
|
williamr@2
|
486 |
# ifdef _STLP_USE_RAW_SGI_ALLOCATORS
|
williamr@2
|
487 |
// move obsolete stuff out of the way
|
williamr@2
|
488 |
# include <stl/_alloc_old.h>
|
williamr@2
|
489 |
# endif
|
williamr@2
|
490 |
|
williamr@2
|
491 |
// inheritance is being used for EBO optimization
|
williamr@2
|
492 |
template <class _Value, class _Tp, class _MaybeReboundAlloc>
|
williamr@2
|
493 |
class _STLP_alloc_proxy : public _MaybeReboundAlloc {
|
williamr@2
|
494 |
private:
|
williamr@2
|
495 |
typedef _MaybeReboundAlloc _Base;
|
williamr@2
|
496 |
typedef _STLP_alloc_proxy<_Value, _Tp, _MaybeReboundAlloc> _Self;
|
williamr@2
|
497 |
public:
|
williamr@2
|
498 |
_Value _M_data;
|
williamr@2
|
499 |
inline _STLP_alloc_proxy(const _MaybeReboundAlloc& __a, _Value __p) : _MaybeReboundAlloc(__a), _M_data(__p) {}
|
williamr@2
|
500 |
|
williamr@2
|
501 |
# if 0
|
williamr@2
|
502 |
inline _STLP_alloc_proxy(const _Self& __x) : _MaybeReboundAlloc(__x), _M_data(__x._M_data) {}
|
williamr@2
|
503 |
// construction/destruction
|
williamr@2
|
504 |
inline _Self& operator = (const _Self& __x) {
|
williamr@2
|
505 |
*(_MaybeReboundAlloc*)this = *(_MaybeReboundAlloc*)__x;
|
williamr@2
|
506 |
_M_data = __x._M_data; return *this;
|
williamr@2
|
507 |
}
|
williamr@2
|
508 |
inline _Self& operator = (const _Base& __x) { ((_Base&)*this) = __x; return *this; }
|
williamr@2
|
509 |
# endif
|
williamr@2
|
510 |
// Unified interface to perform allocate()/deallocate() with limited
|
williamr@2
|
511 |
// language support
|
williamr@2
|
512 |
#if ! defined (_STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM)
|
williamr@2
|
513 |
// else it is rebound already, and allocate() member is accessible
|
williamr@2
|
514 |
inline _Tp* allocate(size_t __n) {
|
williamr@2
|
515 |
return __stl_alloc_rebind(__STATIC_CAST(_Base&,*this),(_Tp*)0).allocate(__n,0);
|
williamr@2
|
516 |
}
|
williamr@2
|
517 |
inline void deallocate(_Tp* __p, size_t __n) {
|
williamr@2
|
518 |
__stl_alloc_rebind(__STATIC_CAST(_Base&, *this),(_Tp*)0).deallocate(__p, __n);
|
williamr@2
|
519 |
}
|
williamr@2
|
520 |
#endif /* !_STLP_USE_NESTED_TCLASS_THROUGHT_TPARAM */
|
williamr@2
|
521 |
};
|
williamr@2
|
522 |
|
williamr@2
|
523 |
# if defined (_STLP_USE_TEMPLATE_EXPORT)
|
williamr@2
|
524 |
_STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<char *,char,allocator<char> >;
|
williamr@2
|
525 |
# if defined (_STLP_HAS_WCHAR_T)
|
williamr@2
|
526 |
_STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<wchar_t *,wchar_t,allocator<wchar_t> >;
|
williamr@2
|
527 |
# endif
|
williamr@2
|
528 |
# endif /* _STLP_USE_TEMPLATE_EXPORT */
|
williamr@2
|
529 |
|
williamr@2
|
530 |
# undef _STLP_NODE_ALLOCATOR_THREADS
|
williamr@2
|
531 |
|
williamr@2
|
532 |
_STLP_END_NAMESPACE
|
williamr@2
|
533 |
|
williamr@2
|
534 |
# if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
|
williamr@2
|
535 |
# include <stl/_alloc.c>
|
williamr@2
|
536 |
# endif
|
williamr@2
|
537 |
|
williamr@2
|
538 |
#endif /* _STLP_INTERNAL_ALLOC_H */
|
williamr@2
|
539 |
|
williamr@2
|
540 |
// Local Variables:
|
williamr@2
|
541 |
// mode:C++
|
williamr@2
|
542 |
// End:
|
williamr@2
|
543 |
|