| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (c) 1996,1997 |
| 4 |
* Silicon Graphics Computer Systems, Inc. |
| 5 |
* |
| 6 |
* Copyright (c) 1997 |
| 7 |
* Moscow Center for SPARC Technology |
| 8 |
* |
| 9 |
* Copyright (c) 1999 |
| 10 |
* Boris Fomitchev |
| 11 |
* |
| 12 |
* This material is provided "as is", with absolutely no warranty expressed |
| 13 |
* or implied. Any use is at your own risk. |
| 14 |
* |
| 15 |
* Permission to use or copy this software for any purpose is hereby granted |
| 16 |
* without fee, provided the above notices are retained on all copies. |
| 17 |
* Permission to modify the code and to distribute modified code is granted, |
| 18 |
* provided the above notices are retained, and a notice that the code was |
| 19 |
* modified is included with the above copyright notice. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "stlport_prefix.h" |
| 24 |
|
| 25 |
#include <memory> |
| 26 |
|
| 27 |
#if defined (__GNUC__) && (defined (__CYGWIN__) || defined (__MINGW32__)) |
| 28 |
# include <malloc.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#if defined (_STLP_PTHREADS) && !defined (_STLP_NO_THREADS) |
| 32 |
# include <pthread_alloc> |
| 33 |
# include <cerrno> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <stl/_threads.h> |
| 37 |
|
| 38 |
#include "lock_free_slist.h" |
| 39 |
|
| 40 |
#if defined (__WATCOMC__) |
| 41 |
# pragma warning 13 9 |
| 42 |
# pragma warning 367 9 |
| 43 |
# pragma warning 368 9 |
| 44 |
#endif |
| 45 |
|
| 46 |
#if defined (_STLP_SGI_THREADS) |
| 47 |
// We test whether threads are in use before locking. |
| 48 |
// Perhaps this should be moved into stl_threads.h, but that |
| 49 |
// probably makes it harder to avoid the procedure call when |
| 50 |
// it isn't needed. |
| 51 |
extern "C" { |
| 52 |
extern int __us_rsthread_malloc; |
| 53 |
} |
| 54 |
#endif |
| 55 |
|
| 56 |
_STLP_BEGIN_NAMESPACE |
| 57 |
|
| 58 |
void *align( _STLP_STD::size_t alignment, _STLP_STD::size_t size, void*& ptr, _STLP_STD::size_t& space ) |
| 59 |
{ |
| 60 |
void* tmp = reinterpret_cast<void*>( reinterpret_cast<intptr_t>((reinterpret_cast<char*>(ptr) + alignment - 1)) & ~(alignment - 1) ); |
| 61 |
|
| 62 |
if ( (reinterpret_cast<char*>(tmp) + size) <= (reinterpret_cast<char*>(ptr) + space) ) { |
| 63 |
if ( ptr != tmp ) { |
| 64 |
space -= reinterpret_cast<char*>(tmp) - reinterpret_cast<char*>(ptr); |
| 65 |
ptr = tmp; |
| 66 |
} |
| 67 |
return ptr; |
| 68 |
} |
| 69 |
|
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
_STLP_END_NAMESPACE |
| 74 |
|
| 75 |
// Specialised debug form of new operator which does not provide "false" |
| 76 |
// memory leaks when run with debug CRT libraries. |
| 77 |
#if defined (_STLP_MSVC) && (_STLP_MSVC >= 1020 && defined (_STLP_DEBUG_ALLOC)) && !defined (_STLP_WCE) |
| 78 |
# include <crtdbg.h> |
| 79 |
inline void* __stlp_new_void_chunk(size_t __bytes) { |
| 80 |
_STLP_CHECK_NULL_ALLOC(::operator new(__bytes, _CRT_BLOCK, __FILE__, __LINE__)); |
| 81 |
} |
| 82 |
inline char* __stlp_new_chunk(size_t __bytes) { |
| 83 |
return __STATIC_CAST(char*, __stlp_new_void_chunk(__bytes)); |
| 84 |
} |
| 85 |
inline void __stlp_delete_chunck(void* __p) { ::operator delete(__p, _CRT_BLOCK, __FILE__, __LINE__); } |
| 86 |
#else |
| 87 |
# ifdef _STLP_NODE_ALLOC_USE_MALLOC |
| 88 |
# include <cstdlib> |
| 89 |
inline char* __stlp_new_chunk(size_t __bytes) { |
| 90 |
// do not use _STLP_CHECK_NULL_ALLOC, this macro is dedicated to new operator. |
| 91 |
void *__chunk = _STLP_VENDOR_CSTD::malloc(__bytes); |
| 92 |
if (__chunk == 0) { |
| 93 |
_STLP_THROW_BAD_ALLOC; |
| 94 |
} |
| 95 |
return __STATIC_CAST(char*, __chunk); |
| 96 |
} |
| 97 |
inline void __stlp_delete_chunck(void* __p) { _STLP_VENDOR_CSTD::free(__p); } |
| 98 |
# else |
| 99 |
inline char* __stlp_new_chunk(size_t __bytes) |
| 100 |
{ return __STATIC_CAST(char*, _STLP_STD::__stl_new(__bytes)); } |
| 101 |
inline void __stlp_delete_chunck(void* __p) { _STLP_STD::__stl_delete(__p); } |
| 102 |
# endif |
| 103 |
#endif |
| 104 |
|
| 105 |
/* This is an additional atomic operations to the ones already defined in |
| 106 |
* stl/_threads.h, platform should try to support it to improve performance. |
| 107 |
* __add_atomic_t _STLP_ATOMIC_ADD(volatile __add_atomic_t* __target, __add_atomic_t __val) : |
| 108 |
* does *__target = *__target + __val and returns the old *__target value */ |
| 109 |
typedef long __add_atomic_t; |
| 110 |
typedef unsigned long __uadd_atomic_t; |
| 111 |
|
| 112 |
#if defined (__GNUC__) && defined (__i386__) |
| 113 |
inline long _STLP_atomic_add_gcc_x86(long volatile* p, long addend) { |
| 114 |
long result; |
| 115 |
__asm__ __volatile__ |
| 116 |
("lock; xaddl %1, %0;" |
| 117 |
:"=m" (*p), "=r" (result) |
| 118 |
:"m" (*p), "1" (addend) |
| 119 |
:"cc"); |
| 120 |
return result + addend; |
| 121 |
} |
| 122 |
# define _STLP_ATOMIC_ADD(__dst, __val) _STLP_atomic_add_gcc_x86(__dst, __val) |
| 123 |
#elif defined (_STLP_WIN32THREADS) |
| 124 |
// The Win32 API function InterlockedExchangeAdd is not available on Windows 95. |
| 125 |
# if !defined (_STLP_WIN95_LIKE) |
| 126 |
# if defined (_STLP_NEW_PLATFORM_SDK) |
| 127 |
# define _STLP_ATOMIC_ADD(__dst, __val) InterlockedExchangeAdd(__dst, __val) |
| 128 |
# else |
| 129 |
# define _STLP_ATOMIC_ADD(__dst, __val) InterlockedExchangeAdd(__CONST_CAST(__add_atomic_t*, __dst), __val) |
| 130 |
# endif |
| 131 |
# endif |
| 132 |
#endif |
| 133 |
|
| 134 |
#if defined (__OS400__) |
| 135 |
// dums 02/05/2007: is it really necessary ? |
| 136 |
enum { _ALIGN = 16, _ALIGN_SHIFT = 4 }; |
| 137 |
#else |
| 138 |
enum { _ALIGN = 2 * sizeof(void*), _ALIGN_SHIFT = 2 + sizeof(void*) / 4 }; |
| 139 |
#endif |
| 140 |
|
| 141 |
#define _S_FREELIST_INDEX(__bytes) ((__bytes - size_t(1)) >> (int)_ALIGN_SHIFT) |
| 142 |
|
| 143 |
_STLP_BEGIN_NAMESPACE |
| 144 |
|
| 145 |
// malloc_alloc out-of-memory handling |
| 146 |
static __oom_handler_type __oom_handler = __STATIC_CAST(__oom_handler_type, 0); |
| 147 |
|
| 148 |
#ifdef _STLP_THREADS |
| 149 |
_STLP_mutex __oom_handler_lock; |
| 150 |
#endif |
| 151 |
|
| 152 |
void* _STLP_CALL __malloc_alloc::allocate(size_t __n) |
| 153 |
{ |
| 154 |
void *__result = malloc(__n); |
| 155 |
if ( 0 == __result ) { |
| 156 |
__oom_handler_type __my_malloc_handler; |
| 157 |
|
| 158 |
for (;;) { |
| 159 |
{ |
| 160 |
#ifdef _STLP_THREADS |
| 161 |
_STLP_auto_lock _l( __oom_handler_lock ); |
| 162 |
#endif |
| 163 |
__my_malloc_handler = __oom_handler; |
| 164 |
} |
| 165 |
if ( 0 == __my_malloc_handler) { |
| 166 |
_STLP_THROW_BAD_ALLOC; |
| 167 |
} |
| 168 |
(*__my_malloc_handler)(); |
| 169 |
__result = malloc(__n); |
| 170 |
if ( __result ) |
| 171 |
return __result; |
| 172 |
} |
| 173 |
} |
| 174 |
return __result; |
| 175 |
} |
| 176 |
|
| 177 |
__oom_handler_type _STLP_CALL __malloc_alloc::set_malloc_handler(__oom_handler_type __f) |
| 178 |
{ |
| 179 |
#ifdef _STLP_THREADS |
| 180 |
_STLP_auto_lock _l( __oom_handler_lock ); |
| 181 |
#endif |
| 182 |
__oom_handler_type __old = __oom_handler; |
| 183 |
__oom_handler = __f; |
| 184 |
return __old; |
| 185 |
} |
| 186 |
|
| 187 |
// ******************************************************* |
| 188 |
// Default node allocator. |
| 189 |
// With a reasonable compiler, this should be roughly as fast as the |
| 190 |
// original STL class-specific allocators, but with less fragmentation. |
| 191 |
// |
| 192 |
// Important implementation properties: |
| 193 |
// 1. If the client request an object of size > _MAX_BYTES, the resulting |
| 194 |
// object will be obtained directly from malloc. |
| 195 |
// 2. In all other cases, we allocate an object of size exactly |
| 196 |
// _S_round_up(requested_size). Thus the client has enough size |
| 197 |
// information that we can return the object to the proper free list |
| 198 |
// without permanently losing part of the object. |
| 199 |
// |
| 200 |
|
| 201 |
#define _STLP_NFREELISTS 16 |
| 202 |
|
| 203 |
#if defined (_STLP_LEAKS_PEDANTIC) && defined (_STLP_USE_DYNAMIC_LIB) |
| 204 |
/* |
| 205 |
* We can only do cleanup of the node allocator memory pool if we are |
| 206 |
* sure that the STLport library is used as a shared one as it guaranties |
| 207 |
* the unicity of the node allocator instance. Without that guaranty node |
| 208 |
* allocator instances might exchange memory blocks making the implementation |
| 209 |
* of a cleaning process much more complicated. |
| 210 |
*/ |
| 211 |
# define _STLP_DO_CLEAN_NODE_ALLOC |
| 212 |
#endif |
| 213 |
|
| 214 |
/* When STLport is used without multi threaded safety we use the node allocator |
| 215 |
* implementation with locks as locks becomes no-op. The lock free implementation |
| 216 |
* always use system specific atomic operations which are slower than 'normal' |
| 217 |
* ones. |
| 218 |
*/ |
| 219 |
#if defined (_STLP_THREADS) && \ |
| 220 |
defined (_STLP_HAS_ATOMIC_FREELIST) && defined (_STLP_ATOMIC_ADD) |
| 221 |
/* |
| 222 |
* We have an implementation of the atomic freelist (_STLP_atomic_freelist) |
| 223 |
* for this architecture and compiler. That means we can use the non-blocking |
| 224 |
* implementation of the node-allocation engine.*/ |
| 225 |
# define _STLP_USE_LOCK_FREE_IMPLEMENTATION |
| 226 |
#endif |
| 227 |
|
| 228 |
#if !defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 229 |
# if defined (_STLP_THREADS) |
| 230 |
|
| 231 |
class _Node_Alloc_Lock { |
| 232 |
static _STLP_STATIC_MUTEX& _S_Mutex() { |
| 233 |
static _STLP_STATIC_MUTEX mutex _STLP_MUTEX_INITIALIZER; |
| 234 |
return mutex; |
| 235 |
} |
| 236 |
public: |
| 237 |
_Node_Alloc_Lock() { |
| 238 |
# if defined (_STLP_SGI_THREADS) |
| 239 |
if (__us_rsthread_malloc) |
| 240 |
# endif |
| 241 |
_S_Mutex()._M_acquire_lock(); |
| 242 |
} |
| 243 |
|
| 244 |
~_Node_Alloc_Lock() { |
| 245 |
# if defined (_STLP_SGI_THREADS) |
| 246 |
if (__us_rsthread_malloc) |
| 247 |
# endif |
| 248 |
_S_Mutex()._M_release_lock(); |
| 249 |
} |
| 250 |
}; |
| 251 |
|
| 252 |
# else |
| 253 |
|
| 254 |
class _Node_Alloc_Lock { |
| 255 |
public: |
| 256 |
_Node_Alloc_Lock() { } |
| 257 |
~_Node_Alloc_Lock() { } |
| 258 |
}; |
| 259 |
|
| 260 |
# endif |
| 261 |
|
| 262 |
struct _Node_alloc_obj { |
| 263 |
_Node_alloc_obj * _M_next; |
| 264 |
}; |
| 265 |
#endif |
| 266 |
|
| 267 |
class __node_alloc_impl { |
| 268 |
static inline size_t _STLP_CALL _S_round_up(size_t __bytes) |
| 269 |
{ return (((__bytes) + (size_t)_ALIGN-1) & ~((size_t)_ALIGN - 1)); } |
| 270 |
|
| 271 |
#if defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 272 |
typedef _STLP_atomic_freelist::item _Obj; |
| 273 |
typedef _STLP_atomic_freelist _Freelist; |
| 274 |
typedef _STLP_atomic_freelist _ChunkList; |
| 275 |
|
| 276 |
// Header of blocks of memory that have been allocated as part of |
| 277 |
// a larger chunk but have not yet been chopped up into nodes. |
| 278 |
struct _FreeBlockHeader : public _STLP_atomic_freelist::item { |
| 279 |
char* _M_end; // pointer to end of free memory |
| 280 |
}; |
| 281 |
#else |
| 282 |
typedef _Node_alloc_obj _Obj; |
| 283 |
typedef _Obj* _STLP_VOLATILE _Freelist; |
| 284 |
typedef _Obj* _ChunkList; |
| 285 |
#endif |
| 286 |
|
| 287 |
private: |
| 288 |
// Returns an object of size __n, and optionally adds to size __n free list. |
| 289 |
static _Obj* _S_refill(size_t __n); |
| 290 |
// Allocates a chunk for nobjs of size __p_size. nobjs may be reduced |
| 291 |
// if it is inconvenient to allocate the requested number. |
| 292 |
static char* _S_chunk_alloc(size_t __p_size, int& __nobjs); |
| 293 |
// Chunk allocation state. |
| 294 |
static _Freelist _S_free_list[_STLP_NFREELISTS]; |
| 295 |
// Amount of total allocated memory |
| 296 |
#if defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 297 |
static _STLP_VOLATILE __add_atomic_t _S_heap_size; |
| 298 |
#else |
| 299 |
static size_t _S_heap_size; |
| 300 |
#endif |
| 301 |
|
| 302 |
#if defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 303 |
// List of blocks of free memory |
| 304 |
static _STLP_atomic_freelist _S_free_mem_blocks; |
| 305 |
#else |
| 306 |
// Start of the current free memory buffer |
| 307 |
static char* _S_start_free; |
| 308 |
// End of the current free memory buffer |
| 309 |
static char* _S_end_free; |
| 310 |
#endif |
| 311 |
|
| 312 |
#if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 313 |
public: |
| 314 |
// Methods to report alloc/dealloc calls to the counter system. |
| 315 |
# if defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 316 |
typedef _STLP_VOLATILE __stl_atomic_t _AllocCounter; |
| 317 |
# else |
| 318 |
typedef __stl_atomic_t _AllocCounter; |
| 319 |
# endif |
| 320 |
static _AllocCounter& _STLP_CALL _S_alloc_counter(); |
| 321 |
static void _S_alloc_call(); |
| 322 |
static void _S_dealloc_call(); |
| 323 |
|
| 324 |
private: |
| 325 |
// Free all the allocated chuncks of memory |
| 326 |
static void _S_chunk_dealloc(); |
| 327 |
// Beginning of the linked list of allocated chunks of memory |
| 328 |
static _ChunkList _S_chunks; |
| 329 |
#endif /* _STLP_DO_CLEAN_NODE_ALLOC */ |
| 330 |
|
| 331 |
public: |
| 332 |
/* __n must be > 0 */ |
| 333 |
static void* _M_allocate(size_t& __n); |
| 334 |
/* __p may not be 0 */ |
| 335 |
static void _M_deallocate(void *__p, size_t __n); |
| 336 |
}; |
| 337 |
|
| 338 |
#if !defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 339 |
void* __node_alloc_impl::_M_allocate(size_t& __n) { |
| 340 |
__n = _S_round_up(__n); |
| 341 |
_Obj * _STLP_VOLATILE * __my_free_list = _S_free_list + _S_FREELIST_INDEX(__n); |
| 342 |
_Obj *__r; |
| 343 |
|
| 344 |
// Acquire the lock here with a constructor call. |
| 345 |
// This ensures that it is released in exit or during stack |
| 346 |
// unwinding. |
| 347 |
_Node_Alloc_Lock __lock_instance; |
| 348 |
|
| 349 |
if ( (__r = *__my_free_list) != 0 ) { |
| 350 |
*__my_free_list = __r->_M_next; |
| 351 |
} else { |
| 352 |
__r = _S_refill(__n); |
| 353 |
} |
| 354 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 355 |
_S_alloc_call(); |
| 356 |
# endif |
| 357 |
// lock is released here |
| 358 |
return __r; |
| 359 |
} |
| 360 |
|
| 361 |
void __node_alloc_impl::_M_deallocate(void *__p, size_t __n) { |
| 362 |
_Obj * _STLP_VOLATILE * __my_free_list = _S_free_list + _S_FREELIST_INDEX(__n); |
| 363 |
_Obj * __pobj = __STATIC_CAST(_Obj*, __p); |
| 364 |
|
| 365 |
// acquire lock |
| 366 |
_Node_Alloc_Lock __lock_instance; |
| 367 |
__pobj->_M_next = *__my_free_list; |
| 368 |
*__my_free_list = __pobj; |
| 369 |
|
| 370 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 371 |
_S_dealloc_call(); |
| 372 |
# endif |
| 373 |
// lock is released here |
| 374 |
} |
| 375 |
|
| 376 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 377 |
# define _STLP_OFFSET sizeof(_Obj) |
| 378 |
# else |
| 379 |
# define _STLP_OFFSET 0 |
| 380 |
# endif |
| 381 |
|
| 382 |
/* We allocate memory in large chunks in order to avoid fragmenting */ |
| 383 |
/* the malloc heap too much. */ |
| 384 |
/* We assume that size is properly aligned. */ |
| 385 |
/* We hold the allocation lock. */ |
| 386 |
char* __node_alloc_impl::_S_chunk_alloc(size_t _p_size, int& __nobjs) { |
| 387 |
char* __result; |
| 388 |
size_t __total_bytes = _p_size * __nobjs; |
| 389 |
size_t __bytes_left = _S_end_free - _S_start_free; |
| 390 |
|
| 391 |
if (__bytes_left > 0) { |
| 392 |
if (__bytes_left >= __total_bytes) { |
| 393 |
__result = _S_start_free; |
| 394 |
_S_start_free += __total_bytes; |
| 395 |
return __result; |
| 396 |
} |
| 397 |
|
| 398 |
if (__bytes_left >= _p_size) { |
| 399 |
__nobjs = (int)(__bytes_left / _p_size); |
| 400 |
__total_bytes = _p_size * __nobjs; |
| 401 |
__result = _S_start_free; |
| 402 |
_S_start_free += __total_bytes; |
| 403 |
return __result; |
| 404 |
} |
| 405 |
|
| 406 |
// Try to make use of the left-over piece. |
| 407 |
_Obj* _STLP_VOLATILE* __my_free_list = _S_free_list + _S_FREELIST_INDEX(__bytes_left); |
| 408 |
__REINTERPRET_CAST(_Obj*, _S_start_free)->_M_next = *__my_free_list; |
| 409 |
*__my_free_list = __REINTERPRET_CAST(_Obj*, _S_start_free); |
| 410 |
_S_start_free = _S_end_free = 0; |
| 411 |
} |
| 412 |
|
| 413 |
size_t __bytes_to_get = 2 * __total_bytes + _S_round_up(_S_heap_size) + _STLP_OFFSET; |
| 414 |
|
| 415 |
_STLP_TRY { |
| 416 |
_S_start_free = __stlp_new_chunk(__bytes_to_get); |
| 417 |
} |
| 418 |
#if defined (_STLP_USE_EXCEPTIONS) |
| 419 |
catch (const _STLP_STD::bad_alloc&) { |
| 420 |
_Obj* _STLP_VOLATILE* __my_free_list; |
| 421 |
_Obj* __p; |
| 422 |
// Try to do with what we have. That can't hurt. |
| 423 |
// We do not try smaller requests, since that tends |
| 424 |
// to result in disaster on multi-process machines. |
| 425 |
for (size_t __i = _p_size; __i <= (size_t)_MAX_BYTES; __i += (size_t)_ALIGN) { |
| 426 |
__my_free_list = _S_free_list + _S_FREELIST_INDEX(__i); |
| 427 |
__p = *__my_free_list; |
| 428 |
if (0 != __p) { |
| 429 |
*__my_free_list = __p -> _M_next; |
| 430 |
_S_start_free = __REINTERPRET_CAST(char*, __p); |
| 431 |
_S_end_free = _S_start_free + __i; |
| 432 |
return _S_chunk_alloc(_p_size, __nobjs); |
| 433 |
// Any leftover piece will eventually make it to the |
| 434 |
// right free list. |
| 435 |
} |
| 436 |
} |
| 437 |
__bytes_to_get = __total_bytes + _STLP_OFFSET; |
| 438 |
_S_start_free = __stlp_new_chunk(__bytes_to_get); |
| 439 |
} |
| 440 |
#endif |
| 441 |
|
| 442 |
_S_heap_size += __bytes_to_get >> 4; |
| 443 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 444 |
__REINTERPRET_CAST(_Obj*, _S_start_free)->_M_next = _S_chunks; |
| 445 |
_S_chunks = __REINTERPRET_CAST(_Obj*, _S_start_free); |
| 446 |
# endif |
| 447 |
_S_end_free = _S_start_free + __bytes_to_get; |
| 448 |
_S_start_free += _STLP_OFFSET; |
| 449 |
return _S_chunk_alloc(_p_size, __nobjs); |
| 450 |
} |
| 451 |
|
| 452 |
/* Returns an object of size __n, and optionally adds to size __n free list.*/ |
| 453 |
/* We assume that __n is properly aligned. */ |
| 454 |
/* We hold the allocation lock. */ |
| 455 |
_Node_alloc_obj* __node_alloc_impl::_S_refill(size_t __n) { |
| 456 |
int __nobjs = 20; |
| 457 |
char* __chunk = _S_chunk_alloc(__n, __nobjs); |
| 458 |
|
| 459 |
if (1 == __nobjs) return __REINTERPRET_CAST(_Obj*, __chunk); |
| 460 |
|
| 461 |
_Obj* _STLP_VOLATILE* __my_free_list = _S_free_list + _S_FREELIST_INDEX(__n); |
| 462 |
_Obj* __result; |
| 463 |
_Obj* __current_obj; |
| 464 |
_Obj* __next_obj; |
| 465 |
|
| 466 |
/* Build free list in chunk */ |
| 467 |
__result = __REINTERPRET_CAST(_Obj*, __chunk); |
| 468 |
*__my_free_list = __next_obj = __REINTERPRET_CAST(_Obj*, __chunk + __n); |
| 469 |
for (--__nobjs; --__nobjs; ) { |
| 470 |
__current_obj = __next_obj; |
| 471 |
__next_obj = __REINTERPRET_CAST(_Obj*, __REINTERPRET_CAST(char*, __next_obj) + __n); |
| 472 |
__current_obj->_M_next = __next_obj; |
| 473 |
} |
| 474 |
__next_obj->_M_next = 0; |
| 475 |
return __result; |
| 476 |
} |
| 477 |
|
| 478 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 479 |
void __node_alloc_impl::_S_alloc_call() |
| 480 |
{ ++_S_alloc_counter(); } |
| 481 |
|
| 482 |
void __node_alloc_impl::_S_dealloc_call() { |
| 483 |
__stl_atomic_t &counter = _S_alloc_counter(); |
| 484 |
if (--counter == 0) |
| 485 |
{ _S_chunk_dealloc(); } |
| 486 |
} |
| 487 |
|
| 488 |
/* We deallocate all the memory chunks */ |
| 489 |
void __node_alloc_impl::_S_chunk_dealloc() { |
| 490 |
_Obj *__pcur = _S_chunks, *__pnext; |
| 491 |
while (__pcur != 0) { |
| 492 |
__pnext = __pcur->_M_next; |
| 493 |
__stlp_delete_chunck(__pcur); |
| 494 |
__pcur = __pnext; |
| 495 |
} |
| 496 |
_S_chunks = 0; |
| 497 |
_S_start_free = _S_end_free = 0; |
| 498 |
_S_heap_size = 0; |
| 499 |
memset(__REINTERPRET_CAST(char*, __CONST_CAST(_Obj**, &_S_free_list[0])), 0, _STLP_NFREELISTS * sizeof(_Obj*)); |
| 500 |
} |
| 501 |
# endif |
| 502 |
|
| 503 |
#else |
| 504 |
|
| 505 |
void* __node_alloc_impl::_M_allocate(size_t& __n) { |
| 506 |
__n = _S_round_up(__n); |
| 507 |
_Obj* __r = _S_free_list[_S_FREELIST_INDEX(__n)].pop(); |
| 508 |
if (__r == 0) |
| 509 |
{ __r = _S_refill(__n); } |
| 510 |
|
| 511 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 512 |
_S_alloc_call(); |
| 513 |
# endif |
| 514 |
return __r; |
| 515 |
} |
| 516 |
|
| 517 |
void __node_alloc_impl::_M_deallocate(void *__p, size_t __n) { |
| 518 |
_S_free_list[_S_FREELIST_INDEX(__n)].push(__STATIC_CAST(_Obj*, __p)); |
| 519 |
|
| 520 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 521 |
_S_dealloc_call(); |
| 522 |
# endif |
| 523 |
} |
| 524 |
|
| 525 |
/* Returns an object of size __n, and optionally adds additional ones to */ |
| 526 |
/* freelist of objects of size __n. */ |
| 527 |
/* We assume that __n is properly aligned. */ |
| 528 |
__node_alloc_impl::_Obj* __node_alloc_impl::_S_refill(size_t __n) { |
| 529 |
int __nobjs = 20; |
| 530 |
char* __chunk = _S_chunk_alloc(__n, __nobjs); |
| 531 |
|
| 532 |
if (__nobjs <= 1) |
| 533 |
return __REINTERPRET_CAST(_Obj*, __chunk); |
| 534 |
|
| 535 |
// Push all new nodes (minus first one) onto freelist |
| 536 |
_Obj* __result = __REINTERPRET_CAST(_Obj*, __chunk); |
| 537 |
_Obj* __cur_item = __result; |
| 538 |
_Freelist* __my_freelist = _S_free_list + _S_FREELIST_INDEX(__n); |
| 539 |
for (--__nobjs; __nobjs != 0; --__nobjs) { |
| 540 |
__cur_item = __REINTERPRET_CAST(_Obj*, __REINTERPRET_CAST(char*, __cur_item) + __n); |
| 541 |
__my_freelist->push(__cur_item); |
| 542 |
} |
| 543 |
return __result; |
| 544 |
} |
| 545 |
|
| 546 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 547 |
# define _STLP_OFFSET _ALIGN |
| 548 |
# else |
| 549 |
# define _STLP_OFFSET 0 |
| 550 |
# endif |
| 551 |
|
| 552 |
/* We allocate memory in large chunks in order to avoid fragmenting */ |
| 553 |
/* the malloc heap too much. */ |
| 554 |
/* We assume that size is properly aligned. */ |
| 555 |
char* __node_alloc_impl::_S_chunk_alloc(size_t _p_size, int& __nobjs) { |
| 556 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 557 |
//We are going to add a small memory block to keep all the allocated blocks |
| 558 |
//address, we need to do so respecting the memory alignment. The following |
| 559 |
//static assert checks that the reserved block is big enough to store a pointer. |
| 560 |
_STLP_STATIC_ASSERT(sizeof(_Obj) <= _ALIGN) |
| 561 |
# endif |
| 562 |
char* __result = 0; |
| 563 |
__add_atomic_t __total_bytes = __STATIC_CAST(__add_atomic_t, _p_size) * __nobjs; |
| 564 |
|
| 565 |
_FreeBlockHeader* __block = __STATIC_CAST(_FreeBlockHeader*, _S_free_mem_blocks.pop()); |
| 566 |
if (__block != 0) { |
| 567 |
// We checked a block out and can now mess with it with impugnity. |
| 568 |
// We'll put the remainder back into the list if we're done with it below. |
| 569 |
char* __buf_start = __REINTERPRET_CAST(char*, __block); |
| 570 |
__add_atomic_t __bytes_left = __block->_M_end - __buf_start; |
| 571 |
|
| 572 |
if ((__bytes_left < __total_bytes) && (__bytes_left >= __STATIC_CAST(__add_atomic_t, _p_size))) { |
| 573 |
// There's enough left for at least one object, but not as much as we wanted |
| 574 |
__result = __buf_start; |
| 575 |
__nobjs = (int)(__bytes_left/_p_size); |
| 576 |
__total_bytes = __STATIC_CAST(__add_atomic_t, _p_size) * __nobjs; |
| 577 |
__bytes_left -= __total_bytes; |
| 578 |
__buf_start += __total_bytes; |
| 579 |
} |
| 580 |
else if (__bytes_left >= __total_bytes) { |
| 581 |
// The block has enough left to satisfy all that was asked for |
| 582 |
__result = __buf_start; |
| 583 |
__bytes_left -= __total_bytes; |
| 584 |
__buf_start += __total_bytes; |
| 585 |
} |
| 586 |
|
| 587 |
if (__bytes_left != 0) { |
| 588 |
// There is still some memory left over in block after we satisfied our request. |
| 589 |
if ((__result != 0) && (__bytes_left >= (__add_atomic_t)sizeof(_FreeBlockHeader))) { |
| 590 |
// We were able to allocate at least one object and there is still enough |
| 591 |
// left to put remainder back into list. |
| 592 |
_FreeBlockHeader* __newblock = __REINTERPRET_CAST(_FreeBlockHeader*, __buf_start); |
| 593 |
__newblock->_M_end = __block->_M_end; |
| 594 |
_S_free_mem_blocks.push(__newblock); |
| 595 |
} |
| 596 |
else { |
| 597 |
// We were not able to allocate enough for at least one object. |
| 598 |
// Shove into freelist of nearest (rounded-down!) size. |
| 599 |
size_t __rounded_down = _S_round_up(__bytes_left + 1) - (size_t)_ALIGN; |
| 600 |
if (__rounded_down > 0) |
| 601 |
_S_free_list[_S_FREELIST_INDEX(__rounded_down)].push((_Obj*)__buf_start); |
| 602 |
} |
| 603 |
} |
| 604 |
if (__result != 0) |
| 605 |
return __result; |
| 606 |
} |
| 607 |
|
| 608 |
// We couldn't satisfy it from the list of free blocks, get new memory. |
| 609 |
__add_atomic_t __bytes_to_get = 2 * __total_bytes + |
| 610 |
__STATIC_CAST(__add_atomic_t, |
| 611 |
_S_round_up(__STATIC_CAST(__uadd_atomic_t, _STLP_ATOMIC_ADD(&_S_heap_size, 0)))) + |
| 612 |
_STLP_OFFSET; |
| 613 |
_STLP_TRY { |
| 614 |
__result = __stlp_new_chunk(__bytes_to_get); |
| 615 |
} |
| 616 |
#if defined (_STLP_USE_EXCEPTIONS) |
| 617 |
catch (const bad_alloc&) { |
| 618 |
// Allocation failed; try to canibalize from freelist of a larger object size. |
| 619 |
for (size_t __i = _p_size; __i <= (size_t)_MAX_BYTES; __i += (size_t)_ALIGN) { |
| 620 |
_Obj* __p = _S_free_list[_S_FREELIST_INDEX(__i)].pop(); |
| 621 |
if (0 != __p) { |
| 622 |
if (__i < sizeof(_FreeBlockHeader)) { |
| 623 |
// Not enough to put into list of free blocks, divvy it up here. |
| 624 |
// Use as much as possible for this request and shove remainder into freelist. |
| 625 |
__nobjs = (int)(__i/_p_size); |
| 626 |
__total_bytes = __nobjs * __STATIC_CAST(__add_atomic_t, _p_size); |
| 627 |
size_t __bytes_left = __i - __total_bytes; |
| 628 |
size_t __rounded_down = _S_round_up(__bytes_left+1) - (size_t)_ALIGN; |
| 629 |
if (__rounded_down > 0) { |
| 630 |
_S_free_list[_S_FREELIST_INDEX(__rounded_down)].push(__REINTERPRET_CAST(_Obj*, __REINTERPRET_CAST(char*, __p) + __total_bytes)); |
| 631 |
} |
| 632 |
return __REINTERPRET_CAST(char*, __p); |
| 633 |
} |
| 634 |
else { |
| 635 |
// Add node to list of available blocks and recursively allocate from it. |
| 636 |
_FreeBlockHeader* __newblock = (_FreeBlockHeader*)__p; |
| 637 |
__newblock->_M_end = __REINTERPRET_CAST(char*, __p) + __i; |
| 638 |
_S_free_mem_blocks.push(__newblock); |
| 639 |
return _S_chunk_alloc(_p_size, __nobjs); |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
// We were not able to find something in a freelist, try to allocate a smaller amount. |
| 645 |
__bytes_to_get = __total_bytes + _STLP_OFFSET; |
| 646 |
__result = __stlp_new_chunk(__bytes_to_get); |
| 647 |
|
| 648 |
// This should either throw an exception or remedy the situation. |
| 649 |
// Thus we assume it succeeded. |
| 650 |
} |
| 651 |
#endif |
| 652 |
// Alignment check |
| 653 |
_STLP_VERBOSE_ASSERT(((__REINTERPRET_CAST(size_t, __result) & __STATIC_CAST(size_t, _ALIGN - 1)) == 0), |
| 654 |
_StlMsg_DBA_DELETED_TWICE) |
| 655 |
_STLP_ATOMIC_ADD(&_S_heap_size, __bytes_to_get >> 4); |
| 656 |
|
| 657 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 658 |
// We have to track the allocated memory chunks for release on exit. |
| 659 |
_S_chunks.push(__REINTERPRET_CAST(_Obj*, __result)); |
| 660 |
__result += _ALIGN; |
| 661 |
__bytes_to_get -= _ALIGN; |
| 662 |
# endif |
| 663 |
|
| 664 |
if (__bytes_to_get > __total_bytes) { |
| 665 |
// Push excess memory allocated in this chunk into list of free memory blocks |
| 666 |
_FreeBlockHeader* __freeblock = __REINTERPRET_CAST(_FreeBlockHeader*, __result + __total_bytes); |
| 667 |
__freeblock->_M_end = __result + __bytes_to_get; |
| 668 |
_S_free_mem_blocks.push(__freeblock); |
| 669 |
} |
| 670 |
return __result; |
| 671 |
} |
| 672 |
|
| 673 |
# if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 674 |
void __node_alloc_impl::_S_alloc_call() |
| 675 |
{ _STLP_ATOMIC_INCREMENT(&_S_alloc_counter()); } |
| 676 |
|
| 677 |
void __node_alloc_impl::_S_dealloc_call() { |
| 678 |
_STLP_VOLATILE __stl_atomic_t *pcounter = &_S_alloc_counter(); |
| 679 |
if (_STLP_ATOMIC_DECREMENT(pcounter) == 0) |
| 680 |
_S_chunk_dealloc(); |
| 681 |
} |
| 682 |
|
| 683 |
/* We deallocate all the memory chunks */ |
| 684 |
void __node_alloc_impl::_S_chunk_dealloc() { |
| 685 |
// Note: The _Node_alloc_helper class ensures that this function |
| 686 |
// will only be called when the (shared) library is unloaded or the |
| 687 |
// process is shutdown. It's thus not possible that another thread |
| 688 |
// is currently trying to allocate a node (we're not thread-safe here). |
| 689 |
// |
| 690 |
|
| 691 |
// Clear the free blocks and all freelistst. This makes sure that if |
| 692 |
// for some reason more memory is allocated again during shutdown |
| 693 |
// (it'd also be really nasty to leave references to deallocated memory). |
| 694 |
_S_free_mem_blocks.clear(); |
| 695 |
_S_heap_size = 0; |
| 696 |
|
| 697 |
for (size_t __i = 0; __i < _STLP_NFREELISTS; ++__i) { |
| 698 |
_S_free_list[__i].clear(); |
| 699 |
} |
| 700 |
|
| 701 |
// Detach list of chunks and free them all |
| 702 |
_Obj* __chunk = _S_chunks.clear(); |
| 703 |
while (__chunk != 0) { |
| 704 |
_Obj* __next = __chunk->_M_next; |
| 705 |
__stlp_delete_chunck(__chunk); |
| 706 |
__chunk = __next; |
| 707 |
} |
| 708 |
} |
| 709 |
# endif |
| 710 |
|
| 711 |
#endif |
| 712 |
|
| 713 |
#if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 714 |
struct __node_alloc_cleaner { |
| 715 |
~__node_alloc_cleaner() |
| 716 |
{ __node_alloc_impl::_S_dealloc_call(); } |
| 717 |
}; |
| 718 |
|
| 719 |
# if defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 720 |
_STLP_VOLATILE __stl_atomic_t& _STLP_CALL |
| 721 |
# else |
| 722 |
__stl_atomic_t& _STLP_CALL |
| 723 |
# endif |
| 724 |
__node_alloc_impl::_S_alloc_counter() { |
| 725 |
static _AllocCounter _S_counter = 1; |
| 726 |
static __node_alloc_cleaner _S_node_alloc_cleaner; |
| 727 |
return _S_counter; |
| 728 |
} |
| 729 |
#endif |
| 730 |
|
| 731 |
#if !defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 732 |
_Node_alloc_obj * _STLP_VOLATILE |
| 733 |
__node_alloc_impl::_S_free_list[_STLP_NFREELISTS] |
| 734 |
= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 735 |
// The 16 zeros are necessary to make version 4.1 of the SunPro |
| 736 |
// compiler happy. Otherwise it appears to allocate too little |
| 737 |
// space for the array. |
| 738 |
#else |
| 739 |
_STLP_atomic_freelist __node_alloc_impl::_S_free_list[_STLP_NFREELISTS]; |
| 740 |
_STLP_atomic_freelist __node_alloc_impl::_S_free_mem_blocks; |
| 741 |
#endif |
| 742 |
|
| 743 |
#if !defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 744 |
char *__node_alloc_impl::_S_start_free = 0; |
| 745 |
char *__node_alloc_impl::_S_end_free = 0; |
| 746 |
#endif |
| 747 |
|
| 748 |
#if defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 749 |
_STLP_VOLATILE __add_atomic_t |
| 750 |
#else |
| 751 |
size_t |
| 752 |
#endif |
| 753 |
__node_alloc_impl::_S_heap_size = 0; |
| 754 |
|
| 755 |
#if defined (_STLP_DO_CLEAN_NODE_ALLOC) |
| 756 |
# if defined (_STLP_USE_LOCK_FREE_IMPLEMENTATION) |
| 757 |
_STLP_atomic_freelist __node_alloc_impl::_S_chunks; |
| 758 |
# else |
| 759 |
_Node_alloc_obj* __node_alloc_impl::_S_chunks = 0; |
| 760 |
# endif |
| 761 |
#endif |
| 762 |
|
| 763 |
void * _STLP_CALL __node_alloc::_M_allocate(size_t& __n) |
| 764 |
{ return __node_alloc_impl::_M_allocate(__n); } |
| 765 |
|
| 766 |
void _STLP_CALL __node_alloc::_M_deallocate(void *__p, size_t __n) |
| 767 |
{ __node_alloc_impl::_M_deallocate(__p, __n); } |
| 768 |
|
| 769 |
#if defined (_STLP_PTHREADS) && !defined (_STLP_NO_THREADS) |
| 770 |
|
| 771 |
# define _STLP_DATA_ALIGNMENT 8 |
| 772 |
|
| 773 |
_STLP_MOVE_TO_PRIV_NAMESPACE |
| 774 |
|
| 775 |
// ******************************************************* |
| 776 |
// __perthread_alloc implementation |
| 777 |
union _Pthread_alloc_obj { |
| 778 |
union _Pthread_alloc_obj * __free_list_link; |
| 779 |
char __client_data[_STLP_DATA_ALIGNMENT]; /* The client sees this. */ |
| 780 |
}; |
| 781 |
|
| 782 |
// Pthread allocators don't appear to the client to have meaningful |
| 783 |
// instances. We do in fact need to associate some state with each |
| 784 |
// thread. That state is represented by _Pthread_alloc_per_thread_state. |
| 785 |
|
| 786 |
struct _Pthread_alloc_per_thread_state { |
| 787 |
typedef _Pthread_alloc_obj __obj; |
| 788 |
enum { _S_NFREELISTS = _MAX_BYTES / _STLP_DATA_ALIGNMENT }; |
| 789 |
|
| 790 |
// Free list link for list of available per thread structures. |
| 791 |
// When one of these becomes available for reuse due to thread |
| 792 |
// termination, any objects in its free list remain associated |
| 793 |
// with it. The whole structure may then be used by a newly |
| 794 |
// created thread. |
| 795 |
_Pthread_alloc_per_thread_state() : __next(0) |
| 796 |
{ memset((void *)__CONST_CAST(_Pthread_alloc_obj**, __free_list), 0, (size_t)_S_NFREELISTS * sizeof(__obj *)); } |
| 797 |
// Returns an object of size __n, and possibly adds to size n free list. |
| 798 |
void *_M_refill(size_t __n); |
| 799 |
|
| 800 |
_Pthread_alloc_obj* volatile __free_list[_S_NFREELISTS]; |
| 801 |
_Pthread_alloc_per_thread_state *__next; |
| 802 |
// this data member is only to be used by per_thread_allocator, which returns memory to the originating thread. |
| 803 |
_STLP_mutex _M_lock; |
| 804 |
}; |
| 805 |
|
| 806 |
// Pthread-specific allocator. |
| 807 |
class _Pthread_alloc_impl { |
| 808 |
public: // but only for internal use: |
| 809 |
typedef _Pthread_alloc_per_thread_state __state_type; |
| 810 |
typedef char value_type; |
| 811 |
|
| 812 |
// Allocates a chunk for nobjs of size size. nobjs may be reduced |
| 813 |
// if it is inconvenient to allocate the requested number. |
| 814 |
static char *_S_chunk_alloc(size_t __size, size_t &__nobjs, __state_type*); |
| 815 |
|
| 816 |
enum {_S_ALIGN = _STLP_DATA_ALIGNMENT}; |
| 817 |
|
| 818 |
static size_t _S_round_up(size_t __bytes) |
| 819 |
{ return (((__bytes) + (int)_S_ALIGN - 1) & ~((int)_S_ALIGN - 1)); } |
| 820 |
static size_t _S_freelist_index(size_t __bytes) |
| 821 |
{ return (((__bytes) + (int)_S_ALIGN - 1) / (int)_S_ALIGN - 1); } |
| 822 |
|
| 823 |
private: |
| 824 |
// Chunk allocation state. And other shared state. |
| 825 |
// Protected by _S_chunk_allocator_lock. |
| 826 |
static _STLP_STATIC_MUTEX _S_chunk_allocator_lock; |
| 827 |
static char *_S_start_free; |
| 828 |
static char *_S_end_free; |
| 829 |
static size_t _S_heap_size; |
| 830 |
static __state_type *_S_free_per_thread_states; |
| 831 |
static pthread_key_t _S_key; |
| 832 |
static bool _S_key_initialized; |
| 833 |
// Pthread key under which per thread state is stored. |
| 834 |
// Allocator instances that are currently unclaimed by any thread. |
| 835 |
static void _S_destructor(void *instance); |
| 836 |
// Function to be called on thread exit to reclaim per thread |
| 837 |
// state. |
| 838 |
static __state_type *_S_new_per_thread_state(); |
| 839 |
public: |
| 840 |
// Return a recycled or new per thread state. |
| 841 |
static __state_type *_S_get_per_thread_state(); |
| 842 |
private: |
| 843 |
// ensure that the current thread has an associated |
| 844 |
// per thread state. |
| 845 |
class _M_lock; |
| 846 |
friend class _M_lock; |
| 847 |
class _M_lock { |
| 848 |
public: |
| 849 |
_M_lock () { _S_chunk_allocator_lock._M_acquire_lock(); } |
| 850 |
~_M_lock () { _S_chunk_allocator_lock._M_release_lock(); } |
| 851 |
}; |
| 852 |
|
| 853 |
public: |
| 854 |
|
| 855 |
/* n must be > 0 */ |
| 856 |
static void * allocate(size_t& __n); |
| 857 |
|
| 858 |
/* p may not be 0 */ |
| 859 |
static void deallocate(void *__p, size_t __n); |
| 860 |
|
| 861 |
// boris : versions for per_thread_allocator |
| 862 |
/* n must be > 0 */ |
| 863 |
static void * allocate(size_t& __n, __state_type* __a); |
| 864 |
|
| 865 |
/* p may not be 0 */ |
| 866 |
static void deallocate(void *__p, size_t __n, __state_type* __a); |
| 867 |
|
| 868 |
static void * reallocate(void *__p, size_t __old_sz, size_t& __new_sz); |
| 869 |
}; |
| 870 |
|
| 871 |
/* Returns an object of size n, and optionally adds to size n free list.*/ |
| 872 |
/* We assume that n is properly aligned. */ |
| 873 |
/* We hold the allocation lock. */ |
| 874 |
void *_Pthread_alloc_per_thread_state::_M_refill(size_t __n) { |
| 875 |
typedef _Pthread_alloc_obj __obj; |
| 876 |
size_t __nobjs = 128; |
| 877 |
char * __chunk = _Pthread_alloc_impl::_S_chunk_alloc(__n, __nobjs, this); |
| 878 |
__obj * volatile * __my_free_list; |
| 879 |
__obj * __result; |
| 880 |
__obj * __current_obj, * __next_obj; |
| 881 |
size_t __i; |
| 882 |
|
| 883 |
if (1 == __nobjs) { |
| 884 |
return __chunk; |
| 885 |
} |
| 886 |
|
| 887 |
__my_free_list = __free_list + _Pthread_alloc_impl::_S_freelist_index(__n); |
| 888 |
|
| 889 |
/* Build free list in chunk */ |
| 890 |
__result = (__obj *)__chunk; |
| 891 |
*__my_free_list = __next_obj = (__obj *)(__chunk + __n); |
| 892 |
for (__i = 1; ; ++__i) { |
| 893 |
__current_obj = __next_obj; |
| 894 |
__next_obj = (__obj *)((char *)__next_obj + __n); |
| 895 |
if (__nobjs - 1 == __i) { |
| 896 |
__current_obj -> __free_list_link = 0; |
| 897 |
break; |
| 898 |
} else { |
| 899 |
__current_obj -> __free_list_link = __next_obj; |
| 900 |
} |
| 901 |
} |
| 902 |
return __result; |
| 903 |
} |
| 904 |
|
| 905 |
void _Pthread_alloc_impl::_S_destructor(void *__instance) { |
| 906 |
_M_lock __lock_instance; // Need to acquire lock here. |
| 907 |
_Pthread_alloc_per_thread_state* __s = (_Pthread_alloc_per_thread_state*)__instance; |
| 908 |
__s -> __next = _S_free_per_thread_states; |
| 909 |
_S_free_per_thread_states = __s; |
| 910 |
} |
| 911 |
|
| 912 |
_Pthread_alloc_per_thread_state* _Pthread_alloc_impl::_S_new_per_thread_state() { |
| 913 |
/* lock already held here. */ |
| 914 |
if (0 != _S_free_per_thread_states) { |
| 915 |
_Pthread_alloc_per_thread_state *__result = _S_free_per_thread_states; |
| 916 |
_S_free_per_thread_states = _S_free_per_thread_states -> __next; |
| 917 |
return __result; |
| 918 |
} |
| 919 |
else { |
| 920 |
return new _Pthread_alloc_per_thread_state; |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
_Pthread_alloc_per_thread_state* _Pthread_alloc_impl::_S_get_per_thread_state() { |
| 925 |
int __ret_code; |
| 926 |
__state_type* __result; |
| 927 |
|
| 928 |
if (_S_key_initialized && (__result = (__state_type*) pthread_getspecific(_S_key))) |
| 929 |
return __result; |
| 930 |
|
| 931 |
/*REFERENCED*/ |
| 932 |
_M_lock __lock_instance; // Need to acquire lock here. |
| 933 |
if (!_S_key_initialized) { |
| 934 |
if (pthread_key_create(&_S_key, _S_destructor)) { |
| 935 |
_STLP_THROW_BAD_ALLOC; // failed |
| 936 |
} |
| 937 |
_S_key_initialized = true; |
| 938 |
} |
| 939 |
|
| 940 |
__result = _S_new_per_thread_state(); |
| 941 |
__ret_code = pthread_setspecific(_S_key, __result); |
| 942 |
if (__ret_code) { |
| 943 |
if (__ret_code == ENOMEM) { |
| 944 |
_STLP_THROW_BAD_ALLOC; |
| 945 |
} else { |
| 946 |
// EINVAL |
| 947 |
_STLP_ABORT(); |
| 948 |
} |
| 949 |
} |
| 950 |
return __result; |
| 951 |
} |
| 952 |
|
| 953 |
/* We allocate memory in large chunks in order to avoid fragmenting */ |
| 954 |
/* the malloc heap too much. */ |
| 955 |
/* We assume that size is properly aligned. */ |
| 956 |
char *_Pthread_alloc_impl::_S_chunk_alloc(size_t __p_size, size_t &__nobjs, _Pthread_alloc_per_thread_state *__a) { |
| 957 |
typedef _Pthread_alloc_obj __obj; |
| 958 |
{ |
| 959 |
char * __result; |
| 960 |
size_t __total_bytes; |
| 961 |
size_t __bytes_left; |
| 962 |
/*REFERENCED*/ |
| 963 |
_M_lock __lock_instance; // Acquire lock for this routine |
| 964 |
|
| 965 |
__total_bytes = __p_size * __nobjs; |
| 966 |
__bytes_left = _S_end_free - _S_start_free; |
| 967 |
if (__bytes_left >= __total_bytes) { |
| 968 |
__result = _S_start_free; |
| 969 |
_S_start_free += __total_bytes; |
| 970 |
return __result; |
| 971 |
} else if (__bytes_left >= __p_size) { |
| 972 |
__nobjs = __bytes_left/__p_size; |
| 973 |
__total_bytes = __p_size * __nobjs; |
| 974 |
__result = _S_start_free; |
| 975 |
_S_start_free += __total_bytes; |
| 976 |
return __result; |
| 977 |
} else { |
| 978 |
size_t __bytes_to_get = 2 * __total_bytes + _S_round_up(_S_heap_size); |
| 979 |
// Try to make use of the left-over piece. |
| 980 |
if (__bytes_left > 0) { |
| 981 |
__obj * volatile * __my_free_list = __a->__free_list + _S_freelist_index(__bytes_left); |
| 982 |
((__obj *)_S_start_free) -> __free_list_link = *__my_free_list; |
| 983 |
*__my_free_list = (__obj *)_S_start_free; |
| 984 |
} |
| 985 |
# ifdef _SGI_SOURCE |
| 986 |
// Try to get memory that's aligned on something like a |
| 987 |
// cache line boundary, so as to avoid parceling out |
| 988 |
// parts of the same line to different threads and thus |
| 989 |
// possibly different processors. |
| 990 |
{ |
| 991 |
const int __cache_line_size = 128; // probable upper bound |
| 992 |
__bytes_to_get &= ~(__cache_line_size-1); |
| 993 |
_S_start_free = (char *)memalign(__cache_line_size, __bytes_to_get); |
| 994 |
if (0 == _S_start_free) { |
| 995 |
_S_start_free = (char *)__malloc_alloc::allocate(__bytes_to_get); |
| 996 |
} |
| 997 |
} |
| 998 |
# else /* !SGI_SOURCE */ |
| 999 |
_S_start_free = (char *)__malloc_alloc::allocate(__bytes_to_get); |
| 1000 |
# endif |
| 1001 |
_S_heap_size += __bytes_to_get >> 4; |
| 1002 |
_S_end_free = _S_start_free + __bytes_to_get; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
// lock is released here |
| 1006 |
return _S_chunk_alloc(__p_size, __nobjs, __a); |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
/* n must be > 0 */ |
| 1011 |
void *_Pthread_alloc_impl::allocate(size_t& __n) { |
| 1012 |
typedef _Pthread_alloc_obj __obj; |
| 1013 |
__obj * volatile * __my_free_list; |
| 1014 |
__obj * __result; |
| 1015 |
__state_type* __a; |
| 1016 |
|
| 1017 |
if (__n > _MAX_BYTES) { |
| 1018 |
return __malloc_alloc::allocate(__n); |
| 1019 |
} |
| 1020 |
|
| 1021 |
__n = _S_round_up(__n); |
| 1022 |
__a = _S_get_per_thread_state(); |
| 1023 |
|
| 1024 |
__my_free_list = __a->__free_list + _S_freelist_index(__n); |
| 1025 |
__result = *__my_free_list; |
| 1026 |
if (__result == 0) { |
| 1027 |
void *__r = __a->_M_refill(__n); |
| 1028 |
return __r; |
| 1029 |
} |
| 1030 |
*__my_free_list = __result->__free_list_link; |
| 1031 |
return __result; |
| 1032 |
}; |
| 1033 |
|
| 1034 |
/* p may not be 0 */ |
| 1035 |
void _Pthread_alloc_impl::deallocate(void *__p, size_t __n) { |
| 1036 |
typedef _Pthread_alloc_obj __obj; |
| 1037 |
__obj *__q = (__obj *)__p; |
| 1038 |
__obj * volatile * __my_free_list; |
| 1039 |
__state_type* __a; |
| 1040 |
|
| 1041 |
if (__n > _MAX_BYTES) { |
| 1042 |
__malloc_alloc::deallocate(__p, __n); |
| 1043 |
return; |
| 1044 |
} |
| 1045 |
|
| 1046 |
__a = _S_get_per_thread_state(); |
| 1047 |
|
| 1048 |
__my_free_list = __a->__free_list + _S_freelist_index(__n); |
| 1049 |
__q -> __free_list_link = *__my_free_list; |
| 1050 |
*__my_free_list = __q; |
| 1051 |
} |
| 1052 |
|
| 1053 |
// boris : versions for per_thread_allocator |
| 1054 |
/* n must be > 0 */ |
| 1055 |
void *_Pthread_alloc_impl::allocate(size_t& __n, __state_type* __a) { |
| 1056 |
typedef _Pthread_alloc_obj __obj; |
| 1057 |
__obj * volatile * __my_free_list; |
| 1058 |
__obj * __result; |
| 1059 |
|
| 1060 |
if (__n > _MAX_BYTES) { |
| 1061 |
return __malloc_alloc::allocate(__n); |
| 1062 |
} |
| 1063 |
__n = _S_round_up(__n); |
| 1064 |
|
| 1065 |
// boris : here, we have to lock per thread state, as we may be getting memory from |
| 1066 |
// different thread pool. |
| 1067 |
_STLP_auto_lock __lock(__a->_M_lock); |
| 1068 |
|
| 1069 |
__my_free_list = __a->__free_list + _S_freelist_index(__n); |
| 1070 |
__result = *__my_free_list; |
| 1071 |
if (__result == 0) { |
| 1072 |
void *__r = __a->_M_refill(__n); |
| 1073 |
return __r; |
| 1074 |
} |
| 1075 |
*__my_free_list = __result->__free_list_link; |
| 1076 |
return __result; |
| 1077 |
}; |
| 1078 |
|
| 1079 |
/* p may not be 0 */ |
| 1080 |
void _Pthread_alloc_impl::deallocate(void *__p, size_t __n, __state_type* __a) { |
| 1081 |
typedef _Pthread_alloc_obj __obj; |
| 1082 |
__obj *__q = (__obj *)__p; |
| 1083 |
__obj * volatile * __my_free_list; |
| 1084 |
|
| 1085 |
if (__n > _MAX_BYTES) { |
| 1086 |
__malloc_alloc::deallocate(__p, __n); |
| 1087 |
return; |
| 1088 |
} |
| 1089 |
|
| 1090 |
// boris : here, we have to lock per thread state, as we may be returning memory from |
| 1091 |
// different thread. |
| 1092 |
_STLP_auto_lock __lock(__a->_M_lock); |
| 1093 |
|
| 1094 |
__my_free_list = __a->__free_list + _S_freelist_index(__n); |
| 1095 |
__q -> __free_list_link = *__my_free_list; |
| 1096 |
*__my_free_list = __q; |
| 1097 |
} |
| 1098 |
|
| 1099 |
void *_Pthread_alloc_impl::reallocate(void *__p, size_t __old_sz, size_t& __new_sz) { |
| 1100 |
void * __result; |
| 1101 |
size_t __copy_sz; |
| 1102 |
|
| 1103 |
if (__old_sz > _MAX_BYTES && __new_sz > _MAX_BYTES) { |
| 1104 |
return realloc(__p, __new_sz); |
| 1105 |
} |
| 1106 |
|
| 1107 |
if (_S_round_up(__old_sz) == _S_round_up(__new_sz)) return __p; |
| 1108 |
__result = allocate(__new_sz); |
| 1109 |
__copy_sz = __new_sz > __old_sz? __old_sz : __new_sz; |
| 1110 |
memcpy(__result, __p, __copy_sz); |
| 1111 |
deallocate(__p, __old_sz); |
| 1112 |
return __result; |
| 1113 |
} |
| 1114 |
|
| 1115 |
_Pthread_alloc_per_thread_state* _Pthread_alloc_impl::_S_free_per_thread_states = 0; |
| 1116 |
pthread_key_t _Pthread_alloc_impl::_S_key = 0; |
| 1117 |
_STLP_STATIC_MUTEX _Pthread_alloc_impl::_S_chunk_allocator_lock _STLP_MUTEX_INITIALIZER; |
| 1118 |
bool _Pthread_alloc_impl::_S_key_initialized = false; |
| 1119 |
char *_Pthread_alloc_impl::_S_start_free = 0; |
| 1120 |
char *_Pthread_alloc_impl::_S_end_free = 0; |
| 1121 |
size_t _Pthread_alloc_impl::_S_heap_size = 0; |
| 1122 |
|
| 1123 |
void * _STLP_CALL _Pthread_alloc::allocate(size_t& __n) |
| 1124 |
{ return _Pthread_alloc_impl::allocate(__n); } |
| 1125 |
void _STLP_CALL _Pthread_alloc::deallocate(void *__p, size_t __n) |
| 1126 |
{ _Pthread_alloc_impl::deallocate(__p, __n); } |
| 1127 |
void * _STLP_CALL _Pthread_alloc::allocate(size_t& __n, __state_type* __a) |
| 1128 |
{ return _Pthread_alloc_impl::allocate(__n, __a); } |
| 1129 |
void _STLP_CALL _Pthread_alloc::deallocate(void *__p, size_t __n, __state_type* __a) |
| 1130 |
{ _Pthread_alloc_impl::deallocate(__p, __n, __a); } |
| 1131 |
void * _STLP_CALL _Pthread_alloc::reallocate(void *__p, size_t __old_sz, size_t& __new_sz) |
| 1132 |
{ return _Pthread_alloc_impl::reallocate(__p, __old_sz, __new_sz); } |
| 1133 |
_Pthread_alloc_per_thread_state* _STLP_CALL _Pthread_alloc::_S_get_per_thread_state() |
| 1134 |
{ return _Pthread_alloc_impl::_S_get_per_thread_state(); } |
| 1135 |
|
| 1136 |
_STLP_MOVE_TO_STD_NAMESPACE |
| 1137 |
|
| 1138 |
#endif |
| 1139 |
|
| 1140 |
_STLP_END_NAMESPACE |
| 1141 |
|
| 1142 |
#undef _S_FREELIST_INDEX |