11 template <
class Class>
16 friend class boost::serialization::access;
23 template <
class Archive>
24 void serialize(Archive &ar,
const unsigned int version){
95 if(lastBlock==
nullptr || lastBlockElements == blockSize){
96 blockSize = _nextBlockSize();
98 blocks.push_back(lastBlock);
99 lastBlockElements = 1;
104 Class *next = lastBlock + lastBlockElements;
114 virtual inline Class *
_new() {
return new Class[blockSize];}
125 {this->_nextBlockSize = f;}
130 virtual inline vector<Class *>
getBlocks()
const {
return blocks;}
Class to handle allocation of multiple instances of the same class by blocks. It is useful to reduce ...
Definition: BlockAllocator.h:12
virtual ~BlockAllocator()=default
Destructor for BlockAllocator.
vector< Class * > blocks
Vector of allocated blocks. Each pointer in this vectors points to the starting element of a block of...
Definition: BlockAllocator.h:44
Class * lastBlock
Pointer to last block in blocks. It is useful to prevent multiple queries for the same block at block...
Definition: BlockAllocator.h:56
virtual vector< Class * > getBlocks() const
Obtain a vector with pointers to start of allocated blocks.
Definition: BlockAllocator.h:130
size_t blockSize
How many elements per allocated block.
Definition: BlockAllocator.h:38
virtual void setNextBlockSize(std::function< size_t(void)> f)
Update next block size function.
Definition: BlockAllocator.h:124
void serialize(Archive &ar, const unsigned int version)
Serialize a BlockAllocator to a stream of byes.
Definition: BlockAllocator.h:24
size_t lastBlockElements
The number of already used elements in last block.
Definition: BlockAllocator.h:49
virtual Class * _new()
The allocation itself.
Definition: BlockAllocator.h:114
BlockAllocator(size_t const blockSize=256)
Default constructor for BlockAllocator.
Definition: BlockAllocator.h:70
virtual Class * nextNew()
Obtain the next new allocated object.
Definition: BlockAllocator.h:93
std::function< size_t(void)> _nextBlockSize
Compute the next block size. By default it preserves blockSize but it can be overridden to provide a ...
Definition: BlockAllocator.h:61