|
HAllocator
A Simple C++ Memory Allocator
|
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. | |
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.
| BlockSize | Size of each memory block in bytes |
| MaxNumBlocks | Maximum number of blocks allowed |
| hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks >::BlocksContainer | ( | ) |
Default constructor - initializes container with no blocks.
Constructor - creates container with one initial block.
Initializes the container by creating the first block of size BlockSize. Additional blocks are created on-demand during allocation.
| BlockSize | Size of each memory block |
| MaxNumBlocks | Maximum number of blocks |
| void * hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks >::allocate | ( | std::size_t | bytes | ) |
Allocates memory from the container.
Algorithm:
| bytes | Number of bytes to allocate |
Algorithm:
| BlockSize | Size of each memory block |
| MaxNumBlocks | Maximum number of blocks |
| bytes | Number of bytes to allocate |
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.
| 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.
| ptr | Pointer previously returned by allocate() |
| bytes | Size of the allocation (used to determine owning block) |
Searches through blocks to find which one owns the given pointer, then delegates deallocation to that block.
Algorithm:
| BlockSize | Size of each memory block |
| MaxNumBlocks | Maximum number of blocks |
| ptr | Pointer to deallocate |
| bytes | Size of allocation (for block compatibility) |
updated deallocation logic here: we try to munmap the memory directly if the address of the node needed for deallocation
| void hh::halloc::BlocksContainer< BlockSize, MaxNumBlocks >::log_container_state | ( | std::ofstream & | logfile | ) | const |
Logs the current state of the container to a file.
| logfile_dir | Directory to write the log file |