Helios++
Helios software for LiDAR simulations
BlockAllocator.h
1 #pragma once
2 
11 template <class Class>
13 private:
14  // *** SERIALIZATION *** //
15  // *********************** //
16  friend class boost::serialization::access;
23  template <class Archive>
24  void serialize(Archive &ar, const unsigned int version){
25  ar &blockSize;
26  ar &blocks;
28  ar &lastBlock;
29  ar &_nextBlockSize;
30  }
31 
32 protected:
33  // *** ATTRIBUTES *** //
34  // ******************** //
38  size_t blockSize;
44  vector<Class *> blocks;
56  Class *lastBlock;
61  std::function<size_t(void)> _nextBlockSize;
62 
63 public:
64  // *** CONSTRUCTION / DESTRUCTION *** //
65  // ************************************ //
70  BlockAllocator(size_t const blockSize=256) :
73  lastBlock(nullptr),
74  _nextBlockSize([&] () -> size_t {return blockSize;})
75  {}
79  virtual ~BlockAllocator() = default;
80 
81  // *** ALLOCATOR METHODS *** //
82  // *************************** //
93  virtual Class * nextNew(){
94  // Allocate first or next block
95  if(lastBlock==nullptr || lastBlockElements == blockSize){
96  blockSize = _nextBlockSize();
97  lastBlock = _new();
98  blocks.push_back(lastBlock);
99  lastBlockElements = 1;
100  return lastBlock;
101  }
102 
103  // Return next element from last allocated block
104  Class *next = lastBlock + lastBlockElements;
105  ++lastBlockElements;
106  return next;
107  }
108 
109 protected:
114  virtual inline Class * _new() {return new Class[blockSize];}
115 
116 public:
117  // *** GETTERs and SETTERs *** //
118  // ***************************** //
124  virtual inline void setNextBlockSize(std::function<size_t(void)> f)
125  {this->_nextBlockSize = f;}
130  virtual inline vector<Class *> getBlocks() const {return blocks;}
131 };
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