29#ifndef _MT_ALLOCATOR_H
30#define _MT_ALLOCATOR_H 1
37#if __cplusplus >= 201103L
41namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
46 typedef void (*__destroy_handler)(
void*);
53 typedef unsigned short int _Binmap_type;
54 typedef std::size_t size_t;
61 enum { _S_align = 8 };
62 enum { _S_max_bytes = 128 };
63 enum { _S_min_bin = 8 };
64 enum { _S_chunk_size = 4096 - 4 *
sizeof(
void*) };
65 enum { _S_max_threads = 4096 };
66 enum { _S_freelist_headroom = 10 };
99 size_t _M_max_threads;
107 size_t _M_freelist_headroom;
114 : _M_align(_S_align), _M_max_bytes(_S_max_bytes), _M_min_bin(_S_min_bin),
115 _M_chunk_size(_S_chunk_size), _M_max_threads(_S_max_threads),
116 _M_freelist_headroom(_S_freelist_headroom),
117 _M_force_new(std::getenv(
"GLIBCXX_FORCE_NEW") ?
true :
false)
121 _Tune(
size_t __align,
size_t __maxb,
size_t __minbin,
size_t __chunk,
122 size_t __maxthreads,
size_t __headroom,
bool __force)
123 : _M_align(__align), _M_max_bytes(__maxb), _M_min_bin(__minbin),
124 _M_chunk_size(__chunk), _M_max_threads(__maxthreads),
125 _M_freelist_headroom(__headroom), _M_force_new(__force)
129 struct _Block_address
132 _Block_address* _M_next;
136 _M_get_options()
const
137 {
return _M_options; }
140 _M_set_options(_Tune __t)
147 _M_check_threshold(
size_t __bytes)
148 {
return __bytes > _M_options._M_max_bytes || _M_options._M_force_new; }
151 _M_get_binmap(
size_t __bytes)
152 {
return _M_binmap[__bytes]; }
156 {
return _M_options._M_align; }
160 : _M_options(_Tune()), _M_binmap(0), _M_init(
false) { }
164 : _M_options(__options), _M_binmap(0), _M_init(
false) { }
177 _Binmap_type* _M_binmap;
190 template<
bool _Thread>
201 _Block_record* _M_next;
207 _Block_record** _M_first;
210 _Block_address* _M_address;
216 if (__builtin_expect(_M_init ==
false,
false))
221 _M_destroy()
throw();
224 _M_reserve_block(
size_t __bytes,
const size_t __thread_id);
227 _M_reclaim_block(
char* __p,
size_t __bytes)
throw ();
230 _M_get_thread_id() {
return 0; }
233 _M_get_bin(
size_t __which)
234 {
return _M_bin[__which]; }
237 _M_adjust_freelist(
const _Bin_record&, _Block_record*,
size_t)
241 : _M_bin(0), _M_bin_size(1) { }
243 explicit __pool(
const __pool_base::_Tune& __tune)
244 :
__pool_base(__tune), _M_bin(0), _M_bin_size(1) { }
274 struct _Thread_record
277 _Thread_record* _M_next;
286 _Block_record* _M_next;
297 _Block_record** _M_first;
300 _Block_address* _M_address;
317 __gthread_mutex_t* _M_mutex;
322 _M_initialize(__destroy_handler);
327 if (__builtin_expect(_M_init ==
false,
false))
332 _M_destroy()
throw();
335 _M_reserve_block(
size_t __bytes,
const size_t __thread_id);
338 _M_reclaim_block(
char* __p,
size_t __bytes)
throw ();
341 _M_get_bin(
size_t __which)
342 {
return _M_bin[__which]; }
345 _M_adjust_freelist(
const _Bin_record& __bin, _Block_record* __block,
348 if (__gthread_active_p())
350 __block->_M_thread_id = __thread_id;
351 --__bin._M_free[__thread_id];
352 ++__bin._M_used[__thread_id];
358 _M_destroy_thread_key(
void*)
throw ();
364 : _M_bin(0), _M_bin_size(1), _M_thread_freelist(0)
367 explicit __pool(
const __pool_base::_Tune& __tune)
369 _M_thread_freelist(0)
381 _Thread_record* _M_thread_freelist;
382 void* _M_thread_freelist_initial;
389 template<
template <
bool>
class _PoolTp,
bool _Thread>
392 typedef _PoolTp<_Thread> pool_type;
397 static pool_type _S_pool;
402 template<
template <
bool>
class _PoolTp,
bool _Thread>
403 struct __common_pool_base;
405 template<
template <
bool>
class _PoolTp>
406 struct __common_pool_base<_PoolTp, false>
407 :
public __common_pool<_PoolTp, false>
409 using __common_pool<_PoolTp,
false>::_S_get_pool;
415 if (__builtin_expect(__init ==
false,
false))
417 _S_get_pool()._M_initialize_once();
424 template<
template <
bool>
class _PoolTp>
425 struct __common_pool_base<_PoolTp, true>
426 :
public __common_pool<_PoolTp, true>
428 using __common_pool<_PoolTp,
true>::_S_get_pool;
432 { _S_get_pool()._M_initialize_once(); }
438 if (__builtin_expect(__init ==
false,
false))
440 if (__gthread_active_p())
443 static __gthread_once_t __once = __GTHREAD_ONCE_INIT;
444 __gthread_once(&__once, _S_initialize);
450 _S_get_pool()._M_initialize_once();
458 template<
template <
bool>
class _PoolTp,
bool _Thread>
461 template<
typename _Tp1,
template <
bool>
class _PoolTp1 = _PoolTp,
462 bool _Thread1 = _Thread>
466 using __common_pool_base<_PoolTp, _Thread>::_S_get_pool;
467 using __common_pool_base<_PoolTp, _Thread>::_S_initialize_once;
471 template<
typename _Tp,
template <
bool>
class _PoolTp,
bool _Thread>
472 struct __per_type_pool
474 typedef _Tp value_type;
475 typedef _PoolTp<_Thread> pool_type;
482 typedef typename pool_type::_Block_record _Block_record;
483 const static size_t __a = (__alignof__(_Tp) >=
sizeof(_Block_record)
484 ? __alignof__(_Tp) :
sizeof(_Block_record));
486 typedef typename __pool_base::_Tune _Tune;
487 static _Tune _S_tune(__a,
sizeof(_Tp) * 64,
488 sizeof(_Tp) * 2 >= __a ?
sizeof(_Tp) * 2 : __a,
489 sizeof(_Tp) * size_t(_Tune::_S_chunk_size),
490 _Tune::_S_max_threads,
491 _Tune::_S_freelist_headroom,
492 std::getenv(
"GLIBCXX_FORCE_NEW") ? true : false);
493 static pool_type _S_pool(_S_tune);
498 template<
typename _Tp,
template <
bool>
class _PoolTp,
bool _Thread>
499 struct __per_type_pool_base;
501 template<
typename _Tp,
template <
bool>
class _PoolTp>
502 struct __per_type_pool_base<_Tp, _PoolTp, false>
503 :
public __per_type_pool<_Tp, _PoolTp, false>
505 using __per_type_pool<_Tp, _PoolTp,
false>::_S_get_pool;
511 if (__builtin_expect(__init ==
false,
false))
513 _S_get_pool()._M_initialize_once();
520 template<
typename _Tp,
template <
bool>
class _PoolTp>
521 struct __per_type_pool_base<_Tp, _PoolTp, true>
522 :
public __per_type_pool<_Tp, _PoolTp, true>
524 using __per_type_pool<_Tp, _PoolTp,
true>::_S_get_pool;
528 { _S_get_pool()._M_initialize_once(); }
534 if (__builtin_expect(__init ==
false,
false))
536 if (__gthread_active_p())
539 static __gthread_once_t __once = __GTHREAD_ONCE_INIT;
540 __gthread_once(&__once, _S_initialize);
546 _S_get_pool()._M_initialize_once();
554 template<
typename _Tp,
template <
bool>
class _PoolTp,
bool _Thread>
556 :
public __per_type_pool_base<_Tp, _PoolTp, _Thread>
558 template<
typename _Tp1,
template <
bool>
class _PoolTp1 = _PoolTp,
559 bool _Thread1 = _Thread>
563 using __per_type_pool_base<_Tp, _PoolTp, _Thread>::_S_get_pool;
564 using __per_type_pool_base<_Tp, _PoolTp, _Thread>::_S_initialize_once;
569 template<
typename _Tp>
573 typedef std::size_t size_type;
574 typedef std::ptrdiff_t difference_type;
575 typedef _Tp* pointer;
576 typedef const _Tp* const_pointer;
577 typedef _Tp& reference;
578 typedef const _Tp& const_reference;
579 typedef _Tp value_type;
581#if __cplusplus >= 201103L
588 address(reference __x)
const _GLIBCXX_NOEXCEPT
592 address(const_reference __x)
const _GLIBCXX_NOEXCEPT
596 max_size()
const _GLIBCXX_USE_NOEXCEPT
597 {
return size_type(-1) /
sizeof(_Tp); }
599#if __cplusplus >= 201103L
600 template<
typename _Up,
typename... _Args>
602 construct(_Up* __p, _Args&&... __args)
603 { ::new((
void *)__p) _Up(std::forward<_Args>(__args)...); }
605 template<
typename _Up>
607 destroy(_Up* __p) { __p->~_Up(); }
612 construct(pointer __p,
const _Tp& __val)
613 { ::new((
void *)__p) _Tp(__val); }
616 destroy(pointer __p) { __p->~_Tp(); }
621#define __thread_default true
623#define __thread_default false
637 template<
typename _Tp,
642 typedef std::size_t size_type;
643 typedef std::ptrdiff_t difference_type;
644 typedef _Tp* pointer;
645 typedef const _Tp* const_pointer;
646 typedef _Tp& reference;
647 typedef const _Tp& const_reference;
648 typedef _Tp value_type;
649 typedef _Poolp __policy_type;
650 typedef typename _Poolp::pool_type __pool_type;
652 template<
typename _Tp1,
typename _Poolp1 = _Poolp>
655 typedef typename _Poolp1::template _M_rebind<_Tp1>::other pol_type;
663 template<
typename _Tp1,
typename _Poolp1>
668 _GLIBCXX_NODISCARD pointer
669 allocate(size_type __n,
const void* = 0);
672 deallocate(pointer __p, size_type __n);
674 const __pool_base::_Tune
678 return __policy_type::_S_get_pool()._M_get_options();
682 _M_set_options(__pool_base::_Tune __t)
683 { __policy_type::_S_get_pool()._M_set_options(__t); }
686 template<
typename _Tp,
typename _Poolp>
687 _GLIBCXX_NODISCARD
typename __mt_alloc<_Tp, _Poolp>::pointer
689 allocate(size_type __n,
const void*)
691 if (__n > this->max_size())
692 std::__throw_bad_alloc();
696 if (
alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
698 std::align_val_t __al = std::align_val_t(
alignof(_Tp));
699 return static_cast<_Tp*
>(::operator
new(__n *
sizeof(_Tp), __al));
703 __policy_type::_S_initialize_once();
707 __pool_type& __pool = __policy_type::_S_get_pool();
708 const size_type __bytes = __n *
sizeof(_Tp);
709 if (__pool._M_check_threshold(__bytes))
711 void* __ret = ::operator
new(__bytes);
712 return static_cast<_Tp*
>(__ret);
716 const size_type __which = __pool._M_get_binmap(__bytes);
717 const size_type __thread_id = __pool._M_get_thread_id();
722 typedef typename __pool_type::_Bin_record _Bin_record;
723 const _Bin_record& __bin = __pool._M_get_bin(__which);
724 if (__bin._M_first[__thread_id])
727 typedef typename __pool_type::_Block_record _Block_record;
728 _Block_record* __block = __bin._M_first[__thread_id];
729 __bin._M_first[__thread_id] = __block->_M_next;
731 __pool._M_adjust_freelist(__bin, __block, __thread_id);
732 __c =
reinterpret_cast<char*
>(__block) + __pool._M_get_align();
737 __c = __pool._M_reserve_block(__bytes, __thread_id);
739 return static_cast<_Tp*
>(
static_cast<void*
>(__c));
742 template<
typename _Tp,
typename _Poolp>
744 __mt_alloc<_Tp, _Poolp>::
745 deallocate(pointer __p, size_type __n)
747 if (__builtin_expect(__p != 0,
true))
751 if (
alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
753 ::operator
delete(__p, std::align_val_t(
alignof(_Tp)));
760 __pool_type& __pool = __policy_type::_S_get_pool();
761 const size_type __bytes = __n *
sizeof(_Tp);
762 if (__pool._M_check_threshold(__bytes))
763 ::operator
delete(__p);
765 __pool._M_reclaim_block(
reinterpret_cast<char*
>(__p), __bytes);
769 template<
typename _Tp,
typename _Poolp>
771 operator==(
const __mt_alloc<_Tp, _Poolp>&,
const __mt_alloc<_Tp, _Poolp>&)
774#if __cpp_impl_three_way_comparison < 201907L
775 template<
typename _Tp,
typename _Poolp>
777 operator!=(
const __mt_alloc<_Tp, _Poolp>&,
const __mt_alloc<_Tp, _Poolp>&)
781#undef __thread_default
783_GLIBCXX_END_NAMESPACE_VERSION
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
ISO C++ entities toplevel namespace is std.
GNU extensions for public use.
Base class for pool object.
Data describing the underlying memory pool, parameterized on threading support.
Policy for shared __pool objects.
Policy for individual __pool objects.
Base class for _Tp dependent member functions.
This is a fixed size (power of 2) allocator which - when compiled with thread support - will maintain...