aGrUM  0.16.0
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 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 __allocChunk, __blockSize, __chunks, __deallocChunk, and __numBlocks.

131  {
132  // GUM_CONSTRUCTOR(FixedAllocator)
133  __blockSize = blockSize;
134  __numBlocks = numBlocks;
135  __allocChunk = __chunks.begin();
136  __deallocChunk = __chunks.begin();
137  }
__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 142 of file fixedAllocator_inl.h.

References __chunks.

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

Member Function Documentation

◆ allocate()

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

Allocates a block.

Definition at line 157 of file fixedAllocator_inl.h.

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

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 __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 186 of file fixedAllocator_inl.h.

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

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  }
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 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.

Referenced by allocate(), and FixedAllocator().

◆ __blockSize

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

Size of a memory block allocated.

Definition at line 168 of file fixedAllocator.h.

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

◆ __chunks

__Chunks gum::FixedAllocator::__chunks
private

Definition at line 179 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 189 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 173 of file fixedAllocator.h.

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


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