heap_object.h
Go to the documentation of this file.
#ifndef _RHEOLEF_HEAP_OBJECT_H
#define _RHEOLEF_HEAP_OBJECT_H
#include "rheolef/compiler.h"
namespace rheolef {
template<class T>
class heap_object {
public:
typedef size_t size_type;
heap_object(size_type sizeof_bucket = sizeof(T));
~heap_object();
T* new_bucket() const;
#ifdef TO_CLEAN
T* malloc() const { return new_bucket(); } // for boost::object_pool compatibility
#endif // TO_CLEAN
size_type size() const { return _counter; }
void reinitialize (size_type sizeof_bucket = sizeof(T));
void clear ();
protected:
mutable std::list<std::vector<char> > _heap;
mutable size_type _heap_block_size;
mutable size_type _heap_block_last_free;
size_type _sizeof_bucket;
mutable size_type _counter;
static const size_type _heap_block_size_init = 10;
};
template<class T>
inline
heap_object<T>::heap_object(size_type sz)
: _heap(),
_heap_block_size(_heap_block_size_init),
_heap_block_last_free(0),
_sizeof_bucket(sz),
_counter(0)
{
_heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
}
template<class T>
inline
void
heap_object<T>::reinitialize (size_type sz)
{
clear();
_heap_block_size = heap_object<T>::_heap_block_size_init;
_heap_block_last_free = 0;
_sizeof_bucket = sz;
_counter = 0;
_heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
}
template<class T>
inline
T*
heap_object<T>::new_bucket() const
{
if (_heap_block_last_free == _heap_block_size) {
_heap_block_size *= 2;
_heap.push_front(std::vector<char>(_heap_block_size*_sizeof_bucket));
_heap_block_last_free = 0;
}
std::vector<char>& block = *(_heap.begin());
char* p = &block [_heap_block_last_free*_sizeof_bucket];
_heap_block_last_free++;
new ((void*) p) T(); // call default T constructor at p
_counter++;
return (T*)p;
}
template<class T>
inline
heap_object<T>::~heap_object()
{
clear();
}
template<class T>
void
heap_object<T>::clear()
{
size_type n = _heap_block_size_init;
for (std::list<std::vector<char> >::reverse_iterator i = _heap.rbegin();
_counter != 0 && i != _heap.rend(); i++, n *= 2) {
std::vector<char>& block = *i;
char* p = &(block[0]);
for (size_type c = 0; _counter != 0 && c < n; c++, p += _sizeof_bucket) {
((T*)p)->~T();
_counter--;
}
}
_heap.erase(_heap.begin(), _heap.end());
}
}// namespace rheolef
#endif // _RHEOLEF_HEAP_OBJECT_H
size_type
field::size_type size_type
Definition: branch.cc:425
rheolef::heap_object
Definition: heap_object.h:28
rheolef::heap_object::_heap
std::list< std::vector< char > > _heap
Definition: heap_object.h:41
rheolef::heap_object::_counter
size_type _counter
Definition: heap_object.h:45
rheolef::heap_object::heap_object
heap_object(size_type sizeof_bucket=sizeof(T))
Definition: heap_object.h:50
rheolef::heap_object::size_type
size_t size_type
Definition: heap_object.h:30
rheolef::heap_object::size
size_type size() const
Definition: heap_object.h:37
rheolef::heap_object::_heap_block_size_init
static const size_type _heap_block_size_init
Definition: heap_object.h:46
rheolef::heap_object::reinitialize
void reinitialize(size_type sizeof_bucket=sizeof(T))
Definition: heap_object.h:62
rheolef::heap_object::_sizeof_bucket
size_type _sizeof_bucket
Definition: heap_object.h:44
rheolef::heap_object::~heap_object
~heap_object()
Definition: heap_object.h:90
rheolef::heap_object::new_bucket
T * new_bucket() const
Definition: heap_object.h:74
rheolef::heap_object::_heap_block_last_free
size_type _heap_block_last_free
Definition: heap_object.h:43
rheolef::heap_object::clear
void clear()
Definition: heap_object.h:96
rheolef::heap_object::_heap_block_size
size_type _heap_block_size
Definition: heap_object.h:42
T
Expr1::float_type T
Definition: field_expr.h:261
mkgeo_ball.c
int c
Definition: mkgeo_ball.sh:153
mkgeo_ball.n
int n
Definition: mkgeo_ball.sh:150
rheolef
This file is part of Rheolef.
Definition: compiler_eigen.h:37
p
Definition: sphere.icc:25