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

Allocates objects of one given size. More...

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

Classes

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

Public Member Functions

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

Private Types

using _Chunks_ = std::vector< _Chunk_ >
 Vector of Chunk objects.

Private Attributes

std::size_t _blockSize_
 Size of a memory block allocated.
unsigned char _numBlocks_
 The maximum number of blocks a chunk can allocate.
_Chunks_ _chunks_
_Chunks_::iterator _allocChunk_
 Last Chunk used for an allocation.
_Chunks_::iterator _deallocChunk_
 Last Chunk used for a deallocation.

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 79 of file fixedAllocator.h.

Member Typedef Documentation

◆ _Chunks_

using gum::FixedAllocator::_Chunks_ = std::vector< _Chunk_ >
private

Vector of Chunk objects.

Definition at line 199 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 150 of file fixedAllocator_inl.h.

151 {
152 // GUM_CONSTRUCTOR(FixedAllocator);
153 _blockSize_ = blockSize;
154 _numBlocks_ = numBlocks;
155 _allocChunk_ = _chunks_.begin();
156 _deallocChunk_ = _chunks_.begin();
157 }
_Chunks_::iterator _deallocChunk_
Last Chunk used for a deallocation.
unsigned char _numBlocks_
The maximum number of blocks a chunk can allocate.
std::size_t _blockSize_
Size of a memory block allocated.
_Chunks_::iterator _allocChunk_
Last Chunk used for an allocation.

References _allocChunk_, _blockSize_, _chunks_, _deallocChunk_, and _numBlocks_.

◆ ~FixedAllocator()

INLINE gum::FixedAllocator::~FixedAllocator ( )

Destructor.

Definition at line 162 of file fixedAllocator_inl.h.

162 {
163 for (_Chunks_::iterator chunkIter = _chunks_.begin(); chunkIter != _chunks_.end(); ++chunkIter)
164 chunkIter->_release_();
165 // GUM_DESTRUCTOR(FixedAllocator);
166 }

References _chunks_.

Member Function Documentation

◆ allocate()

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

Allocates a block.

Definition at line 175 of file fixedAllocator_inl.h.

175 {
176 if (_chunks_.empty() || _allocChunk_->_blocksAvailable_ == 0) {
177 // no available memory in this chunk
178 // Try to find one with memory available
179 for (_Chunks_::iterator chunksIter = _chunks_.begin();; ++chunksIter) {
180 if (chunksIter == _chunks_.end()) {
181 // All chunks are filled up. Adding a new one
182 _chunks_.reserve(_chunks_.size() + 1);
183 _Chunk_ newChunk;
184 newChunk._init_(_blockSize_, _numBlocks_);
185 _chunks_.push_back(newChunk);
186 _allocChunk_ = _chunks_.end();
187 --_allocChunk_;
189 break;
190 }
191 if (chunksIter->_blocksAvailable_ > 0) {
192 // Found a chunk
193 _allocChunk_ = chunksIter;
194 break;
195 }
196 }
197 }
198 return _allocChunk_->_allocate_(_blockSize_);
199 }
Allocates objects of one given size.
void _init_(const std::size_t &blockSize, const unsigned char &numBlocks)
Initializes a Chunk object.

References _allocChunk_, _blockSize_, _chunks_, _deallocChunk_, gum::FixedAllocator::_Chunk_::_init_(), and _numBlocks_.

Here is the call graph for this function:

◆ deallocate()

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

Deallocates a block.

Definition at line 204 of file fixedAllocator_inl.h.

204 {
205 if (_deallocChunk_->_pData_ > pDeallocatedBlock
206 || pDeallocatedBlock > (_deallocChunk_->_pData_ + (_numBlocks_ * _blockSize_))) {
207 // If not things get ugly
208 // We have to find where the Chunk containing this pointer is
209 std::ptrdiff_t offset = 0;
210
211 // We perform a bidirectionnal search from _deallocChunk_
212 while (true) {
213 ++offset;
214 // First we look for the one going to the end of the vector
215 if ((_deallocChunk_ + offset) < _chunks_.end()) {
216 if ((_deallocChunk_ + offset)->_pData_ <= pDeallocatedBlock
217 && pDeallocatedBlock
218 < ((_deallocChunk_ + offset)->_pData_ + (_numBlocks_ * _blockSize_))) {
219 // If pointed chunk contains this pointer, deallocation find the
220 // place
221 _deallocChunk_ = (_deallocChunk_ + offset);
222 break;
223 }
224 }
225
226 // Then we look for the one going to the beginning of the vector
227 if ((_deallocChunk_ - offset) >= _chunks_.begin()) {
228 if ((_deallocChunk_ - offset)->_pData_ <= pDeallocatedBlock
229 && pDeallocatedBlock
230 < ((_deallocChunk_ - offset)->_pData_ + (_numBlocks_ * _blockSize_))) {
231 // If pointed chunk contains this pointer, deallocation find the
232 // place
233 _deallocChunk_ = (_deallocChunk_ - offset);
234 break;
235 }
236 }
237 }
238 }
239 _deallocChunk_->_deallocat_(pDeallocatedBlock, _blockSize_);
240 }

References _blockSize_, _chunks_, _deallocChunk_, and _numBlocks_.

◆ objectSize()

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

Returns the size of block allocated by this FixedAllocator.

Definition at line 183 of file fixedAllocator.h.

183{ return _blockSize_; }

References _blockSize_.

Member Data Documentation

◆ _allocChunk_

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

Last Chunk used for an allocation.

Definition at line 205 of file fixedAllocator.h.

Referenced by FixedAllocator(), and allocate().

◆ _blockSize_

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

Size of a memory block allocated.

Definition at line 189 of file fixedAllocator.h.

Referenced by FixedAllocator(), allocate(), deallocate(), and objectSize().

◆ _chunks_

_Chunks_ gum::FixedAllocator::_chunks_
private

Definition at line 200 of file fixedAllocator.h.

Referenced by FixedAllocator(), ~FixedAllocator(), allocate(), and deallocate().

◆ _deallocChunk_

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

Last Chunk used for a deallocation.

Definition at line 210 of file fixedAllocator.h.

Referenced by FixedAllocator(), allocate(), and deallocate().

◆ _numBlocks_

unsigned char gum::FixedAllocator::_numBlocks_
private

The maximum number of blocks a chunk can allocate.

Definition at line 194 of file fixedAllocator.h.

Referenced by FixedAllocator(), allocate(), and deallocate().


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