aGrUM 3.1.1
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 chunk 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 accommodate 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 195 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 83 of file fixedAllocator_inl.h.

84 {
85 // GUM_CONSTRUCTOR(FixedAllocator);
86 _blockSize_ = blockSize;
87 _numBlocks_ = numBlocks;
88 _allocChunk_ = _chunks_.begin();
89 _deallocChunk_ = _chunks_.begin();
90 }
_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 95 of file fixedAllocator_inl.h.

95 {
96 for (_Chunks_::iterator chunkIter = _chunks_.begin(); chunkIter != _chunks_.end();
97 ++chunkIter) {
98 chunkIter->_release_();
99 }
100 // GUM_DESTRUCTOR(FixedAllocator);
101 }

References _chunks_.

Member Function Documentation

◆ allocate()

void * gum::FixedAllocator::allocate ( )

Allocates a block.

Definition at line 131 of file fixedAllocator.cpp.

131 {
132 if (_chunks_.empty() || _allocChunk_->_blocksAvailable_ == 0) {
133 // no available memory in this chunk
134 // Try to find one with memory available
135 for (_Chunks_::iterator chunksIter = _chunks_.begin();; ++chunksIter) {
136 if (chunksIter == _chunks_.end()) {
137 // All chunks are filled up. Adding a new one
138 _chunks_.reserve(_chunks_.size() + 1);
139 _Chunk_ newChunk;
140 newChunk._init_(_blockSize_, _numBlocks_);
141 _chunks_.push_back(newChunk);
142 _allocChunk_ = _chunks_.end();
143 --_allocChunk_;
145 break;
146 }
147 if (chunksIter->_blocksAvailable_ > 0) {
148 // Found a chunk
149 _allocChunk_ = chunksIter;
150 break;
151 }
152 }
153 }
154 return _allocChunk_->_allocate_(_blockSize_);
155 }
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()

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

Deallocates a block.

Definition at line 157 of file fixedAllocator.cpp.

157 {
158 bool chunk_found = true;
159 if (_deallocChunk_->_pData_ > pDeallocatedBlock
160 || pDeallocatedBlock > (_deallocChunk_->_pData_ + (_numBlocks_ * _blockSize_))) {
161 // If not things get ugly
162 // We have to find where the Chunk containing this pointer is
163 std::ptrdiff_t offset = 0;
164
165 // We perform a bidirectional search from _deallocChunk_
166 bool iter_ok = true;
167 while (iter_ok) {
168 iter_ok = false;
169 ++offset;
170 // First we look for the one going to the end of the vector
171 if ((_deallocChunk_ + offset) < _chunks_.end()) {
172 iter_ok = true;
173 if ((_deallocChunk_ + offset)->_pData_ <= pDeallocatedBlock
174 && pDeallocatedBlock
175 < ((_deallocChunk_ + offset)->_pData_ + (_numBlocks_ * _blockSize_))) {
176 // If pointed chunk contains this pointer, deallocation find the
177 // place
178 _deallocChunk_ = (_deallocChunk_ + offset);
179 chunk_found = true;
180 break;
181 }
182 }
183
184 // Then we look for the one going to the beginning of the vector
185 if ((_deallocChunk_ - offset) >= _chunks_.begin()) {
186 iter_ok = true;
187 if ((_deallocChunk_ - offset)->_pData_ <= pDeallocatedBlock
188 && pDeallocatedBlock
189 < ((_deallocChunk_ - offset)->_pData_ + (_numBlocks_ * _blockSize_))) {
190 // If pointed chunk contains this pointer, deallocation find the
191 // place
192 _deallocChunk_ = (_deallocChunk_ - offset);
193 chunk_found = true;
194 break;
195 }
196 }
197 }
198 }
199
200 if (chunk_found) { _deallocChunk_->_deallocat_(pDeallocatedBlock, _blockSize_); }
201 }

References _blockSize_, _chunks_, _deallocChunk_, and _numBlocks_.

◆ objectSize()

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

Returns the size of block allocated by this FixedAllocator.

Definition at line 118 of file fixedAllocator_inl.h.

118{ return _blockSize_; }

References _blockSize_.

Member Data Documentation

◆ _allocChunk_

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

Last Chunk used for an allocation.

Definition at line 201 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 185 of file fixedAllocator.h.

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

◆ _chunks_

_Chunks_ gum::FixedAllocator::_chunks_
private

Definition at line 196 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 206 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 190 of file fixedAllocator.h.

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


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