aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gum::FixedAllocator::Chunk__ Struct Reference

Allocates objects of one given size. More...

Public Attributes

unsigned char * pData__
 Pointer to the managed memory itself. More...
 
unsigned char firstAvailableBlock__
 Holds the index of the first block available in this chunck. More...
 
unsigned char blocksAvailable__
 Number of blocks available in this chunck. More...
 

Public Member Functions

void init__ (const std::size_t &blockSize, const unsigned char &numBlocks)
 Initializes a Chunk object. More...
 
void * allocate__ (const std::size_t &blockSize)
 Allocates a block of memory. More...
 
void deallocat__ (void *p, const std::size_t &blockSize)
 Deallocates a block of memory. More...
 
void release__ ()
 Releases the allocated memory. More...
 

Detailed Description

Allocates objects of one given size.

Has a fixed limit of allocation

Each object of type Chunk contains and manages a chunk of memory containing a amount of blocks. At construction time, you configure the block size and the number of blocks. A Chunk contains logic that allows you to allocate and deallocate memory blocks from that chunk of memory. When there are no more blocks available in the chunk, the allocation function returns zero.

Definition at line 80 of file fixedAllocator.h.

Member Function Documentation

◆ allocate__()

INLINE void * gum::FixedAllocator::Chunk__::allocate__ ( const std::size_t &  blockSize)

Allocates a block of memory.

Definition at line 65 of file fixedAllocator_inl.h.

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

65  {
66  if (!blocksAvailable__)
67  // If no block is available return nullptr
68  return NULL;
69 
70  // pData__ points to the beginning of allocated space.
71  // firstAvailableBlock__ gives us how many block to pass before getting
72  // the good one. We have to multiply by blockSize to get the good memory
73  // emplacement
74  unsigned char* pResult = pData__ + (firstAvailableBlock__ * blockSize);
75 
76  // Remember that the first byte of each block gives us the index of next
77  // available slot.
78  // The new first availble block will be at the index indicating in this
79  // block.
80  firstAvailableBlock__ = *pResult;
81 
82  // We lose one block
84 
85  return pResult;
86  }
unsigned char firstAvailableBlock__
Holds the index of the first block available in this chunck.
unsigned char blocksAvailable__
Number of blocks available in this chunck.
unsigned char * pData__
Pointer to the managed memory itself.
+ Here is the call graph for this function:

◆ deallocat__()

INLINE void gum::FixedAllocator::Chunk__::deallocat__ ( void *  p,
const std::size_t &  blockSize 
)

Deallocates a block of memory.

Definition at line 91 of file fixedAllocator_inl.h.

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

92  {
93  // first, ensure that deallocated is in this chunk
94  GUM_ASSERT(pDeallocatedBlock >= pData__);
95 
96  // Conversion pf pointer for handling
97  unsigned char* toRelease = static_cast< unsigned char* >(pDeallocatedBlock);
98 
99  // Alignement check
100  GUM_ASSERT((toRelease - pData__) % blockSize == 0);
101 
102  // First byte of toRelease has now to give the index of current first
103  // available block
104  *toRelease = firstAvailableBlock__;
105 
106  // So that first available block points to it
108  = static_cast< unsigned char >((toRelease - pData__) / blockSize);
109 
110  // Truncation check
111  GUM_ASSERT(firstAvailableBlock__ == (toRelease - pData__) / blockSize);
112 
113  // We gain one block, yeah
115  }
unsigned char firstAvailableBlock__
Holds the index of the first block available in this chunck.
unsigned char blocksAvailable__
Number of blocks available in this chunck.
unsigned char * pData__
Pointer to the managed memory itself.
+ Here is the call graph for this function:

◆ init__()

INLINE void gum::FixedAllocator::Chunk__::init__ ( const std::size_t &  blockSize,
const unsigned char &  numBlocks 
)

Initializes a Chunk object.

Definition at line 39 of file fixedAllocator_inl.h.

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

40  {
41  // Chunk memory space allocation. A chunk allocates a memory of blockSize *
42  // numBlocks size.
43  // The chunk will then give us numBlocks distinct blocks of blockSize from
44  // that space.
45  pData__ = new unsigned char[blockSize * numBlocks];
46 
47  // The first available block of memory is logically at the beginning.
49 
50  // The number of block still available is all the blocks at the beginning.
51  blocksAvailable__ = numBlocks;
52 
53  // For each unallocated block, the first byte contains a number.
54  // That number is the index of the next available block
55  // Since we're at the beginning, next free block is the next one simply.
56  // Following code initiate those number for each block
57  unsigned char* p = pData__;
58  for (unsigned char indexBlock = 0; indexBlock != numBlocks; p += blockSize)
59  *p = ++indexBlock;
60  }
unsigned char firstAvailableBlock__
Holds the index of the first block available in this chunck.
unsigned char blocksAvailable__
Number of blocks available in this chunck.
unsigned char * pData__
Pointer to the managed memory itself.
+ Here is the call graph for this function:

◆ release__()

INLINE void gum::FixedAllocator::Chunk__::release__ ( )

Releases the allocated memory.

Definition at line 120 of file fixedAllocator_inl.h.

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

120 { delete[] pData__; }
unsigned char * pData__
Pointer to the managed memory itself.
+ Here is the call graph for this function:

Member Data Documentation

◆ blocksAvailable__

unsigned char gum::FixedAllocator::Chunk__::blocksAvailable__

Number of blocks available in this chunck.

Definition at line 114 of file fixedAllocator.h.

◆ firstAvailableBlock__

unsigned char gum::FixedAllocator::Chunk__::firstAvailableBlock__

Holds the index of the first block available in this chunck.

Definition at line 109 of file fixedAllocator.h.

◆ pData__

unsigned char* gum::FixedAllocator::Chunk__::pData__

Pointer to the managed memory itself.

Definition at line 104 of file fixedAllocator.h.


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