aGrUM 3.1.1
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 98 of file fixedAllocator.h.

Member Function Documentation

◆ _allocate_()

void * gum::FixedAllocator::_Chunk_::_allocate_ ( const std::size_t & blockSize)

Allocates a block of memory.

Definition at line 83 of file fixedAllocator.cpp.

83 {
84 if (!_blocksAvailable_) {
85 // If no block is available return nullptr
86 return nullptr;
87 }
88
89 // _pData_ points to the beginning of allocated space.
90 // _firstAvailableBlock_ gives us how many block to pass before getting
91 // the good one. We have to multiply by blockSize to get the good memory
92 // emplacement
93 unsigned char* pResult = _pData_ + (_firstAvailableBlock_ * blockSize);
94
95 // Remember that the first byte of each block gives us the index of next
96 // available slot.
97 // The new first available block will be at the index indicating in this
98 // block.
99 _firstAvailableBlock_ = *pResult;
100
101 // We lose one block
103
104 return pResult;
105 }
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_()

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

Deallocates a block of memory.

Definition at line 107 of file fixedAllocator.cpp.

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

References _blocksAvailable_, _firstAvailableBlock_, and _pData_.

◆ _init_()

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

Initializes a Chunk object.

Definition at line 59 of file fixedAllocator.cpp.

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

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

74{ delete[] _pData_; }

References _pData_.

Member Data Documentation

◆ _blocksAvailable_

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

Number of blocks available in this chunck.

Definition at line 132 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 127 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 122 of file fixedAllocator.h.

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


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