#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);
class __pool_base
{
public:
struct _Tune
{
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(8), _M_max_bytes(128), _M_min_bin(8),
_M_chunk_size(4096 - 4 * sizeof(void*)),
_M_max_threads(4096), _M_freelist_headroom(10),
_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)
{ }
};
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]; }
explicit __pool_base()
: _M_init(false), _M_options(_Tune()), _M_binmap(NULL) { }
protected:
bool _M_init;
_Tune _M_options;
typedef unsigned short int _Binmap_type;
_Binmap_type* _M_binmap;
};
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;
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();
}
}
char*
_M_reserve_memory(size_t __bytes, const size_t __thread_id);
void
_M_reclaim_memory(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;
}
private:
_Bin_record* volatile _M_bin;
size_t _M_bin_size;
__gthread_once_t _M_once;
_Thread_record* _M_thread_freelist;
};
#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;
};
void
_M_initialize_once()
{
if (__builtin_expect(_M_init == false, false))
_M_initialize();
}
char*
_M_reserve_memory(size_t __bytes, const size_t __thread_id);
void
_M_reclaim_memory(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) { }
private:
_Bin_record* volatile _M_bin;
size_t _M_bin_size;
void
_M_initialize();
};
template<bool _Thread>
struct __common_pool_policy
{
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; };
typedef __pool<_Thread> __pool_type;
static __pool_type _S_data;
static __pool_type&
_S_get_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>
{
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; };
typedef __pool<true> __pool_type;
static __pool_type _S_data;
static __pool_type&
_S_get_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
{
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; };
typedef __pool<_Thread> __pool_type;
static __pool_type _S_data;
static __pool_type&
_S_get_pool( ) { return _S_data; }
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, bool _Thread>
__pool<_Thread>
__per_type_pool_policy<_Tp, _Thread>::_S_data;
template<typename _Tp>
struct __per_type_pool_policy<_Tp, true>;
#ifdef __GTHREADS
template<typename _Tp>
struct __per_type_pool_policy<_Tp, true>
{
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; };
typedef __pool<true> __pool_type;
static __pool_type _S_data;
static __pool_type&
_S_get_pool( ) { return _S_data; }
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;
}
}
};
template<typename _Tp>
__pool<true>
__per_type_pool_policy<_Tp, true>::_S_data;
#endif
#ifdef __GTHREADS
typedef __common_pool_policy<true> __default_policy;
#else
typedef __common_pool_policy<false> __default_policy;
#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(); }
};
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()
{
}
__mt_alloc(const __mt_alloc&) throw()
{
}
template<typename _Tp1, typename _Poolp1>
__mt_alloc(const __mt_alloc<_Tp1, _Poolp1>& obj) throw()
{
}
~__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();
__pool_type& __pl = this->_S_get_pool();
const size_t __bytes = __n * sizeof(_Tp);
if (__pl._M_check_threshold(__bytes))
{
void* __ret = ::operator new(__bytes);
return static_cast<_Tp*>(__ret);
}
const size_t __which = __pl._M_get_binmap(__bytes);
const size_t __thread_id = __pl._M_get_thread_id();
char* __c;
typedef typename __pool_type::_Bin_record _Bin_record;
const _Bin_record& __bin = __pl._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] = __bin._M_first[__thread_id]->_M_next;
__pl._M_adjust_freelist(__bin, __block, __thread_id);
const __pool_base::_Tune& __options = __pl._M_get_options();
__c = reinterpret_cast<char*>(__block) + __options._M_align;
}
else
{
__c = __pl._M_reserve_memory(__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)
{
__pool_type& __pl = this->_S_get_pool();
const size_t __bytes = __n * sizeof(_Tp);
if (__pl._M_check_threshold(__bytes))
::operator delete(__p);
else
__pl._M_reclaim_memory(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; }
}
#endif