Helios++
Helios software for LiDAR simulations
BlockAllocator< Class > Class Template Reference

Class to handle allocation of multiple instances of the same class by blocks. It is useful to reduce the number of new calls that, when too high, might cause a performance bottleneck. More...

#include <BlockAllocator.h>

Collaboration diagram for BlockAllocator< Class >:

Public Member Functions

 BlockAllocator (size_t const blockSize=256)
 Default constructor for BlockAllocator. More...
 
virtual ~BlockAllocator ()=default
 Destructor for BlockAllocator.
 
virtual Class * nextNew ()
 Obtain the next new allocated object. More...
 
virtual void setNextBlockSize (std::function< size_t(void)> f)
 Update next block size function. More...
 
virtual vector< Class * > getBlocks () const
 Obtain a vector with pointers to start of allocated blocks. More...
 

Protected Member Functions

virtual Class * _new ()
 The allocation itself. More...
 

Protected Attributes

size_t blockSize
 How many elements per allocated block.
 
vector< Class * > blocks
 Vector of allocated blocks. Each pointer in this vectors points to the starting element of a block of blockSize elements. More...
 
size_t lastBlockElements
 The number of already used elements in last block. More...
 
Class * lastBlock
 Pointer to last block in blocks. It is useful to prevent multiple queries for the same block at blocks vector. More...
 
std::function< size_t(void)> _nextBlockSize
 Compute the next block size. By default it preserves blockSize but it can be overridden to provide a dynamic behavior for block size.
 

Private Member Functions

template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 Serialize a BlockAllocator to a stream of byes. More...
 

Friends

class boost::serialization::access
 

Detailed Description

template<class Class>
class BlockAllocator< Class >

Class to handle allocation of multiple instances of the same class by blocks. It is useful to reduce the number of new calls that, when too high, might cause a performance bottleneck.

Author
Alberto M. Esmoris Pena
Version
1.0
Template Parameters
ClassType of class to be allocated

Constructor & Destructor Documentation

◆ BlockAllocator()

template<class Class >
BlockAllocator< Class >::BlockAllocator ( size_t const  blockSize = 256)
inline

Default constructor for BlockAllocator.

Parameters
blockSizeThe block size to be used for allocations

Member Function Documentation

◆ _new()

template<class Class >
virtual Class* BlockAllocator< Class >::_new ( )
inlineprotectedvirtual

The allocation itself.

Returns
Allocated block

◆ getBlocks()

template<class Class >
virtual vector<Class *> BlockAllocator< Class >::getBlocks ( ) const
inlinevirtual

Obtain a vector with pointers to start of allocated blocks.

Returns
Vector with pointers to start of allocated blocks

◆ nextNew()

template<class Class >
virtual Class* BlockAllocator< Class >::nextNew ( )
inlinevirtual

Obtain the next new allocated object.

If no block has been allocated, it handles the allocation of the first block. If last allocated block is not full, it return the next not yet used element in the block. If last allocated block is full, it allocates a new block

Returns
Next new allocated object

◆ serialize()

template<class Class >
template<class Archive >
void BlockAllocator< Class >::serialize ( Archive &  ar,
const unsigned int  version 
)
inlineprivate

Serialize a BlockAllocator to a stream of byes.

Template Parameters
ArchiveType of rendering
Parameters
arSpecific rendering for the stream of bytes
versionVersion number for the BlockAllocator

◆ setNextBlockSize()

template<class Class >
virtual void BlockAllocator< Class >::setNextBlockSize ( std::function< size_t(void)>  f)
inlinevirtual

Update next block size function.

Parameters
fNew next block size function
See also
BlockAllocator::_nextBlockSize

Member Data Documentation

◆ blocks

template<class Class >
vector<Class *> BlockAllocator< Class >::blocks
protected

Vector of allocated blocks. Each pointer in this vectors points to the starting element of a block of blockSize elements.

See also
BlockAllocator::lastBlock

◆ lastBlock

template<class Class >
Class* BlockAllocator< Class >::lastBlock
protected

Pointer to last block in blocks. It is useful to prevent multiple queries for the same block at blocks vector.

See also
BlockAllocator::blocks
BlockAllocator::lastBlockElements

◆ lastBlockElements

template<class Class >
size_t BlockAllocator< Class >::lastBlockElements
protected

The number of already used elements in last block.

See also
BlockAllocator::lastBlock

The documentation for this class was generated from the following file: