28 #ifndef util_FixedPoolAlloc_hpp
29 #define util_FixedPoolAlloc_hpp
35 void ** fixed_pool_buffer_init(
const std::size_t nbyte_total ,
36 const std::size_t nbyte ,
39 void throw_fixed_pool_buffer_bad_size(
const std::size_t nbyte_total ,
40 const std::size_t nbyte_first ,
41 const std::size_t nbyte );
43 void throw_fixed_pool_buffer_exhausted(
const std::size_t nbyte_total ,
44 const std::size_t nbyte );
46 void throw_fixed_pool_buffer_bad_deallocate(
const std::size_t ,
49 template<
unsigned NBYTE>
50 class FixedPoolBuffer {
53 enum { NWORD = NBYTE /
sizeof(
void*) };
59 FixedPoolBuffer(
const FixedPoolBuffer<NBYTE> & );
60 FixedPoolBuffer<NBYTE> & operator = (
const FixedPoolBuffer<NBYTE> & );
66 FixedPoolBuffer() : size(0) { link = mem ; }
68 bool full()
const {
return ! link ; }
70 void * allocate( std::size_t nbyte )
76 fixed_pool_buffer_init(NBYTE,size,link);
78 else if ( nbyte != size ) {
79 throw_fixed_pool_buffer_bad_size(NBYTE,size,nbyte);
82 throw_fixed_pool_buffer_exhausted(NBYTE,nbyte);
85 link =
reinterpret_cast<void**
>( *link );
90 void deallocate(
void * p , std::size_t nbyte )
92 if ( nbyte != size ) {
93 throw_fixed_pool_buffer_bad_size(NBYTE,size,nbyte);
95 void **
const vp =
reinterpret_cast<void**
>(p);
96 if ( vp < mem || ( mem + NWORD - size ) < vp ) {
97 throw_fixed_pool_buffer_bad_deallocate(NBYTE,p);
100 link =
reinterpret_cast<void**
>(vp);
106 template<
unsigned NBYTE,
typename T=
void*>
107 class FixedPoolAllocator {
110 FixedPoolBuffer<NBYTE> * buffer ;
112 FixedPoolAllocator<NBYTE,T> &
113 operator = (
const FixedPoolAllocator<NBYTE,T> & );
115 template<
unsigned M,
typename U>
friend class FixedPoolAllocator ;
119 typedef FixedPoolBuffer<NBYTE> buffer_type ;
121 typedef T value_type;
122 typedef std::size_t size_type;
124 typedef const T* const_pointer;
125 typedef T& reference;
126 typedef const T& const_reference;
127 typedef std::ptrdiff_t difference_type;
129 pointer address( reference value )
const {
return & value ; }
130 const_pointer address( const_reference value )
const {
return & value ; }
132 ~FixedPoolAllocator() {}
134 FixedPoolAllocator() : buffer(NULL) {}
136 FixedPoolAllocator(
const FixedPoolAllocator<NBYTE,T> & a )
137 : buffer(a.buffer) {}
139 explicit FixedPoolAllocator( buffer_type & b ) : buffer( & b ) {}
142 struct rebind {
typedef FixedPoolAllocator<NBYTE,U> other ; };
145 FixedPoolAllocator(
const FixedPoolAllocator<NBYTE,U> & a )
146 : buffer(a.buffer) {}
148 pointer allocate( size_type n ,
const void * = NULL )
149 {
return reinterpret_cast<pointer
>( buffer->allocate( n *
sizeof(T) ) ); }
151 void deallocate( pointer p , size_type n )
152 { buffer->deallocate(p, n *
sizeof(T) ); }
154 void construct( pointer p ,
const T & val ) {
new(p) T(val); }
155 void destroy( pointer p ) { p->~T(); }
158 void construct( U * p ,
const U & val ) {
new(p) U(val); }
161 void destroy( U * p ) { p->~U(); }
163 size_type max_size()
const {
return NBYTE /
sizeof(T) ; }
166 template<
unsigned NBYTE,
typename T>
167 bool operator == (
const FixedPoolAllocator<NBYTE,T> & lhs ,
168 const FixedPoolAllocator<NBYTE,T> & rhs )
169 {
return lhs.buffer == rhs.buffer ; }
171 template<
unsigned NBYTE,
typename T>
172 bool operator != (
const FixedPoolAllocator<NBYTE,T> & lhs ,
173 const FixedPoolAllocator<NBYTE,T> & rhs )
174 {
return lhs.buffer != rhs.buffer ; }