aGrUM  0.20.2
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 178 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 130 of file fixedAllocator_inl.h.

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

131  {
132  // GUM_CONSTRUCTOR(FixedAllocator)
133  blockSize__ = blockSize;
134  numBlocks__ = numBlocks;
135  allocChunk__ = chunks__.begin();
136  deallocChunk__ = chunks__.begin();
137  }
Chunks__::iterator deallocChunk__
Last Chunk used for a deallocation.
std::size_t blockSize__
Size of a memory block allocated.
Chunks__::iterator allocChunk__
Last Chunk used for an allocation.
unsigned char numBlocks__
The maximum number of blocks a chunk can allocate.
+ Here is the call graph for this function:

◆ ~FixedAllocator()

INLINE gum::FixedAllocator::~FixedAllocator ( )

Destructor.

Definition at line 142 of file fixedAllocator_inl.h.

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

142  {
143  for (Chunks__::iterator chunkIter = chunks__.begin();
144  chunkIter != chunks__.end();
145  ++chunkIter)
146  chunkIter->release__();
147  // GUM_DESTRUCTOR(FixedAllocator)
148  }
+ Here is the call graph for this function:

Member Function Documentation

◆ allocate()

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

Allocates a block.

Definition at line 157 of file fixedAllocator_inl.h.

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

157  {
158  if (chunks__.empty() || allocChunk__->blocksAvailable__ == 0) {
159  // no available memory in this chunk
160  // Try to find one with memory available
161  for (Chunks__::iterator chunksIter = chunks__.begin();; ++chunksIter) {
162  if (chunksIter == chunks__.end()) {
163  // All chunks are filled up. Adding a new one
164  chunks__.reserve(chunks__.size() + 1);
165  Chunk__ newChunk;
166  newChunk.init__(blockSize__, numBlocks__);
167  chunks__.push_back(newChunk);
168  allocChunk__ = chunks__.end();
169  --allocChunk__;
171  break;
172  }
173  if (chunksIter->blocksAvailable__ > 0) {
174  // Found a chunk
175  allocChunk__ = chunksIter;
176  break;
177  }
178  }
179  }
180  return allocChunk__->allocate__(blockSize__);
181  }
Chunks__::iterator deallocChunk__
Last Chunk used for a deallocation.
std::size_t blockSize__
Size of a memory block allocated.
Chunks__::iterator allocChunk__
Last Chunk used for an allocation.
unsigned char numBlocks__
The maximum number of blocks a chunk can allocate.
+ Here is the call graph for this function:

◆ deallocate()

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

Deallocates a block.

Definition at line 186 of file fixedAllocator_inl.h.

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

186  {
187  if (deallocChunk__->pData__ > pDeallocatedBlock
188  || pDeallocatedBlock
189  > (deallocChunk__->pData__ + (numBlocks__ * blockSize__))) {
190  // If not things get ugly
191  // We have to find where the Chunk containing this pointer is
192  std::ptrdiff_t offset = 0;
193 
194  // We perform a bidirectionnal search from deallocChunk__
195  while (true) {
196  ++offset;
197  // First we look for the one going to the end of the vector
198  if ((deallocChunk__ + offset) < chunks__.end()) {
199  if ((deallocChunk__ + offset)->pData__ <= pDeallocatedBlock
200  && pDeallocatedBlock < ((deallocChunk__ + offset)->pData__
201  + (numBlocks__ * blockSize__))) {
202  // If pointed chunk contains this pointer, deallocation find the
203  // place
204  deallocChunk__ = (deallocChunk__ + offset);
205  break;
206  }
207  }
208 
209  // Then we look for the one going to the beginning of the vector
210  if ((deallocChunk__ - offset) >= chunks__.begin()) {
211  if ((deallocChunk__ - offset)->pData__ <= pDeallocatedBlock
212  && pDeallocatedBlock < ((deallocChunk__ - offset)->pData__
213  + (numBlocks__ * blockSize__))) {
214  // If pointed chunk contains this pointer, deallocation find the
215  // place
216  deallocChunk__ = (deallocChunk__ - offset);
217  break;
218  }
219  }
220  }
221  }
222  deallocChunk__->deallocat__(pDeallocatedBlock, blockSize__);
223  }
Chunks__::iterator deallocChunk__
Last Chunk used for a deallocation.
std::size_t blockSize__
Size of a memory block allocated.
unsigned char numBlocks__
The maximum number of blocks a chunk can allocate.
+ 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 162 of file fixedAllocator.h.

References blockSize__.

162 { 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 184 of file fixedAllocator.h.

◆ blockSize__

std::size_t gum::FixedAllocator::blockSize__
private

Size of a memory block allocated.

Definition at line 168 of file fixedAllocator.h.

Referenced by objectSize().

◆ chunks__

Chunks__ gum::FixedAllocator::chunks__
private

Definition at line 179 of file fixedAllocator.h.

◆ deallocChunk__

Chunks__::iterator gum::FixedAllocator::deallocChunk__
private

Last Chunk used for a deallocation.

Definition at line 189 of file fixedAllocator.h.

◆ numBlocks__

unsigned char gum::FixedAllocator::numBlocks__
private

The maximum number of blocks a chunk can allocate.

Definition at line 173 of file fixedAllocator.h.


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