bitmap_allocator.h [plain text]
#ifndef _BITMAP_ALLOCATOR_H
#define _BITMAP_ALLOCATOR_H 1
#include <cstddef>
#include <bits/functexcept.h>
#include <utility>
#include <functional>
#include <new>
#include <bits/gthr.h>
#define _BALLOC_ALIGN_BYTES 8
#if defined _BALLOC_SANITY_CHECK
#include <cassert>
#define _BALLOC_ASSERT(_EXPR) assert(_EXPR)
#else
#define _BALLOC_ASSERT(_EXPR)
#endif
namespace __gnu_cxx
{
#if defined __GTHREADS
namespace
{
bool const __threads_enabled = __gthread_active_p();
}
#endif
#if defined __GTHREADS
class _Mutex
{
__gthread_mutex_t _M_mut;
_Mutex(_Mutex const&);
_Mutex& operator=(_Mutex const&);
public:
_Mutex()
{
if (__threads_enabled)
{
#if !defined __GTHREAD_MUTEX_INIT
__GTHREAD_MUTEX_INIT_FUNCTION(&_M_mut);
#else
__gthread_mutex_t __mtemp = __GTHREAD_MUTEX_INIT;
_M_mut = __mtemp;
#endif
}
}
~_Mutex()
{
}
__gthread_mutex_t*
_M_get() { return &_M_mut; }
};
class _Lock
{
_Mutex* _M_pmt;
bool _M_locked;
_Lock(_Lock const&);
_Lock& operator=(_Lock const&);
public:
_Lock(_Mutex* __mptr)
: _M_pmt(__mptr), _M_locked(false)
{ }
void
_M_lock()
{
if (__threads_enabled)
{
_M_locked = true;
__gthread_mutex_lock(_M_pmt->_M_get());
}
}
void
_M_unlock()
{
if (__threads_enabled)
{
if (__builtin_expect(_M_locked, true))
{
__gthread_mutex_unlock(_M_pmt->_M_get());
_M_locked = false;
}
}
}
~_Lock() { }
};
class _Auto_Lock
{
_Mutex* _M_pmt;
_Auto_Lock(_Auto_Lock const&);
_Auto_Lock& operator=(_Auto_Lock const&);
void
_M_lock()
{
if (__threads_enabled)
__gthread_mutex_lock(_M_pmt->_M_get());
}
void
_M_unlock()
{
if (__threads_enabled)
__gthread_mutex_unlock(_M_pmt->_M_get());
}
public:
_Auto_Lock(_Mutex* __mptr) : _M_pmt(__mptr)
{ this->_M_lock(); }
~_Auto_Lock() { this->_M_unlock(); }
};
#endif
namespace balloc
{
template<typename _Tp>
class __mini_vector
{
__mini_vector(const __mini_vector&);
__mini_vector& operator=(const __mini_vector&);
public:
typedef _Tp value_type;
typedef _Tp* pointer;
typedef _Tp& reference;
typedef const _Tp& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef pointer iterator;
private:
pointer _M_start;
pointer _M_finish;
pointer _M_end_of_storage;
size_type
_M_space_left() const throw()
{ return _M_end_of_storage - _M_finish; }
pointer
allocate(size_type __n)
{ return static_cast<pointer>(::operator new(__n * sizeof(_Tp))); }
void
deallocate(pointer __p, size_type)
{ ::operator delete(__p); }
public:
__mini_vector() : _M_start(0), _M_finish(0),
_M_end_of_storage(0)
{ }
#if 0
~__mini_vector()
{
if (this->_M_start)
{
this->deallocate(this->_M_start, this->_M_end_of_storage
- this->_M_start);
}
}
#endif
size_type
size() const throw()
{ return _M_finish - _M_start; }
iterator
begin() const throw()
{ return this->_M_start; }
iterator
end() const throw()
{ return this->_M_finish; }
reference
back() const throw()
{ return *(this->end() - 1); }
reference
operator[](const size_type __pos) const throw()
{ return this->_M_start[__pos]; }
void
insert(iterator __pos, const_reference __x);
void
push_back(const_reference __x)
{
if (this->_M_space_left())
{
*this->end() = __x;
++this->_M_finish;
}
else
this->insert(this->end(), __x);
}
void
pop_back() throw()
{ --this->_M_finish; }
void
erase(iterator __pos) throw();
void
clear() throw()
{ this->_M_finish = this->_M_start; }
};
template<typename _Tp>
void __mini_vector<_Tp>::
insert(iterator __pos, const_reference __x)
{
if (this->_M_space_left())
{
size_type __to_move = this->_M_finish - __pos;
iterator __dest = this->end();
iterator __src = this->end() - 1;
++this->_M_finish;
while (__to_move)
{
*__dest = *__src;
--__dest; --__src; --__to_move;
}
*__pos = __x;
}
else
{
size_type __new_size = this->size() ? this->size() * 2 : 1;
iterator __new_start = this->allocate(__new_size);
iterator __first = this->begin();
iterator __start = __new_start;
while (__first != __pos)
{
*__start = *__first;
++__start; ++__first;
}
*__start = __x;
++__start;
while (__first != this->end())
{
*__start = *__first;
++__start; ++__first;
}
if (this->_M_start)
this->deallocate(this->_M_start, this->size());
this->_M_start = __new_start;
this->_M_finish = __start;
this->_M_end_of_storage = this->_M_start + __new_size;
}
}
template<typename _Tp>
void __mini_vector<_Tp>::
erase(iterator __pos) throw()
{
while (__pos + 1 != this->end())
{
*__pos = __pos[1];
++__pos;
}
--this->_M_finish;
}
template<typename _Tp>
struct __mv_iter_traits
{
typedef typename _Tp::value_type value_type;
typedef typename _Tp::difference_type difference_type;
};
template<typename _Tp>
struct __mv_iter_traits<_Tp*>
{
typedef _Tp value_type;
typedef std::ptrdiff_t difference_type;
};
enum
{
bits_per_byte = 8,
bits_per_block = sizeof(size_t) * bits_per_byte
};
template<typename _ForwardIterator, typename _Tp, typename _Compare>
_ForwardIterator
__lower_bound(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __val, _Compare __comp)
{
typedef typename __mv_iter_traits<_ForwardIterator>::value_type
_ValueType;
typedef typename __mv_iter_traits<_ForwardIterator>::difference_type
_DistanceType;
_DistanceType __len = __last - __first;
_DistanceType __half;
_ForwardIterator __middle;
while (__len > 0)
{
__half = __len >> 1;
__middle = __first;
__middle += __half;
if (__comp(*__middle, __val))
{
__first = __middle;
++__first;
__len = __len - __half - 1;
}
else
__len = __half;
}
return __first;
}
template<typename _InputIterator, typename _Predicate>
inline _InputIterator
__find_if(_InputIterator __first, _InputIterator __last, _Predicate __p)
{
while (__first != __last && !__p(*__first))
++__first;
return __first;
}
template<typename _AddrPair>
inline size_t
__num_blocks(_AddrPair __ap)
{ return (__ap.second - __ap.first) + 1; }
template<typename _AddrPair>
inline size_t
__num_bitmaps(_AddrPair __ap)
{ return __num_blocks(__ap) / bits_per_block; }
template<typename _Tp>
class _Inclusive_between
: public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
{
typedef _Tp pointer;
pointer _M_ptr_value;
typedef typename std::pair<_Tp, _Tp> _Block_pair;
public:
_Inclusive_between(pointer __ptr) : _M_ptr_value(__ptr)
{ }
bool
operator()(_Block_pair __bp) const throw()
{
if (std::less_equal<pointer>()(_M_ptr_value, __bp.second)
&& std::greater_equal<pointer>()(_M_ptr_value, __bp.first))
return true;
else
return false;
}
};
template<typename _Functor>
class _Functor_Ref
: public std::unary_function<typename _Functor::argument_type,
typename _Functor::result_type>
{
_Functor& _M_fref;
public:
typedef typename _Functor::argument_type argument_type;
typedef typename _Functor::result_type result_type;
_Functor_Ref(_Functor& __fref) : _M_fref(__fref)
{ }
result_type
operator()(argument_type __arg)
{ return _M_fref(__arg); }
};
template<typename _Tp>
class _Ffit_finder
: public std::unary_function<typename std::pair<_Tp, _Tp>, bool>
{
typedef typename std::pair<_Tp, _Tp> _Block_pair;
typedef typename balloc::__mini_vector<_Block_pair> _BPVector;
typedef typename _BPVector::difference_type _Counter_type;
size_t* _M_pbitmap;
_Counter_type _M_data_offset;
public:
_Ffit_finder() : _M_pbitmap(0), _M_data_offset(0)
{ }
bool
operator()(_Block_pair __bp) throw()
{
_Counter_type __diff =
__gnu_cxx::balloc::__num_bitmaps(__bp);
if (*(reinterpret_cast<size_t*>
(__bp.first) - (__diff + 1))
== __gnu_cxx::balloc::__num_blocks(__bp))
return false;
size_t* __rover = reinterpret_cast<size_t*>(__bp.first) - 1;
for (_Counter_type __i = 0; __i < __diff; ++__i)
{
_M_data_offset = __i;
if (*__rover)
{
_M_pbitmap = __rover;
return true;
}
--__rover;
}
return false;
}
size_t*
_M_get() const throw()
{ return _M_pbitmap; }
_Counter_type
_M_offset() const throw()
{ return _M_data_offset * bits_per_block; }
};
template<typename _Tp>
class _Bitmap_counter
{
typedef typename balloc::__mini_vector<typename std::pair<_Tp, _Tp> >
_BPVector;
typedef typename _BPVector::size_type _Index_type;
typedef _Tp pointer;
_BPVector& _M_vbp;
size_t* _M_curr_bmap;
size_t* _M_last_bmap_in_block;
_Index_type _M_curr_index;
public:
_Bitmap_counter(_BPVector& Rvbp, long __index = -1) : _M_vbp(Rvbp)
{ this->_M_reset(__index); }
void
_M_reset(long __index = -1) throw()
{
if (__index == -1)
{
_M_curr_bmap = 0;
_M_curr_index = static_cast<_Index_type>(-1);
return;
}
_M_curr_index = __index;
_M_curr_bmap = reinterpret_cast<size_t*>
(_M_vbp[_M_curr_index].first) - 1;
_BALLOC_ASSERT(__index <= (long)_M_vbp.size() - 1);
_M_last_bmap_in_block = _M_curr_bmap
- ((_M_vbp[_M_curr_index].second
- _M_vbp[_M_curr_index].first + 1)
/ bits_per_block - 1);
}
void
_M_set_internal_bitmap(size_t* __new_internal_marker) throw()
{ _M_curr_bmap = __new_internal_marker; }
bool
_M_finished() const throw()
{ return(_M_curr_bmap == 0); }
_Bitmap_counter&
operator++() throw()
{
if (_M_curr_bmap == _M_last_bmap_in_block)
{
if (++_M_curr_index == _M_vbp.size())
_M_curr_bmap = 0;
else
this->_M_reset(_M_curr_index);
}
else
--_M_curr_bmap;
return *this;
}
size_t*
_M_get() const throw()
{ return _M_curr_bmap; }
pointer
_M_base() const throw()
{ return _M_vbp[_M_curr_index].first; }
_Index_type
_M_offset() const throw()
{
return bits_per_block
* ((reinterpret_cast<size_t*>(this->_M_base())
- _M_curr_bmap) - 1);
}
_Index_type
_M_where() const throw()
{ return _M_curr_index; }
};
inline void
__bit_allocate(size_t* __pbmap, size_t __pos) throw()
{
size_t __mask = 1 << __pos;
__mask = ~__mask;
*__pbmap &= __mask;
}
inline void
__bit_free(size_t* __pbmap, size_t __pos) throw()
{
size_t __mask = 1 << __pos;
*__pbmap |= __mask;
}
}
inline size_t
_Bit_scan_forward(size_t __num)
{ return static_cast<size_t>(__builtin_ctzl(__num)); }
class free_list
{
typedef size_t* value_type;
typedef balloc::__mini_vector<value_type> vector_type;
typedef vector_type::iterator iterator;
struct _LT_pointer_compare
{
bool
operator()(const size_t* __pui,
const size_t __cui) const throw()
{ return *__pui < __cui; }
};
#if defined __GTHREADS
_Mutex*
_M_get_mutex()
{
static _Mutex _S_mutex;
return &_S_mutex;
}
#endif
vector_type&
_M_get_free_list()
{
static vector_type _S_free_list;
return _S_free_list;
}
void
_M_validate(size_t* __addr) throw()
{
vector_type& __free_list = _M_get_free_list();
const vector_type::size_type __max_size = 64;
if (__free_list.size() >= __max_size)
{
if (*__addr >= *__free_list.back())
{
::operator delete(static_cast<void*>(__addr));
return;
}
else
{
::operator delete(static_cast<void*>(__free_list.back()));
__free_list.pop_back();
}
}
iterator __temp = __gnu_cxx::balloc::__lower_bound
(__free_list.begin(), __free_list.end(),
*__addr, _LT_pointer_compare());
__free_list.insert(__temp, __addr);
}
bool
_M_should_i_give(size_t __block_size,
size_t __required_size) throw()
{
const size_t __max_wastage_percentage = 36;
if (__block_size >= __required_size &&
(((__block_size - __required_size) * 100 / __block_size)
< __max_wastage_percentage))
return true;
else
return false;
}
public:
inline void
_M_insert(size_t* __addr) throw()
{
#if defined __GTHREADS
_Auto_Lock __bfl_lock(_M_get_mutex());
#endif
this->_M_validate(reinterpret_cast<size_t*>(__addr) - 1);
}
size_t*
_M_get(size_t __sz) throw(std::bad_alloc);
void
_M_clear();
};
template<typename _Tp>
class bitmap_allocator;
template<>
class bitmap_allocator<void>
{
public:
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template<typename _Tp1>
struct rebind
{
typedef bitmap_allocator<_Tp1> other;
};
};
template<typename _Tp>
class bitmap_allocator : private free_list
{
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef const _Tp* const_pointer;
typedef _Tp& reference;
typedef const _Tp& const_reference;
typedef _Tp value_type;
template<typename _Tp1>
struct rebind
{
typedef bitmap_allocator<_Tp1> other;
};
private:
template<size_t _BSize, size_t _AlignSize>
struct aligned_size
{
enum
{
modulus = _BSize % _AlignSize,
value = _BSize + (modulus ? _AlignSize - (modulus) : 0)
};
};
struct _Alloc_block
{
char __M_unused[aligned_size<sizeof(value_type),
_BALLOC_ALIGN_BYTES>::value];
};
typedef typename std::pair<_Alloc_block*, _Alloc_block*> _Block_pair;
typedef typename
balloc::__mini_vector<_Block_pair> _BPVector;
#if defined _BALLOC_SANITY_CHECK
void
_S_check_for_free_blocks() throw()
{
typedef typename
__gnu_cxx::balloc::_Ffit_finder<_Alloc_block*> _FFF;
_FFF __fff;
typedef typename _BPVector::iterator _BPiter;
_BPiter __bpi =
__gnu_cxx::balloc::__find_if
(_S_mem_blocks.begin(), _S_mem_blocks.end(),
__gnu_cxx::balloc::_Functor_Ref<_FFF>(__fff));
_BALLOC_ASSERT(__bpi == _S_mem_blocks.end());
}
#endif
void
_S_refill_pool() throw(std::bad_alloc)
{
#if defined _BALLOC_SANITY_CHECK
_S_check_for_free_blocks();
#endif
const size_t __num_bitmaps = _S_block_size / balloc::bits_per_block;
const size_t __size_to_allocate = sizeof(size_t)
+ _S_block_size * sizeof(_Alloc_block)
+ __num_bitmaps * sizeof(size_t);
size_t* __temp =
reinterpret_cast<size_t*>
(this->_M_get(__size_to_allocate));
*__temp = 0;
++__temp;
_Block_pair __bp =
std::make_pair(reinterpret_cast<_Alloc_block*>
(__temp + __num_bitmaps),
reinterpret_cast<_Alloc_block*>
(__temp + __num_bitmaps)
+ _S_block_size - 1);
_S_mem_blocks.push_back(__bp);
size_t __bit_mask = 0; __bit_mask = ~__bit_mask;
for (size_t __i = 0; __i < __num_bitmaps; ++__i)
__temp[__i] = __bit_mask;
_S_block_size *= 2;
}
static _BPVector _S_mem_blocks;
static size_t _S_block_size;
static __gnu_cxx::balloc::
_Bitmap_counter<_Alloc_block*> _S_last_request;
static typename _BPVector::size_type _S_last_dealloc_index;
#if defined __GTHREADS
static _Mutex _S_mut;
#endif
public:
pointer
_M_allocate_single_object() throw(std::bad_alloc)
{
#if defined __GTHREADS
_Auto_Lock __bit_lock(&_S_mut);
#endif
while (_S_last_request._M_finished() == false
&& (*(_S_last_request._M_get()) == 0))
{
_S_last_request.operator++();
}
if (__builtin_expect(_S_last_request._M_finished() == true, false))
{
typedef typename
__gnu_cxx::balloc::_Ffit_finder<_Alloc_block*> _FFF;
_FFF __fff;
typedef typename _BPVector::iterator _BPiter;
_BPiter __bpi =
__gnu_cxx::balloc::__find_if
(_S_mem_blocks.begin(), _S_mem_blocks.end(),
__gnu_cxx::balloc::_Functor_Ref<_FFF>(__fff));
if (__bpi != _S_mem_blocks.end())
{
size_t __nz_bit = _Bit_scan_forward(*__fff._M_get());
balloc::__bit_allocate(__fff._M_get(), __nz_bit);
_S_last_request._M_reset(__bpi - _S_mem_blocks.begin());
pointer __ret = reinterpret_cast<pointer>
(__bpi->first + __fff._M_offset() + __nz_bit);
size_t* __puse_count =
reinterpret_cast<size_t*>
(__bpi->first)
- (__gnu_cxx::balloc::__num_bitmaps(*__bpi) + 1);
++(*__puse_count);
return __ret;
}
else
{
_S_refill_pool();
_S_last_request._M_reset(_S_mem_blocks.size() - 1);
}
}
size_t __nz_bit = _Bit_scan_forward(*_S_last_request._M_get());
balloc::__bit_allocate(_S_last_request._M_get(), __nz_bit);
pointer __ret = reinterpret_cast<pointer>
(_S_last_request._M_base() + _S_last_request._M_offset() + __nz_bit);
size_t* __puse_count = reinterpret_cast<size_t*>
(_S_mem_blocks[_S_last_request._M_where()].first)
- (__gnu_cxx::balloc::
__num_bitmaps(_S_mem_blocks[_S_last_request._M_where()]) + 1);
++(*__puse_count);
return __ret;
}
void
_M_deallocate_single_object(pointer __p) throw()
{
#if defined __GTHREADS
_Auto_Lock __bit_lock(&_S_mut);
#endif
_Alloc_block* __real_p = reinterpret_cast<_Alloc_block*>(__p);
typedef typename _BPVector::iterator _Iterator;
typedef typename _BPVector::difference_type _Difference_type;
_Difference_type __diff;
long __displacement;
_BALLOC_ASSERT(_S_last_dealloc_index >= 0);
if (__gnu_cxx::balloc::_Inclusive_between<_Alloc_block*>
(__real_p)
(_S_mem_blocks[_S_last_dealloc_index]))
{
_BALLOC_ASSERT(_S_last_dealloc_index <= _S_mem_blocks.size() - 1);
__diff = _S_last_dealloc_index;
__displacement = __real_p - _S_mem_blocks[__diff].first;
}
else
{
_Iterator _iter =
__gnu_cxx::balloc::
__find_if(_S_mem_blocks.begin(),
_S_mem_blocks.end(),
__gnu_cxx::balloc::
_Inclusive_between<_Alloc_block*>(__real_p));
_BALLOC_ASSERT(_iter != _S_mem_blocks.end());
__diff = _iter - _S_mem_blocks.begin();
__displacement = __real_p - _S_mem_blocks[__diff].first;
_S_last_dealloc_index = __diff;
}
const size_t __rotate = __displacement % balloc::bits_per_block;
size_t* __bitmapC =
reinterpret_cast<size_t*>
(_S_mem_blocks[__diff].first) - 1;
__bitmapC -= (__displacement / balloc::bits_per_block);
balloc::__bit_free(__bitmapC, __rotate);
size_t* __puse_count = reinterpret_cast<size_t*>
(_S_mem_blocks[__diff].first)
- (__gnu_cxx::balloc::__num_bitmaps(_S_mem_blocks[__diff]) + 1);
_BALLOC_ASSERT(*__puse_count != 0);
--(*__puse_count);
if (__builtin_expect(*__puse_count == 0, false))
{
_S_block_size /= 2;
this->_M_insert(__puse_count);
_S_mem_blocks.erase(_S_mem_blocks.begin() + __diff);
if ((_Difference_type)_S_last_request._M_where() >= __diff--)
_S_last_request._M_reset(__diff);
if (_S_last_dealloc_index >= _S_mem_blocks.size())
{
_S_last_dealloc_index =(__diff != -1 ? __diff : 0);
_BALLOC_ASSERT(_S_last_dealloc_index >= 0);
}
}
}
public:
bitmap_allocator() throw()
{ }
bitmap_allocator(const bitmap_allocator&)
{ }
template<typename _Tp1>
bitmap_allocator(const bitmap_allocator<_Tp1>&) throw()
{ }
~bitmap_allocator() throw()
{ }
pointer
allocate(size_type __n)
{
if (__builtin_expect(__n > this->max_size(), false))
std::__throw_bad_alloc();
if (__builtin_expect(__n == 1, true))
return this->_M_allocate_single_object();
else
{
const size_type __b = __n * sizeof(value_type);
return reinterpret_cast<pointer>(::operator new(__b));
}
}
pointer
allocate(size_type __n, typename bitmap_allocator<void>::const_pointer)
{ return allocate(__n); }
void
deallocate(pointer __p, size_type __n) throw()
{
if (__builtin_expect(__p != 0, true))
{
if (__builtin_expect(__n == 1, true))
this->_M_deallocate_single_object(__p);
else
::operator delete(__p);
}
}
pointer
address(reference __r) const
{ return &__r; }
const_pointer
address(const_reference __r) const
{ return &__r; }
size_type
max_size() const throw()
{ return size_type(-1) / sizeof(value_type); }
void
construct(pointer __p, const_reference __data)
{ ::new(__p) value_type(__data); }
void
destroy(pointer __p)
{ __p->~value_type(); }
};
template<typename _Tp1, typename _Tp2>
bool
operator==(const bitmap_allocator<_Tp1>&,
const bitmap_allocator<_Tp2>&) throw()
{ return true; }
template<typename _Tp1, typename _Tp2>
bool
operator!=(const bitmap_allocator<_Tp1>&,
const bitmap_allocator<_Tp2>&) throw()
{ return false; }
template<typename _Tp>
typename bitmap_allocator<_Tp>::_BPVector
bitmap_allocator<_Tp>::_S_mem_blocks;
template<typename _Tp>
size_t bitmap_allocator<_Tp>::_S_block_size =
2 * balloc::bits_per_block;
template<typename _Tp>
typename __gnu_cxx::bitmap_allocator<_Tp>::_BPVector::size_type
bitmap_allocator<_Tp>::_S_last_dealloc_index = 0;
template<typename _Tp>
__gnu_cxx::balloc::_Bitmap_counter
<typename bitmap_allocator<_Tp>::_Alloc_block*>
bitmap_allocator<_Tp>::_S_last_request(_S_mem_blocks);
#if defined __GTHREADS
template<typename _Tp>
__gnu_cxx::_Mutex
bitmap_allocator<_Tp>::_S_mut;
#endif
}
#endif