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 _GLIBCXX_NOEXCEPT
584 address(const_reference __x)
const _GLIBCXX_NOEXCEPT
588 max_size()
const _GLIBCXX_USE_NOEXCEPT
589 {
return size_t(-1) /
sizeof(_Tp); }
591 #ifdef __GXX_EXPERIMENTAL_CXX0X__
592 template<
typename _Up,
typename... _Args>
594 construct(_Up* __p, _Args&&... __args)
595 { ::new((
void *)__p) _Up(std::forward<_Args>(__args)...); }
597 template<
typename _Up>
599 destroy(_Up* __p) { __p->~_Up(); }
604 construct(pointer __p,
const _Tp& __val)
605 { ::new((
void *)__p) _Tp(__val); }
608 destroy(pointer __p) { __p->~_Tp(); }
613 #define __thread_default true
615 #define __thread_default false
629 template<
typename _Tp,
634 typedef size_t size_type;
635 typedef ptrdiff_t difference_type;
636 typedef _Tp* pointer;
637 typedef const _Tp* const_pointer;
638 typedef _Tp& reference;
639 typedef const _Tp& const_reference;
640 typedef _Tp value_type;
641 typedef _Poolp __policy_type;
642 typedef typename _Poolp::pool_type __pool_type;
644 template<
typename _Tp1,
typename _Poolp1 = _Poolp>
647 typedef typename _Poolp1::template _M_rebind<_Tp1>::other pol_type;
655 template<
typename _Tp1,
typename _Poolp1>
661 allocate(size_type __n,
const void* = 0);
664 deallocate(pointer __p, size_type __n);
666 const __pool_base::_Tune
670 return __policy_type::_S_get_pool()._M_get_options();
674 _M_set_options(__pool_base::_Tune __t)
675 { __policy_type::_S_get_pool()._M_set_options(__t); }
678 template<
typename _Tp,
typename _Poolp>
679 typename __mt_alloc<_Tp, _Poolp>::pointer
681 allocate(size_type __n,
const void*)
683 if (__n > this->max_size())
684 std::__throw_bad_alloc();
686 __policy_type::_S_initialize_once();
690 __pool_type&
__pool = __policy_type::_S_get_pool();
691 const size_t __bytes = __n *
sizeof(_Tp);
692 if (__pool._M_check_threshold(__bytes))
694 void* __ret = ::operator
new(__bytes);
695 return static_cast<_Tp*
>(__ret);
699 const size_t __which = __pool._M_get_binmap(__bytes);
700 const size_t __thread_id = __pool._M_get_thread_id();
705 typedef typename __pool_type::_Bin_record _Bin_record;
706 const _Bin_record& __bin = __pool._M_get_bin(__which);
707 if (__bin._M_first[__thread_id])
710 typedef typename __pool_type::_Block_record _Block_record;
711 _Block_record* __block = __bin._M_first[__thread_id];
712 __bin._M_first[__thread_id] = __block->_M_next;
714 __pool._M_adjust_freelist(__bin, __block, __thread_id);
715 __c =
reinterpret_cast<char*
>(__block) + __pool._M_get_align();
720 __c = __pool._M_reserve_block(__bytes, __thread_id);
722 return static_cast<_Tp*
>(
static_cast<void*
>(__c));
725 template<
typename _Tp,
typename _Poolp>
727 __mt_alloc<_Tp, _Poolp>::
728 deallocate(pointer __p, size_type __n)
730 if (__builtin_expect(__p != 0,
true))
734 __pool_type& __pool = __policy_type::_S_get_pool();
735 const size_t __bytes = __n *
sizeof(_Tp);
736 if (__pool._M_check_threshold(__bytes))
737 ::
operator delete(__p);
739 __pool._M_reclaim_block(reinterpret_cast<char*>(__p), __bytes);
743 template<
typename _Tp,
typename _Poolp>
745 operator==(
const __mt_alloc<_Tp, _Poolp>&,
const __mt_alloc<_Tp, _Poolp>&)
748 template<
typename _Tp,
typename _Poolp>
750 operator!=(
const __mt_alloc<_Tp, _Poolp>&,
const __mt_alloc<_Tp, _Poolp>&)
753 #undef __thread_default
755 _GLIBCXX_END_NAMESPACE_VERSION
This is a fixed size (power of 2) allocator which - when compiled with thread support - will maintain...
Base class for pool object.
Policy for shared __pool objects.
Policy for individual __pool objects.
Data describing the underlying memory pool, parameterized on threading support.
Base class for _Tp dependent member functions.
_Tp * __addressof(_Tp &__r) _GLIBCXX_NOEXCEPT
Same as C++11 std::addressof.