#ifndef _MT_ALLOCATOR_H
#define _MT_ALLOCATOR_H 1
#include <new>
#include <cstdlib>
#include <bits/functexcept.h>
#include <bits/gthr.h>
#include <bits/atomicity.h>
namespace __gnu_cxx
{
typedef void (*__destroy_handler)(void*);
typedef void (*__create_handler)(void);
struct __pool_base
{
typedef unsigned short int _Binmap_type;
struct _Tune
{
enum { _S_align = 8 };
enum { _S_max_bytes = 128 };
enum { _S_min_bin = 8 };
enum { _S_chunk_size = 4096 - 4 * sizeof(void*) };
enum { _S_max_threads = 4096 };
enum { _S_freelist_headroom = 10 };
size_t _M_align;
size_t _M_max_bytes;
size_t _M_min_bin;
size_t _M_chunk_size;
size_t _M_max_threads;
size_t _M_freelist_headroom;
bool _M_force_new;
explicit
_Tune()
: _M_align(_S_align), _M_max_bytes(_S_max_bytes), _M_min_bin(_S_min_bin),
_M_chunk_size(_S_chunk_size), _M_max_threads(_S_max_threads),
_M_freelist_headroom(_S_freelist_headroom),
_M_force_new(getenv("GLIBCXX_FORCE_NEW") ? true : false)
{ }
explicit
_Tune(size_t __align, size_t __maxb, size_t __minbin, size_t __chunk,
size_t __maxthreads, size_t __headroom, bool __force)
: _M_align(__align), _M_max_bytes(__maxb), _M_min_bin(__minbin),
_M_chunk_size(__chunk), _M_max_threads(__maxthreads),
_M_freelist_headroom(__headroom), _M_force_new(__force)
{ }
bool
is_default() const
{
bool __ret = true;
__ret &= _M_align == _S_align;
__ret &= _M_max_bytes == _S_max_bytes;
__ret &= _M_min_bin == _S_min_bin;
__ret &= _M_chunk_size == _S_chunk_size;
__ret &= _M_max_threads == _S_max_threads;
__ret &= _M_freelist_headroom == _S_freelist_headroom;
return __ret;
}
};
struct _Block_address
{
void* _M_initial;
_Block_address* _M_next;
};
const _Tune&
_M_get_options() const
{ return _M_options; }
void
_M_set_options(_Tune __t)
{
if (!_M_init)
_M_options = __t;
}
bool
_M_check_threshold(size_t __bytes)
{ return __bytes > _M_options._M_max_bytes || _M_options._M_force_new; }
size_t
_M_get_binmap(size_t __bytes)
{ return _M_binmap[__bytes]; }
const size_t
_M_get_align()
{ return _M_options._M_align; }
explicit __pool_base()
: _M_options(_Tune()), _M_binmap(NULL), _M_init(false) { }
explicit __pool_base(const _Tune& __tune)
: _M_options(__tune), _M_binmap(NULL), _M_init(false) { }
protected:
_Tune _M_options;
_Binmap_type* _M_binmap;
bool _M_init;
};
template<bool _Thread>
class __pool;
template<>
class __pool<true>;
template<>
class __pool<false>;
#ifdef __GTHREADS
template<>
class __pool<true> : public __pool_base
{
public:
struct _Thread_record
{
_Thread_record* volatile _M_next;
size_t _M_id;
};
union _Block_record
{
_Block_record* volatile _M_next;
size_t _M_thread_id;
};
struct _Bin_record
{
_Block_record** volatile _M_first;
_Block_address* _M_address;
size_t* volatile _M_free;
size_t* volatile _M_used;
__gthread_mutex_t* _M_mutex;
};
void
_M_initialize(__destroy_handler __d);
void
_M_initialize_once(__create_handler __c)
{
if (__builtin_expect(_M_init == false, false))
{
if (__gthread_active_p())
__gthread_once(&_M_once, __c);
if (!_M_init)
__c();
}
}
void
_M_destroy() throw();
char*
_M_reserve_block(size_t __bytes, const size_t __thread_id);
void
_M_reclaim_block(char* __p, size_t __bytes);
const _Bin_record&
_M_get_bin(size_t __which)
{ return _M_bin[__which]; }
void
_M_adjust_freelist(const _Bin_record& __bin, _Block_record* __block,
size_t __thread_id)
{
if (__gthread_active_p())
{
__block->_M_thread_id = __thread_id;
--__bin._M_free[__thread_id];
++__bin._M_used[__thread_id];
}
}
void
_M_destroy_thread_key(void* __freelist_pos);
size_t
_M_get_thread_id();
explicit __pool()
: _M_bin(NULL), _M_bin_size(1), _M_thread_freelist(NULL)
{
__gthread_once_t __tmp = __GTHREAD_ONCE_INIT;
_M_once = __tmp;
}
explicit __pool(const __pool_base::_Tune& __tune)
: __pool_base(__tune), _M_bin(NULL), _M_bin_size(1),
_M_thread_freelist(NULL)
{
__gthread_once_t __tmp = __GTHREAD_ONCE_INIT;
_M_once = __tmp;
}
~__pool() { }
private:
_Bin_record* volatile _M_bin;
size_t _M_bin_size;
__gthread_once_t _M_once;
_Thread_record* _M_thread_freelist;
void* _M_thread_freelist_initial;
};
#endif
template<>
class __pool<false> : public __pool_base
{
public:
union _Block_record
{
_Block_record* volatile _M_next;
};
struct _Bin_record
{
_Block_record** volatile _M_first;
_Block_address* _M_address;
};
void
_M_initialize_once()
{
if (__builtin_expect(_M_init == false, false))
_M_initialize();
}
void
_M_destroy() throw();
char*
_M_reserve_block(size_t __bytes, const size_t __thread_id);
void
_M_reclaim_block(char* __p, size_t __bytes);
size_t
_M_get_thread_id() { return 0; }
const _Bin_record&
_M_get_bin(size_t __which)
{ return _M_bin[__which]; }
void
_M_adjust_freelist(const _Bin_record&, _Block_record*, size_t)
{ }
explicit __pool()
: _M_bin(NULL), _M_bin_size(1) { }
explicit __pool(const __pool_base::_Tune& __tune)
: __pool_base(__tune), _M_bin(NULL), _M_bin_size(1) { }
~__pool() { }
private:
_Bin_record* volatile _M_bin;
size_t _M_bin_size;
void
_M_initialize();
};
template<bool _Thread>
struct __common_pool_policy
{
typedef __pool<_Thread> __pool_type;
template<typename _Tp1, bool _Thread1 = _Thread>
struct _M_rebind;
template<typename _Tp1>
struct _M_rebind<_Tp1, true>
{ typedef __common_pool_policy<true> other; };
template<typename _Tp1>
struct _M_rebind<_Tp1, false>
{ typedef __common_pool_policy<false> other; };
static __pool_type&
_S_get_pool()
{
static __pool_type _S_pool;
return _S_pool;
}
static void
_S_initialize_once()
{
static bool __init;
if (__builtin_expect(__init == false, false))
{
_S_get_pool()._M_initialize_once();
__init = true;
}
}
};
template<>
struct __common_pool_policy<true>;
#ifdef __GTHREADS
template<>
struct __common_pool_policy<true>
{
typedef __pool<true> __pool_type;
template<typename _Tp1, bool _Thread1 = true>
struct _M_rebind;
template<typename _Tp1>
struct _M_rebind<_Tp1, true>
{ typedef __common_pool_policy<true> other; };
template<typename _Tp1>
struct _M_rebind<_Tp1, false>
{ typedef __common_pool_policy<false> other; };
static __pool_type&
_S_get_pool()
{
static __pool_type _S_pool;
return _S_pool;
}
static void
_S_destroy_thread_key(void* __freelist_pos)
{ _S_get_pool()._M_destroy_thread_key(__freelist_pos); }
static void
_S_initialize()
{ _S_get_pool()._M_initialize(_S_destroy_thread_key); }
static void
_S_initialize_once()
{
static bool __init;
if (__builtin_expect(__init == false, false))
{
_S_get_pool()._M_initialize_once(_S_initialize);
__init = true;
}
}
};
#endif
template<typename _Tp, bool _Thread>
struct __per_type_pool_policy
{
typedef __pool<_Thread> __pool_type;
template<typename _Tp1, bool _Thread1 = _Thread>
struct _M_rebind;
template<typename _Tp1>
struct _M_rebind<_Tp1, false>
{ typedef __per_type_pool_policy<_Tp1, false> other; };
template<typename _Tp1>
struct _M_rebind<_Tp1, true>
{ typedef __per_type_pool_policy<_Tp1, true> other; };
static __pool_type&
_S_get_pool()
{
const static size_t __align = __alignof__(_Tp) >= sizeof(typename __pool_type::_Block_record) ? __alignof__(_Tp) : sizeof(typename __pool_type::_Block_record);
static __pool_base::_Tune _S_tune(__align, sizeof(_Tp) * 128, (sizeof(_Tp) * 2) >= __align ? sizeof(_Tp) * 2 : __align, __pool_type::_Tune::_S_chunk_size, __pool_type::_Tune::_S_max_threads, __pool_type::_Tune::_S_freelist_headroom, getenv("GLIBCXX_FORCE_NEW") ? true : false);
static __pool_type _S_pool(_S_tune);
return _S_pool;
}
static void
_S_initialize_once()
{
static bool __init;
if (__builtin_expect(__init == false, false))
{
_S_get_pool()._M_initialize_once();
__init = true;
}
}
};
template<typename _Tp>
struct __per_type_pool_policy<_Tp, true>;
#ifdef __GTHREADS
template<typename _Tp>
struct __per_type_pool_policy<_Tp, true>
{
typedef __pool<true> __pool_type;
template<typename _Tp1, bool _Thread1 = true>
struct _M_rebind;
template<typename _Tp1>
struct _M_rebind<_Tp1, false>
{ typedef __per_type_pool_policy<_Tp1, false> other; };
template<typename _Tp1>
struct _M_rebind<_Tp1, true>
{ typedef __per_type_pool_policy<_Tp1, true> other; };
static __pool_type&
_S_get_pool( )
{
const static size_t __align = __alignof__(_Tp) >= sizeof(typename __pool_type::_Block_record) ? __alignof__(_Tp) : sizeof(typename __pool_type::_Block_record);
static __pool_base::_Tune _S_tune(__align, sizeof(_Tp) * 128, (sizeof(_Tp) * 2) >= __align ? sizeof(_Tp) * 2 : __align, __pool_type::_Tune::_S_chunk_size, __pool_type::_Tune::_S_max_threads, __pool_type::_Tune::_S_freelist_headroom, getenv("GLIBCXX_FORCE_NEW") ? true : false);
static __pool_type _S_pool(_S_tune);
return _S_pool;
}
static void
_S_destroy_thread_key(void* __freelist_pos)
{ _S_get_pool()._M_destroy_thread_key(__freelist_pos); }
static void
_S_initialize()
{ _S_get_pool()._M_initialize(_S_destroy_thread_key); }
static void
_S_initialize_once()
{
static bool __init;
if (__builtin_expect(__init == false, false))
{
_S_get_pool()._M_initialize_once(_S_initialize);
__init = true;
}
}
};
#endif
template<typename _Tp>
class __mt_alloc_base
{
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;
pointer
address(reference __x) const
{ return &__x; }
const_pointer
address(const_reference __x) const
{ return &__x; }
size_type
max_size() const throw()
{ return size_t(-1) / sizeof(_Tp); }
void
construct(pointer __p, const _Tp& __val)
{ ::new(__p) _Tp(__val); }
void
destroy(pointer __p) { __p->~_Tp(); }
};
#ifdef __GTHREADS
#define __default_policy __common_pool_policy<true>
#else
#define __default_policy __common_pool_policy<false>
#endif
template<typename _Tp, typename _Poolp = __default_policy>
class __mt_alloc : public __mt_alloc_base<_Tp>, _Poolp
{
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;
typedef _Poolp __policy_type;
typedef typename _Poolp::__pool_type __pool_type;
template<typename _Tp1, typename _Poolp1 = _Poolp>
struct rebind
{
typedef typename _Poolp1::template _M_rebind<_Tp1>::other pol_type;
typedef __mt_alloc<_Tp1, pol_type> other;
};
__mt_alloc() throw()
{ __policy_type::_S_get_pool(); }
__mt_alloc(const __mt_alloc&) throw()
{ __policy_type::_S_get_pool(); }
template<typename _Tp1, typename _Poolp1>
__mt_alloc(const __mt_alloc<_Tp1, _Poolp1>& obj) throw()
{ __policy_type::_S_get_pool(); }
~__mt_alloc() throw() { }
pointer
allocate(size_type __n, const void* = 0);
void
deallocate(pointer __p, size_type __n);
const __pool_base::_Tune
_M_get_options()
{
return __pool_base::_Tune(this->_S_get_pool()._M_get_options());
}
void
_M_set_options(__pool_base::_Tune __t)
{ this->_S_get_pool()._M_set_options(__t); }
};
template<typename _Tp, typename _Poolp>
typename __mt_alloc<_Tp, _Poolp>::pointer
__mt_alloc<_Tp, _Poolp>::
allocate(size_type __n, const void*)
{
this->_S_initialize_once();
if (__builtin_expect(__n > this->max_size(), false))
std::__throw_bad_alloc();
__pool_type& __pool = this->_S_get_pool();
const size_t __bytes = __n * sizeof(_Tp);
if (__pool._M_check_threshold(__bytes))
{
void* __ret = ::operator new(__bytes);
return static_cast<_Tp*>(__ret);
}
const size_t __which = __pool._M_get_binmap(__bytes);
const size_t __thread_id = __pool._M_get_thread_id();
char* __c;
typedef typename __pool_type::_Bin_record _Bin_record;
const _Bin_record& __bin = __pool._M_get_bin(__which);
if (__bin._M_first[__thread_id])
{
typedef typename __pool_type::_Block_record _Block_record;
_Block_record* __block = __bin._M_first[__thread_id];
__bin._M_first[__thread_id] = __block->_M_next;
__pool._M_adjust_freelist(__bin, __block, __thread_id);
__c = reinterpret_cast<char*>(__block) + __pool._M_get_align();
}
else
{
__c = __pool._M_reserve_block(__bytes, __thread_id);
}
return static_cast<_Tp*>(static_cast<void*>(__c));
}
template<typename _Tp, typename _Poolp>
void
__mt_alloc<_Tp, _Poolp>::
deallocate(pointer __p, size_type __n)
{
if (__builtin_expect(__p != 0, true))
{
__pool_type& __pool = this->_S_get_pool();
const size_t __bytes = __n * sizeof(_Tp);
if (__pool._M_check_threshold(__bytes))
::operator delete(__p);
else
__pool._M_reclaim_block(reinterpret_cast<char*>(__p), __bytes);
}
}
template<typename _Tp, typename _Poolp>
inline bool
operator==(const __mt_alloc<_Tp, _Poolp>&, const __mt_alloc<_Tp, _Poolp>&)
{ return true; }
template<typename _Tp, typename _Poolp>
inline bool
operator!=(const __mt_alloc<_Tp, _Poolp>&, const __mt_alloc<_Tp, _Poolp>&)
{ return false; }
#undef __default_policy
}
#endif