aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::FixedAllocator Class Reference

Allocates objects of one given size. More...

#include <agrum/tools/core/fixedAllocator.h>

Public Member Functions

const size_t & objectSize ()
 Returns the size of block allocated by this FixedAllocator. More...
 
Constructors / Destructors
 FixedAllocator (const std::size_t &blockSize, const unsigned char &numBlocks=UCHAR_MAX)
 Constructor. More...
 
 ~FixedAllocator ()
 Destructor. More...
 
Allocator / Deallocator
void * allocate ()
 Allocates a block. More...
 
void deallocate (void *pDeallocatedBlock)
 Deallocates a block. More...
 

Classes

struct  _Chunk_
 Allocates objects of one given size. More...
 

Detailed Description

Allocates objects of one given size.

Fixed allocator knows how to allocate and deallocate blocks of fixed size but is not limited to a chunck size. Its capacity is limited only by the available memory. To achieve this, FixedAllocator aggregates a vector of Chunk objects. Whenever an allocation request occurs, FixedAllocators looks for a Chunk that can accomodate the request. If all Chunks are filled up, FixedAllocator appends a new Chunk.

Definition at line 58 of file fixedAllocator.h.

Member Typedef Documentation

◆ _Chunks_

typedef std::vector< _Chunk_ > gum::FixedAllocator::_Chunks_
private

Vector of Chunk objects.

Definition at line 177 of file fixedAllocator.h.

Constructor & Destructor Documentation

◆ FixedAllocator()

INLINE gum::FixedAllocator::FixedAllocator ( const std::size_t &  blockSize,
const unsigned char &  numBlocks = UCHAR_MAX 
)

Constructor.

Parameters
blockSizeis the size of an allocated block.
numBlocksis the number of block allocated per chunk numBlock * blockSize is the size that a chunk allocates directly when it is created

Definition at line 129 of file fixedAllocator_inl.h.

References gum::Set< Key, Alloc >::emplace().

130  {
131  // GUM_CONSTRUCTOR(FixedAllocator);
132  _blockSize_ = blockSize;
133  _numBlocks_ = numBlocks;
134  _allocChunk_ = _chunks_.begin();
135  _deallocChunk_ = _chunks_.begin();
136  }
_Chunks_::iterator _allocChunk_
Last Chunk used for an allocation.
unsigned char _numBlocks_
The maximum number of blocks a chunk can allocate.
_Chunks_::iterator _deallocChunk_
Last Chunk used for a deallocation.
std::size_t _blockSize_
Size of a memory block allocated.
+ Here is the call graph for this function:

◆ ~FixedAllocator()

INLINE gum::FixedAllocator::~FixedAllocator ( )

Destructor.

Definition at line 141 of file fixedAllocator_inl.h.

References gum::Set< Key, Alloc >::emplace().

141  {
142  for (_Chunks_::iterator chunkIter = _chunks_.begin(); chunkIter != _chunks_.end(); ++chunkIter)
143  chunkIter->_release_();
144  // GUM_DESTRUCTOR(FixedAllocator);
145  }
+ Here is the call graph for this function:

Member Function Documentation

◆ allocate()

INLINE void * gum::FixedAllocator::allocate ( )

Allocates a block.

Definition at line 154 of file fixedAllocator_inl.h.

References gum::Set< Key, Alloc >::emplace().

154  {
155  if (_chunks_.empty() || _allocChunk_->_blocksAvailable_ == 0) {
156  // no available memory in this chunk
157  // Try to find one with memory available
158  for (_Chunks_::iterator chunksIter = _chunks_.begin();; ++chunksIter) {
159  if (chunksIter == _chunks_.end()) {
160  // All chunks are filled up. Adding a new one
161  _chunks_.reserve(_chunks_.size() + 1);
162  _Chunk_ newChunk;
163  newChunk._init_(_blockSize_, _numBlocks_);
164  _chunks_.push_back(newChunk);
165  _allocChunk_ = _chunks_.end();
166  --_allocChunk_;
168  break;
169  }
170  if (chunksIter->_blocksAvailable_ > 0) {
171  // Found a chunk
172  _allocChunk_ = chunksIter;
173  break;
174  }
175  }
176  }
177  return _allocChunk_->_allocate_(_blockSize_);
178  }
_Chunks_::iterator _allocChunk_
Last Chunk used for an allocation.
unsigned char _numBlocks_
The maximum number of blocks a chunk can allocate.
_Chunks_::iterator _deallocChunk_
Last Chunk used for a deallocation.
std::size_t _blockSize_
Size of a memory block allocated.
+ Here is the call graph for this function:

◆ deallocate()

INLINE void gum::FixedAllocator::deallocate ( void *  pDeallocatedBlock)

Deallocates a block.

Definition at line 183 of file fixedAllocator_inl.h.

References gum::Set< Key, Alloc >::emplace().

183  {
184  if (_deallocChunk_->_pData_ > pDeallocatedBlock
185  || pDeallocatedBlock > (_deallocChunk_->_pData_ + (_numBlocks_ * _blockSize_))) {
186  // If not things get ugly
187  // We have to find where the Chunk containing this pointer is
188  std::ptrdiff_t offset = 0;
189 
190  // We perform a bidirectionnal search from _deallocChunk_
191  while (true) {
192  ++offset;
193  // First we look for the one going to the end of the vector
194  if ((_deallocChunk_ + offset) < _chunks_.end()) {
195  if ((_deallocChunk_ + offset)->_pData_ <= pDeallocatedBlock
196  && pDeallocatedBlock
197  < ((_deallocChunk_ + offset)->_pData_ + (_numBlocks_ * _blockSize_))) {
198  // If pointed chunk contains this pointer, deallocation find the
199  // place
200  _deallocChunk_ = (_deallocChunk_ + offset);
201  break;
202  }
203  }
204 
205  // Then we look for the one going to the beginning of the vector
206  if ((_deallocChunk_ - offset) >= _chunks_.begin()) {
207  if ((_deallocChunk_ - offset)->_pData_ <= pDeallocatedBlock
208  && pDeallocatedBlock
209  < ((_deallocChunk_ - offset)->_pData_ + (_numBlocks_ * _blockSize_))) {
210  // If pointed chunk contains this pointer, deallocation find the
211  // place
212  _deallocChunk_ = (_deallocChunk_ - offset);
213  break;
214  }
215  }
216  }
217  }
218  _deallocChunk_->_deallocat_(pDeallocatedBlock, _blockSize_);
219  }
unsigned char _numBlocks_
The maximum number of blocks a chunk can allocate.
_Chunks_::iterator _deallocChunk_
Last Chunk used for a deallocation.
std::size_t _blockSize_
Size of a memory block allocated.
+ Here is the call graph for this function:

◆ objectSize()

const size_t& gum::FixedAllocator::objectSize ( )
inline

Returns the size of block allocated by this FixedAllocator.

Definition at line 161 of file fixedAllocator.h.

References _blockSize_.

161 { return _blockSize_; }
std::size_t _blockSize_
Size of a memory block allocated.

Member Data Documentation

◆ _allocChunk_

_Chunks_::iterator gum::FixedAllocator::_allocChunk_
private

Last Chunk used for an allocation.

Definition at line 183 of file fixedAllocator.h.

◆ _blockSize_

std::size_t gum::FixedAllocator::_blockSize_
private

Size of a memory block allocated.

Definition at line 167 of file fixedAllocator.h.

Referenced by objectSize().

◆ _chunks_

_Chunks_ gum::FixedAllocator::_chunks_
private

Definition at line 178 of file fixedAllocator.h.

◆ _deallocChunk_

_Chunks_::iterator gum::FixedAllocator::_deallocChunk_
private

Last Chunk used for a deallocation.

Definition at line 188 of file fixedAllocator.h.

◆ _numBlocks_

unsigned char gum::FixedAllocator::_numBlocks_
private

The maximum number of blocks a chunk can allocate.

Definition at line 172 of file fixedAllocator.h.


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