aGrUM  0.16.0
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 __blocksAvailable, __firstAvailableBlock, and __pData.

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 __blocksAvailable
Number of blocks available in this chunck.
unsigned char * __pData
Pointer to the managed memory itself.
unsigned char __firstAvailableBlock
Holds the index of the first block available in this chunck.

◆ __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 __blocksAvailable, __firstAvailableBlock, and __pData.

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 __blocksAvailable
Number of blocks available in this chunck.
unsigned char * __pData
Pointer to the managed memory itself.
unsigned char __firstAvailableBlock
Holds the index of the first block available in this chunck.

◆ __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 __blocksAvailable, __firstAvailableBlock, and __pData.

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

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 __blocksAvailable
Number of blocks available in this chunck.
unsigned char * __pData
Pointer to the managed memory itself.
unsigned char __firstAvailableBlock
Holds the index of the first block available in this chunck.
+ Here is the caller 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 __pData.

120 { delete[] __pData; }
unsigned char * __pData
Pointer to the managed memory itself.

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.

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 109 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 104 of file fixedAllocator.h.

Referenced by __allocate(), __deallocat(), __init(), __release(), and gum::FixedAllocator::deallocate().


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