30 #ifndef _MT_ALLOCATOR_H
31 #define _MT_ALLOCATOR_H 1
39 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 typedef void (*__destroy_handler)(
void*);
53 typedef unsigned short int _Binmap_type;
60 enum { _S_align = 8 };
61 enum { _S_max_bytes = 128 };
62 enum { _S_min_bin = 8 };
63 enum { _S_chunk_size = 4096 - 4 *
sizeof(
void*) };
64 enum { _S_max_threads = 4096 };
65 enum { _S_freelist_headroom = 10 };
98 size_t _M_max_threads;
106 size_t _M_freelist_headroom;
113 : _M_align(_S_align), _M_max_bytes(_S_max_bytes), _M_min_bin(_S_min_bin),
114 _M_chunk_size(_S_chunk_size), _M_max_threads(_S_max_threads),
115 _M_freelist_headroom(_S_freelist_headroom),
116 _M_force_new(std::getenv(
"GLIBCXX_FORCE_NEW") ?
true :
false)
120 _Tune(
size_t __align,
size_t __maxb,
size_t __minbin,
size_t __chunk,
121 size_t __maxthreads,
size_t __headroom,
bool __force)
122 : _M_align(__align), _M_max_bytes(__maxb), _M_min_bin(__minbin),
123 _M_chunk_size(__chunk), _M_max_threads(__maxthreads),
124 _M_freelist_headroom(__headroom), _M_force_new(__force)
128 struct _Block_address
131 _Block_address* _M_next;
135 _M_get_options()
const
136 {
return _M_options; }
139 _M_set_options(_Tune __t)
146 _M_check_threshold(
size_t __bytes)
147 {
return __bytes > _M_options._M_max_bytes || _M_options._M_force_new; }
150 _M_get_binmap(
size_t __bytes)
151 {
return _M_binmap[__bytes]; }
155 {
return _M_options._M_align; }
159 : _M_options(_Tune()), _M_binmap(0), _M_init(
false) { }
163 : _M_options(__options), _M_binmap(0), _M_init(
false) { }
176 _Binmap_type* _M_binmap;
189 template<
bool _Thread>
200 _Block_record* _M_next;
206 _Block_record** _M_first;
209 _Block_address* _M_address;
215 if (__builtin_expect(_M_init ==
false,
false))
220 _M_destroy()
throw();
223 _M_reserve_block(
size_t __bytes,
const size_t __thread_id);
226 _M_reclaim_block(
char* __p,
size_t __bytes)
throw ();
229 _M_get_thread_id() {
return 0; }
232 _M_get_bin(
size_t __which)
233 {
return _M_bin[__which]; }
236 _M_adjust_freelist(
const _Bin_record&, _Block_record*,
size_t)
240 : _M_bin(0), _M_bin_size(1) { }
242 explicit __pool(
const __pool_base::_Tune& __tune)
243 :
__pool_base(__tune), _M_bin(0), _M_bin_size(1) { }
273 struct _Thread_record
276 _Thread_record* _M_next;
285 _Block_record* _M_next;
296 _Block_record** _M_first;
299 _Block_address* _M_address;
316 __gthread_mutex_t* _M_mutex;
321 _M_initialize(__destroy_handler);
326 if (__builtin_expect(_M_init ==
false,
false))
331 _M_destroy()
throw();
334 _M_reserve_block(
size_t __bytes,
const size_t __thread_id);
337 _M_reclaim_block(
char* __p,
size_t __bytes)
throw ();
340 _M_get_bin(
size_t __which)
341 {
return _M_bin[__which]; }
344 _M_adjust_freelist(
const _Bin_record& __bin, _Block_record* __block,
347 if (__gthread_active_p())
349 __block->_M_thread_id = __thread_id;
350 --__bin._M_free[__thread_id];
351 ++__bin._M_used[__thread_id];
357 _M_destroy_thread_key(
void*)
throw ();
363 : _M_bin(0), _M_bin_size(1), _M_thread_freelist(0)
366 explicit __pool(
const __pool_base::_Tune& __tune)
368 _M_thread_freelist(0)
380 _Thread_record* _M_thread_freelist;
381 void* _M_thread_freelist_initial;
388 template<
template <
bool>
class _PoolTp,
bool _Thread>
391 typedef _PoolTp<_Thread> pool_type;
396 static pool_type _S_pool;
401 template<
template <
bool>
class _PoolTp,
bool _Thread>
402 struct __common_pool_base;
404 template<
template <
bool>
class _PoolTp>
405 struct __common_pool_base<_PoolTp, false>
406 :
public __common_pool<_PoolTp, false>
408 using __common_pool<_PoolTp, false>::_S_get_pool;
414 if (__builtin_expect(__init ==
false,
false))
416 _S_get_pool()._M_initialize_once();
423 template<
template <
bool>
class _PoolTp>
424 struct __common_pool_base<_PoolTp, true>
425 :
public __common_pool<_PoolTp, true>
427 using __common_pool<_PoolTp, true>::_S_get_pool;
431 { _S_get_pool()._M_initialize_once(); }
437 if (__builtin_expect(__init ==
false,
false))
439 if (__gthread_active_p())
442 static __gthread_once_t __once = __GTHREAD_ONCE_INIT;
443 __gthread_once(&__once, _S_initialize);
449 _S_get_pool()._M_initialize_once();
457 template<
template <
bool>
class _PoolTp,
bool _Thread>
460 template<
typename _Tp1,
template <
bool>
class _PoolTp1 = _PoolTp,
461 bool _Thread1 = _Thread>
465 using __common_pool_base<_PoolTp, _Thread>::_S_get_pool;
466 using __common_pool_base<_PoolTp, _Thread>::_S_initialize_once;
470 template<
typename _Tp,
template <
bool>
class _PoolTp,
bool _Thread>
471 struct __per_type_pool
473 typedef _Tp value_type;
474 typedef _PoolTp<_Thread> pool_type;
480 typedef typename pool_type::_Block_record _Block_record;
481 const static size_t __a = (__alignof__(_Tp) >=
sizeof(_Block_record)
482 ? __alignof__(_Tp) :
sizeof(_Block_record));
484 typedef typename __pool_base::_Tune _Tune;
485 static _Tune _S_tune(__a,
sizeof(_Tp) * 64,
486 sizeof(_Tp) * 2 >= __a ?
sizeof(_Tp) * 2 : __a,
487 sizeof(_Tp) *
size_t(_Tune::_S_chunk_size),
488 _Tune::_S_max_threads,
489 _Tune::_S_freelist_headroom,
490 std::getenv(
"GLIBCXX_FORCE_NEW") ?
true :
false);
491 static pool_type _S_pool(_S_tune);
496 template<
typename _Tp,
template <
bool>
class _PoolTp,
bool _Thread>
497 struct __per_type_pool_base;
499 template<
typename _Tp,
template <
bool>
class _PoolTp>
500 struct __per_type_pool_base<_Tp, _PoolTp, false>
501 :
public __per_type_pool<_Tp, _PoolTp, false>
503 using __per_type_pool<_Tp, _PoolTp, false>::_S_get_pool;
509 if (__builtin_expect(__init ==
false,
false))
511 _S_get_pool()._M_initialize_once();
518 template<
typename _Tp,
template <
bool>
class _PoolTp>
519 struct __per_type_pool_base<_Tp, _PoolTp, true>
520 :
public __per_type_pool<_Tp, _PoolTp, true>
522 using __per_type_pool<_Tp, _PoolTp, true>::_S_get_pool;
526 { _S_get_pool()._M_initialize_once(); }
532 if (__builtin_expect(__init ==
false,
false))
534 if (__gthread_active_p())
537 static __gthread_once_t __once = __GTHREAD_ONCE_INIT;
538 __gthread_once(&__once, _S_initialize);
544 _S_get_pool()._M_initialize_once();
552 template<
typename _Tp,
template <
bool>
class _PoolTp,
bool _Thread>
554 :
public __per_type_pool_base<_Tp, _PoolTp, _Thread>
556 template<
typename _Tp1,
template <
bool>
class _PoolTp1 = _PoolTp,
557 bool _Thread1 = _Thread>
561 using __per_type_pool_base<_Tp, _PoolTp, _Thread>::_S_get_pool;
562 using __per_type_pool_base<_Tp, _PoolTp, _Thread>::_S_initialize_once;
567 template<
typename _Tp>
571 typedef size_t size_type;
572 typedef ptrdiff_t difference_type;
573 typedef _Tp* pointer;
574 typedef const _Tp* const_pointer;
575 typedef _Tp& reference;
576 typedef const _Tp& const_reference;
577 typedef _Tp value_type;
580 address(reference __x)
const
581 {
return std::__addressof(__x); }
584 address(const_reference __x)
const
585 {
return std::__addressof(__x); }
588 max_size()
const throw()
589 {
return size_t(-1) /
sizeof(_Tp); }
594 construct(pointer __p,
const _Tp& __val)
595 { ::new((
void *)__p) _Tp(__val); }
597 #ifdef __GXX_EXPERIMENTAL_CXX0X__
598 template<
typename... _Args>
600 construct(pointer __p, _Args&&... __args)
601 { ::new((
void *)__p) _Tp(std::forward<_Args>(__args)...); }
605 destroy(pointer __p) { __p->~_Tp(); }
609 #define __thread_default true
611 #define __thread_default false
625 template<
typename _Tp,
630 typedef size_t size_type;
631 typedef ptrdiff_t difference_type;
632 typedef _Tp* pointer;
633 typedef const _Tp* const_pointer;
634 typedef _Tp& reference;
635 typedef const _Tp& const_reference;
636 typedef _Tp value_type;
637 typedef _Poolp __policy_type;
638 typedef typename _Poolp::pool_type __pool_type;
640 template<
typename _Tp1,
typename _Poolp1 = _Poolp>
643 typedef typename _Poolp1::template _M_rebind<_Tp1>::other pol_type;
651 template<
typename _Tp1,
typename _Poolp1>
657 allocate(size_type __n,
const void* = 0);
660 deallocate(pointer __p, size_type __n);
662 const __pool_base::_Tune
666 return __policy_type::_S_get_pool()._M_get_options();
670 _M_set_options(__pool_base::_Tune __t)
671 { __policy_type::_S_get_pool()._M_set_options(__t); }
674 template<
typename _Tp,
typename _Poolp>
675 typename __mt_alloc<_Tp, _Poolp>::pointer
677 allocate(size_type __n,
const void*)
679 if (__n > this->max_size())
680 std::__throw_bad_alloc();
682 __policy_type::_S_initialize_once();
686 __pool_type&
__pool = __policy_type::_S_get_pool();
687 const size_t __bytes = __n *
sizeof(_Tp);
688 if (__pool._M_check_threshold(__bytes))
690 void* __ret = ::operator
new(__bytes);
691 return static_cast<_Tp*
>(__ret);
695 const size_t __which = __pool._M_get_binmap(__bytes);
696 const size_t __thread_id = __pool._M_get_thread_id();
701 typedef typename __pool_type::_Bin_record _Bin_record;
702 const _Bin_record& __bin = __pool._M_get_bin(__which);
703 if (__bin._M_first[__thread_id])
706 typedef typename __pool_type::_Block_record _Block_record;
707 _Block_record* __block = __bin._M_first[__thread_id];
708 __bin._M_first[__thread_id] = __block->_M_next;
710 __pool._M_adjust_freelist(__bin, __block, __thread_id);
711 __c =
reinterpret_cast<char*
>(__block) + __pool._M_get_align();
716 __c = __pool._M_reserve_block(__bytes, __thread_id);
718 return static_cast<_Tp*
>(
static_cast<void*
>(__c));
721 template<
typename _Tp,
typename _Poolp>
723 __mt_alloc<_Tp, _Poolp>::
724 deallocate(pointer __p, size_type __n)
726 if (__builtin_expect(__p != 0,
true))
730 __pool_type& __pool = __policy_type::_S_get_pool();
731 const size_t __bytes = __n *
sizeof(_Tp);
732 if (__pool._M_check_threshold(__bytes))
733 ::
operator delete(__p);
735 __pool._M_reclaim_block(reinterpret_cast<char*>(__p), __bytes);
739 template<
typename _Tp,
typename _Poolp>
741 operator==(
const __mt_alloc<_Tp, _Poolp>&,
const __mt_alloc<_Tp, _Poolp>&)
744 template<
typename _Tp,
typename _Poolp>
746 operator!=(
const __mt_alloc<_Tp, _Poolp>&,
const __mt_alloc<_Tp, _Poolp>&)
749 #undef __thread_default
751 _GLIBCXX_END_NAMESPACE_VERSION
Base class for _Tp dependent member functions.
This is a fixed size (power of 2) allocator which - when compiled with thread support - will maintain...
Data describing the underlying memory pool, parameterized on threading support.
Base class for pool object.
Policy for shared __pool objects.
Policy for individual __pool objects.