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

Allocates objects of one given size. More...

Public Member Functions

void _init_ (const std::size_t &blockSize, const unsigned char &numBlocks)
 Initializes a Chunk object.
void * _allocate_ (const std::size_t &blockSize)
 Allocates a block of memory.
void _deallocat_ (void *p, const std::size_t &blockSize)
 Deallocates a block of memory.
void _release_ ()
 Releases the allocated memory.

Public Attributes

unsigned char * _pData_
 Pointer to the managed memory itself.
unsigned char _firstAvailableBlock_
 Holds the index of the first block available in this chunck.
unsigned char _blocksAvailable_
 Number of blocks available in this chunck.

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 102 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 87 of file fixedAllocator_inl.h.

87 {
89 // If no block is available return nullptr
90 return NULL;
91
92 // _pData_ points to the beginning of allocated space.
93 // _firstAvailableBlock_ gives us how many block to pass before getting
94 // the good one. We have to multiply by blockSize to get the good memory
95 // emplacement
96 unsigned char* pResult = _pData_ + (_firstAvailableBlock_ * blockSize);
97
98 // Remember that the first byte of each block gives us the index of next
99 // available slot.
100 // The new first availble block will be at the index indicating in this
101 // block.
102 _firstAvailableBlock_ = *pResult;
103
104 // We lose one block
106
107 return pResult;
108 }
unsigned char _firstAvailableBlock_
Holds the index of the first block available in this chunck.
unsigned char * _pData_
Pointer to the managed memory itself.
unsigned char _blocksAvailable_
Number of blocks available in this chunck.

References _blocksAvailable_, _firstAvailableBlock_, and _pData_.

◆ _deallocat_()

INLINE void gum::FixedAllocator::_Chunk_::_deallocat_ ( void * p,
const std::size_t & blockSize )

Deallocates a block of memory.

Definition at line 113 of file fixedAllocator_inl.h.

114 {
115 // first, ensure that deallocated is in this chunk
116 GUM_ASSERT(pDeallocatedBlock >= _pData_);
117
118 // Conversion pf pointer for handling
119 unsigned char* toRelease = static_cast< unsigned char* >(pDeallocatedBlock);
120
121 // Alignement check
122 GUM_ASSERT((toRelease - _pData_) % blockSize == 0);
123
124 // First byte of toRelease has now to give the index of current first
125 // available block
126 *toRelease = _firstAvailableBlock_;
127
128 // So that first available block points to it
129 _firstAvailableBlock_ = static_cast< unsigned char >((toRelease - _pData_) / blockSize);
130
131 // Truncation check
132 GUM_ASSERT(_firstAvailableBlock_ == (toRelease - _pData_) / blockSize);
133
134 // We gain one block, yeah
136 }

References _blocksAvailable_, _firstAvailableBlock_, and _pData_.

◆ _init_()

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

Initializes a Chunk object.

Definition at line 61 of file fixedAllocator_inl.h.

62 {
63 // Chunk memory space allocation. A chunk allocates a memory of blockSize *
64 // numBlocks size.
65 // The chunk will then give us numBlocks distinct blocks of blockSize from
66 // that space.
67 _pData_ = new unsigned char[blockSize * numBlocks];
68
69 // The first available block of memory is logically at the beginning.
71
72 // The number of block still available is all the blocks at the beginning.
73 _blocksAvailable_ = numBlocks;
74
75 // For each unallocated block, the first byte contains a number.
76 // That number is the index of the next available block
77 // Since we're at the beginning, next free block is the next one simply.
78 // Following code initiate those number for each block
79 unsigned char* p = _pData_;
80 for (unsigned char indexBlock = 0; indexBlock != numBlocks; p += blockSize)
81 *p = ++indexBlock;
82 }

References _blocksAvailable_, _firstAvailableBlock_, and _pData_.

Referenced by gum::FixedAllocator::allocate().

Here is the caller graph for this function:

◆ _release_()

INLINE void gum::FixedAllocator::_Chunk_::_release_ ( )

Releases the allocated memory.

Definition at line 141 of file fixedAllocator_inl.h.

141{ delete[] _pData_; }

References _pData_.

Member Data Documentation

◆ _blocksAvailable_

unsigned char gum::FixedAllocator::_Chunk_::_blocksAvailable_

Number of blocks available in this chunck.

Definition at line 136 of file fixedAllocator.h.

Referenced by _allocate_(), _deallocat_(), and _init_().

◆ _firstAvailableBlock_

unsigned char gum::FixedAllocator::_Chunk_::_firstAvailableBlock_

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

Definition at line 131 of file fixedAllocator.h.

Referenced by _allocate_(), _deallocat_(), and _init_().

◆ _pData_

unsigned char* gum::FixedAllocator::_Chunk_::_pData_

Pointer to the managed memory itself.

Definition at line 126 of file fixedAllocator.h.

Referenced by _allocate_(), _deallocat_(), _init_(), and _release_().


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