1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Copyright (c) 1996,1997
* Silicon Graphics Computer Systems, Inc.
*
* Copyright (c) 1997
* Moscow Center for SPARC Technology
*
* Copyright (c) 1999
* Boris Fomitchev
*
* This material is provided "as is", with absolutely no warranty expressed
* or implied. Any use is at your own risk.
*
* Permission to use or copy this software for any purpose is hereby granted
* without fee, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*/
#ifndef _STLP_PTHREAD_ALLOC_H
#define _STLP_PTHREAD_ALLOC_H
/*
* Pthread-specific node allocator.
* This is similar to the default allocator, except that free-list
* information is kept separately for each thread, avoiding locking.
* This should be reasonably fast even in the presence of threads.
* The down side is that storage may not be well-utilized.
* It is not an error to allocate memory in thread A and deallocate
* it in thread B. But this effectively transfers ownership of the memory,
* so that it can only be reallocated by thread B. Thus this can effectively
* result in a storage leak if it's done on a regular basis.
* It can also result in frequent sharing of
* cache lines among processors, with potentially serious performance
* consequences.
*/
#if !defined (_STLP_PTHREADS)
# error POSIX specific allocator implementation. Your system do not seems to \
have this interface so please comment the _STLP_USE_PERTHREAD_ALLOC macro \
or report to the STLport forum.
#endif
#if defined (_STLP_USE_NO_IOSTREAMS)
# error You cannot use per thread allocator implementation without building \
STLport libraries.
#endif
#ifndef _STLP_INTERNAL_ALLOC_H
# include <stl/_alloc.h>
#endif
_STLP_BEGIN_NAMESPACE
_STLP_MOVE_TO_PRIV_NAMESPACE
struct _Pthread_alloc_per_thread_state;
// Pthread-specific allocator.
class _STLP_CLASS_DECLSPEC _Pthread_alloc {
public: // but only for internal use:
typedef _Pthread_alloc_per_thread_state __state_type;
typedef char value_type;
public:
// Return a recycled or new per thread state.
static __state_type * _STLP_CALL _S_get_per_thread_state();
/* n must be > 0 */
static void * _STLP_CALL allocate(size_t& __n);
/* p may not be 0 */
static void _STLP_CALL deallocate(void *__p, size_t __n);
// boris : versions for per_thread_allocator
/* n must be > 0 */
static void * _STLP_CALL allocate(size_t& __n, __state_type* __a);
/* p may not be 0 */
static void _STLP_CALL deallocate(void *__p, size_t __n, __state_type* __a);
static void * _STLP_CALL reallocate(void *__p, size_t __old_sz, size_t& __new_sz);
};
_STLP_MOVE_TO_STD_NAMESPACE
typedef _STLP_PRIV _Pthread_alloc __pthread_alloc;
typedef __pthread_alloc pthread_alloc;
template <class _Tp>
class pthread_allocator : public __stlport_class<pthread_allocator<_Tp> > {
typedef pthread_alloc _S_Alloc; // The underlying allocator.
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef const _Tp* const_pointer;
typedef _Tp& reference;
typedef const _Tp& const_reference;
typedef _Tp value_type;
#ifdef _STLP_MEMBER_TEMPLATE_CLASSES
template <class _NewType> struct rebind {
typedef pthread_allocator<_NewType> other;
};
#endif
pthread_allocator() _STLP_NOTHROW {}
pthread_allocator(const pthread_allocator<_Tp>& a) _STLP_NOTHROW {}
#if defined (_STLP_MEMBER_TEMPLATES) /* && defined (_STLP_FUNCTION_PARTIAL_ORDER) */
template <class _OtherType> pthread_allocator(const pthread_allocator<_OtherType>&)
_STLP_NOTHROW {}
#endif
~pthread_allocator() _STLP_NOTHROW {}
pointer address(reference __x) const { return &__x; }
const_pointer address(const_reference __x) const { return &__x; }
// __n is permitted to be 0. The C++ standard says nothing about what
// the return value is when __n == 0.
_Tp* allocate(size_type __n, const void* = 0) {
if (__n > max_size()) {
__THROW_BAD_ALLOC;
}
if (__n != 0) {
size_type __buf_size = __n * sizeof(value_type);
_Tp* __ret = __REINTERPRET_CAST(value_type*, _S_Alloc::allocate(__buf_size));
#if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
if (__ret != 0) {
memset((char*)__ret, _STLP_SHRED_BYTE, __buf_size);
}
#endif
return __ret;
}
else
return 0;
}
void deallocate(pointer __p, size_type __n) {
_STLP_ASSERT( (__p == 0) == (__n == 0) )
if (__p != 0) {
#if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
memset((char*)__p, _STLP_SHRED_BYTE, __n * sizeof(value_type));
#endif
_S_Alloc::deallocate(__p, __n * sizeof(value_type));
}
}
size_type max_size() const _STLP_NOTHROW
{ return size_t(-1) / sizeof(_Tp); }
void construct(pointer __p, const _Tp& __val) { _STLP_PLACEMENT_NEW (__p) _Tp(__val); }
void destroy(pointer _p) { _p->~_Tp(); }
#if defined (_STLP_NO_EXTENSIONS)
/* STLport extension giving rounded size of an allocated memory buffer
* This method do not have to be part of a user defined allocator implementation
* and won't even be called if such a function was granted.
*/
protected:
#endif
_Tp* allocate(size_type __n, size_type& __allocated_n) {
if (__n > max_size()) {
__THROW_BAD_ALLOC;
}
if (__n != 0) {
size_type __buf_size = __n * sizeof(value_type);
_Tp* __ret = __REINTERPRET_CAST(value_type*, _S_Alloc::allocate(__buf_size));
#if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
if (__ret != 0) {
memset((char*)__ret, _STLP_SHRED_BYTE, __buf_size);
}
#endif
__allocated_n = __buf_size / sizeof(value_type);
return __ret;
}
else
return 0;
}
};
_STLP_TEMPLATE_NULL
class _STLP_CLASS_DECLSPEC pthread_allocator<void> {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
#ifdef _STLP_MEMBER_TEMPLATE_CLASSES
template <class _NewType> struct rebind {
typedef pthread_allocator<_NewType> other;
};
#endif
};
template <class _T1, class _T2>
inline bool operator==(const pthread_allocator<_T1>&,
const pthread_allocator<_T2>& a2)
{ return true; }
#ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
template <class _T1, class _T2>
inline bool operator!=(const pthread_allocator<_T1>&,
const pthread_allocator<_T2>&)
{ return false; }
#endif
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
# if defined (_STLP_USE_RAW_SGI_ALLOCATORS)
template <class _Tp>
struct _Alloc_traits<_Tp, _Pthread_alloc>
{ typedef __allocator<_Tp, _Pthread_alloc> allocator_type; };
# endif
template <class _Tp, class _Atype>
struct _Alloc_traits<_Tp, pthread_allocator<_Atype> >
{ typedef pthread_allocator<_Tp> allocator_type; };
#endif
#if defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE)
template <class _Tp1, class _Tp2>
inline pthread_allocator<_Tp2>&
__stl_alloc_rebind(pthread_allocator<_Tp1>& __x, const _Tp2*)
{ return (pthread_allocator<_Tp2>&)__x; }
template <class _Tp1, class _Tp2>
inline pthread_allocator<_Tp2>
__stl_alloc_create(pthread_allocator<_Tp1>&, const _Tp2*)
{ return pthread_allocator<_Tp2>(); }
#endif
_STLP_MOVE_TO_PRIV_NAMESPACE
template <class _Tp>
struct __pthread_alloc_type_traits {
typedef typename _IsSTLportClass<pthread_allocator<_Tp> >::_Ret _STLportAlloc;
//The default allocator implementation which is recognize thanks to the
//__stlport_class inheritance is a stateless object so:
typedef _STLportAlloc has_trivial_default_constructor;
typedef _STLportAlloc has_trivial_copy_constructor;
typedef _STLportAlloc has_trivial_assignment_operator;
typedef _STLportAlloc has_trivial_destructor;
typedef _STLportAlloc is_POD_type;
};
_STLP_MOVE_TO_STD_NAMESPACE
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
template <class _Tp>
struct __type_traits<pthread_allocator<_Tp> > : _STLP_PRIV __pthread_alloc_type_traits<_Tp> {};
#else
_STLP_TEMPLATE_NULL
struct __type_traits<pthread_allocator<char> > : _STLP_PRIV __pthread_alloc_type_traits<char> {};
# if defined (_STLP_HAS_WCHAR_T)
_STLP_TEMPLATE_NULL
struct __type_traits<pthread_allocator<wchar_t> > : _STLP_PRIV __pthread_alloc_type_traits<wchar_t> {};
# endif
# if defined (_STLP_USE_PTR_SPECIALIZATIONS)
_STLP_TEMPLATE_NULL
struct __type_traits<pthread_allocator<void*> > : _STLP_PRIV __pthread_alloc_type_traits<void*> {};
# endif
#endif
//
// per_thread_allocator<> : this allocator always return memory to the same thread
// it was allocated from.
//
template <class _Tp>
class per_thread_allocator {
typedef pthread_alloc _S_Alloc; // The underlying allocator.
typedef pthread_alloc::__state_type __state_type;
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef const _Tp* const_pointer;
typedef _Tp& reference;
typedef const _Tp& const_reference;
typedef _Tp value_type;
#ifdef _STLP_MEMBER_TEMPLATE_CLASSES
template <class _NewType> struct rebind {
typedef per_thread_allocator<_NewType> other;
};
#endif
per_thread_allocator() _STLP_NOTHROW {
_M_state = _S_Alloc::_S_get_per_thread_state();
}
per_thread_allocator(const per_thread_allocator<_Tp>& __a) _STLP_NOTHROW : _M_state(__a._M_state){}
#if defined (_STLP_MEMBER_TEMPLATES) /* && defined (_STLP_FUNCTION_PARTIAL_ORDER) */
template <class _OtherType> per_thread_allocator(const per_thread_allocator<_OtherType>& __a)
_STLP_NOTHROW : _M_state(__a._M_state) {}
#endif
~per_thread_allocator() _STLP_NOTHROW {}
pointer address(reference __x) const { return &__x; }
const_pointer address(const_reference __x) const { return &__x; }
// __n is permitted to be 0. The C++ standard says nothing about what
// the return value is when __n == 0.
_Tp* allocate(size_type __n, const void* = 0) {
if (__n > max_size()) {
__THROW_BAD_ALLOC;
}
if (__n != 0) {
size_type __buf_size = __n * sizeof(value_type);
_Tp* __ret = __REINTERPRET_CAST(_Tp*, _S_Alloc::allocate(__buf_size, _M_state));
#if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
if (__ret != 0) {
memset((char*)__ret, _STLP_SHRED_BYTE, __buf_size);
}
#endif
return __ret;
}
else
return 0;
}
void deallocate(pointer __p, size_type __n) {
_STLP_ASSERT( (__p == 0) == (__n == 0) )
if (__p != 0) {
#if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
memset((char*)__p, _STLP_SHRED_BYTE, __n * sizeof(value_type));
#endif
_S_Alloc::deallocate(__p, __n * sizeof(value_type), _M_state);
}
}
size_type max_size() const _STLP_NOTHROW
{ return size_t(-1) / sizeof(_Tp); }
void construct(pointer __p, const _Tp& __val) { _STLP_PLACEMENT_NEW (__p) _Tp(__val); }
void destroy(pointer _p) { _p->~_Tp(); }
// state is being kept here
__state_type* _M_state;
#if defined (_STLP_NO_EXTENSIONS)
/* STLport extension giving rounded size of an allocated memory buffer
* This method do not have to be part of a user defined allocator implementation
* and won't even be called if such a function was granted.
*/
protected:
#endif
_Tp* allocate(size_type __n, size_type& __allocated_n) {
if (__n > max_size()) {
__THROW_BAD_ALLOC;
}
if (__n != 0) {
size_type __buf_size = __n * sizeof(value_type);
_Tp* __ret = __REINTERPRET_CAST(value_type*, _S_Alloc::allocate(__buf_size, _M_state));
#if defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_DEBUG_ALLOC)
if (__ret != 0) {
memset((char*)__ret, _STLP_SHRED_BYTE, __buf_size);
}
#endif
__allocated_n = __buf_size / sizeof(value_type);
return __ret;
}
else
return 0;
}
};
_STLP_TEMPLATE_NULL
class _STLP_CLASS_DECLSPEC per_thread_allocator<void> {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
#ifdef _STLP_MEMBER_TEMPLATE_CLASSES
template <class _NewType> struct rebind {
typedef per_thread_allocator<_NewType> other;
};
#endif
};
template <class _T1, class _T2>
inline bool operator==(const per_thread_allocator<_T1>& __a1,
const per_thread_allocator<_T2>& __a2)
{ return __a1._M_state == __a2._M_state; }
#ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
template <class _T1, class _T2>
inline bool operator!=(const per_thread_allocator<_T1>& __a1,
const per_thread_allocator<_T2>& __a2)
{ return __a1._M_state != __a2._M_state; }
#endif
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
template <class _Tp, class _Atype>
struct _Alloc_traits<_Tp, per_thread_allocator<_Atype> >
{ typedef per_thread_allocator<_Tp> allocator_type; };
#endif
#if defined (_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE)
template <class _Tp1, class _Tp2>
inline per_thread_allocator<_Tp2>&
__stl_alloc_rebind(per_thread_allocator<_Tp1>& __x, const _Tp2*)
{ return (per_thread_allocator<_Tp2>&)__x; }
template <class _Tp1, class _Tp2>
inline per_thread_allocator<_Tp2>
__stl_alloc_create(per_thread_allocator<_Tp1>&, const _Tp2*)
{ return per_thread_allocator<_Tp2>(); }
#endif /* _STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE */
_STLP_MOVE_TO_PRIV_NAMESPACE
template <class _Tp>
struct __perthread_alloc_type_traits {
typedef typename _IsSTLportClass<per_thread_allocator<_Tp> >::_Ret _STLportAlloc;
//The default allocator implementation which is recognize thanks to the
//__stlport_class inheritance is a stateless object so:
typedef __false_type has_trivial_default_constructor;
typedef _STLportAlloc has_trivial_copy_constructor;
typedef _STLportAlloc has_trivial_assignment_operator;
typedef _STLportAlloc has_trivial_destructor;
typedef __false_type is_POD_type;
};
_STLP_MOVE_TO_STD_NAMESPACE
#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
template <class _Tp>
struct __type_traits<per_thread_allocator<_Tp> > : _STLP_PRIV __perthread_alloc_type_traits<_Tp> {};
#else
_STLP_TEMPLATE_NULL
struct __type_traits<per_thread_allocator<char> > : _STLP_PRIV __perthread_alloc_type_traits<char> {};
# if defined (_STLP_HAS_WCHAR_T)
_STLP_TEMPLATE_NULL
struct __type_traits<per_thread_allocator<wchar_t> > : _STLP_PRIV __perthread_alloc_type_traits<wchar_t> {};
# endif
# if defined (_STLP_USE_PTR_SPECIALIZATIONS)
_STLP_TEMPLATE_NULL
struct __type_traits<per_thread_allocator<void*> > : _STLP_PRIV __perthread_alloc_type_traits<void*> {};
# endif
#endif
_STLP_END_NAMESPACE
#endif /* _STLP_PTHREAD_ALLOC */
// Local Variables:
// mode:C++
// End:
|