HAllocator
A Simple C++ Memory Allocator
Loading...
Searching...
No Matches
Public Member Functions | List of all members
hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks > Class Template Reference

Manages multiple memory blocks for scalable allocation. More...

#include <BlocksContainer.hpp>

Public Member Functions

 BlocksContainer ()
 Default constructor - initializes container with no blocks.
 
void * allocate (std::size_t bytes)
 Allocates memory from the container.
 
void deallocate (void *ptr, std::size_t bytes)
 Deallocates previously allocated memory.
 
void log_container_state (std::ofstream &logfile) const
 Logs the current state of the container to a file.
 

Detailed Description

template<std::size_t BlockSize, int MaxNumBlocks>
class hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks >

Manages multiple memory blocks for scalable allocation.

This template class provides a higher-level allocator that can create and manage multiple Block instances. When an allocation cannot be satisfied by existing blocks, a new block is created automatically (up to MaxNumBlocks limit).

The container performs best-fit search across ALL blocks to minimize fragmentation.

Template Parameters
BlockSizeSize of each memory block in bytes
MaxNumBlocksMaximum number of blocks allowed
Note
Thread-safety: This class is NOT thread-safe
Memory overhead: Maintains an array of Block objects

Constructor & Destructor Documentation

◆ BlocksContainer()

template<std::size_t BlockSize, int MaxNumBlocks>
hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks >::BlocksContainer ( )

Default constructor - initializes container with no blocks.

Constructor - creates container with one initial block.

Postcondition
current_block_index == -1 (no blocks created yet)

Initializes the container by creating the first block of size BlockSize. Additional blocks are created on-demand during allocation.

Template Parameters
BlockSizeSize of each memory block
MaxNumBlocksMaximum number of blocks
Postcondition
current_block_index == 0
blocks[0] is initialized with BlockSize bytes

Member Function Documentation

◆ allocate()

template<std::size_t BlockSize, int MaxNumBlocks>
void * hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks >::allocate ( std::size_t  bytes)

Allocates memory from the container.

Algorithm:

  1. Search all existing blocks for best-fit node
  2. If found, allocate from that block
  3. If not found and space available, create new block and allocate
  4. If no space for new block, return nullptr (allocation failure)
Parameters
bytesNumber of bytes to allocate
Returns
Pointer to allocated memory, or nullptr if allocation fails
Precondition
bytes > 0
Postcondition
If successful, returned pointer is valid and points to usable memory
If unsuccessful (nullptr), no blocks were modified
Note
Fails if bytes > BlockSize or current_block_index >= MaxNumBlocks

Algorithm:

  1. Search all blocks for best-fit node
  2. If found: a. Allocate from that block
  3. If not found: a. Check if we can create a new block (current_block_index + 1 < MaxNumBlocks) b. If yes: create new block, find best-fit in new block, allocate c. If no: We allocate via mmap, and return the pointer to the allocated memory
Template Parameters
BlockSizeSize of each memory block
MaxNumBlocksMaximum number of blocks
Parameters
bytesNumber of bytes to allocate
Returns
Pointer to allocated memory, or nullptr if allocation fails
Note
Allocation fails if:
  • bytes > BlockSize (too large for any block)
  • All blocks full and current_block_index + 1 >= MaxNumBlocks

The updated allocation logic here: use mmap to allocate memory directly if no suitable block is found then when deallocate, if the address of the node needed for allocation not included in the block, we try to munmap it.

◆ deallocate()

template<std::size_t BlockSize, int MaxNumBlocks>
void hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks >::deallocate ( void *  ptr,
std::size_t  bytes 
)

Deallocates previously allocated memory.

Deallocates memory by finding the owning block.

Determines which block owns the memory pointer and delegates deallocation to that block. The block will merge adjacent free nodes automatically.

Parameters
ptrPointer previously returned by allocate()
bytesSize of the allocation (used to determine owning block)
Precondition
ptr != nullptr
ptr was returned by this container's allocate()
bytes matches the size passed to allocate()
Postcondition
Memory is marked as free and merged with adjacent free blocks
Warning
Undefined behavior if ptr was not allocated by this container

Searches through blocks to find which one owns the given pointer, then delegates deallocation to that block.

Algorithm:

  1. For each block i in [0, current_block_index]: a. Check if blocks[i].head <= ptr < blocks[i+1].head b. If yes, call blocks[i].deallocate()
Template Parameters
BlockSizeSize of each memory block
MaxNumBlocksMaximum number of blocks
Parameters
ptrPointer to deallocate
bytesSize of allocation (for block compatibility)
Precondition
ptr was allocated by this container
Postcondition
Memory is freed and merged with adjacent free blocks
Warning
Undefined behavior if ptr was not allocated by this container MAY CAUSE SEGFAULT

updated deallocation logic here: we try to munmap the memory directly if the address of the node needed for deallocation

◆ log_container_state()

template<std::size_t BlockSize, int MaxNumBlocks>
void hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks >::log_container_state ( std::ofstream &  logfile) const

Logs the current state of the container to a file.

Parameters
logfile_dirDirectory to write the log file

The documentation for this class was generated from the following file: