aGrUM  0.14.2
gum::FixedAllocator Class Reference

Allocates objects of one given size. More...

#include <agrum/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 56 of file fixedAllocator.h.

Member Typedef Documentation

◆ __Chunks

typedef std::vector< __Chunk > gum::FixedAllocator::__Chunks
private

Vector of __Chunk objects.

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

References __allocChunk, __blockSize, __chunks, __deallocChunk, and __numBlocks.

129  {
130  // GUM_CONSTRUCTOR(FixedAllocator)
131  __blockSize = blockSize;
132  __numBlocks = numBlocks;
133  __allocChunk = __chunks.begin();
134  __deallocChunk = __chunks.begin();
135  }
__Chunks::iterator __allocChunk
Last Chunk used for an allocation.
std::size_t __blockSize
Size of a memory block allocated.
unsigned char __numBlocks
The maximum number of blocks a chunk can allocate.
__Chunks::iterator __deallocChunk
Last Chunk used for a deallocation.

◆ ~FixedAllocator()

INLINE gum::FixedAllocator::~FixedAllocator ( )

Destructor.

Definition at line 140 of file fixedAllocator_inl.h.

References __chunks.

140  {
141  for (__Chunks::iterator chunkIter = __chunks.begin();
142  chunkIter != __chunks.end();
143  ++chunkIter)
144  chunkIter->__release();
145  // GUM_DESTRUCTOR(FixedAllocator)
146  }

Member Function Documentation

◆ allocate()

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

Allocates a block.

Definition at line 155 of file fixedAllocator_inl.h.

References __allocChunk, __blockSize, __chunks, __deallocChunk, gum::FixedAllocator::__Chunk::__init(), and __numBlocks.

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

◆ deallocate()

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

Deallocates a block.

Definition at line 184 of file fixedAllocator_inl.h.

References __blockSize, __chunks, __deallocChunk, __numBlocks, and gum::FixedAllocator::__Chunk::__pData.

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

◆ objectSize()

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

Returns the size of block allocated by this FixedAllocator.

Definition at line 160 of file fixedAllocator.h.

References __blockSize.

160 { 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 182 of file fixedAllocator.h.

Referenced by allocate(), and FixedAllocator().

◆ __blockSize

std::size_t gum::FixedAllocator::__blockSize
private

Size of a memory block allocated.

Definition at line 166 of file fixedAllocator.h.

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

◆ __chunks

__Chunks gum::FixedAllocator::__chunks
private

Definition at line 177 of file fixedAllocator.h.

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

◆ __deallocChunk

__Chunks::iterator gum::FixedAllocator::__deallocChunk
private

Last Chunk used for a deallocation.

Definition at line 187 of file fixedAllocator.h.

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

◆ __numBlocks

unsigned char gum::FixedAllocator::__numBlocks
private

The maximum number of blocks a chunk can allocate.

Definition at line 171 of file fixedAllocator.h.

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


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