aGrUM  0.20.3
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 * _pData_
Pointer to the managed memory itself.
unsigned char _blocksAvailable_
Number of blocks available in this chunck.
+ 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
107  _firstAvailableBlock_ = static_cast< unsigned char >((toRelease - _pData_) / blockSize);
108 
109  // Truncation check
110  GUM_ASSERT(_firstAvailableBlock_ == (toRelease - _pData_) / blockSize);
111 
112  // We gain one block, yeah
114  }
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.
+ 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 * _pData_
Pointer to the managed memory itself.
unsigned char _blocksAvailable_
Number of blocks available in this chunck.
+ Here is the call graph for this function:

◆ _release_()

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

Releases the allocated memory.

Definition at line 119 of file fixedAllocator_inl.h.

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

119 { 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: