|
HAllocator
A Simple C++ Memory Allocator
|
Manages a contiguous memory block with RB-tree based allocation. More...
#include <Block.hpp>
Public Member Functions | |
| Block () | |
| Default constructor - creates invalid block. | |
| Block (std::size_t bytes) | |
| Constructs a memory block of specified size. | |
| Block (Block &&other) | |
| Move constructor. | |
| Block & | operator= (Block &&other) |
| Move assignment operator. | |
| MemoryNode * | best_fit (std::size_t bytes) |
| Finds best-fit free node for allocation. | |
| ~Block () | |
| Destructor - releases memory back to OS. | |
| std::size_t | get_size () const |
| Gets total block size. | |
| void * | get_head () const |
| Gets pointer to the head node. | |
| void * | allocate (std::size_t bytes, MemoryNode *node) |
| Allocates memory from a specific node. | |
| void | deallocate (void *ptr, std::size_t bytes) |
| Deallocates previously allocated memory. | |
| void | log_block_state (std::ofstream &logfile) const |
| Logs the current state of the block to a file. | |
Manages a contiguous memory block with RB-tree based allocation.
The Block class provides memory allocation and deallocation within a fixed memory region. It uses:
Memory is obtained from the OS via mmap and released via munmap. Internal fragmentation is minimized through block splitting and coalescing.
| hh::halloc::Block::Block | ( | ) |
Default constructor - creates invalid block.
|
explicit |
Constructs a memory block of specified size.
Allocates memory from OS via mmap and initializes the block with a single large free node.
| bytes | Total block size in bytes |
| std::bad_alloc | if mmap fails |
| hh::halloc::Block::Block | ( | Block && | other | ) |
Move constructor.
| other | Block to move from |
| hh::halloc::Block::~Block | ( | ) |
Destructor - releases memory back to OS.
Destructor - releases the entire memory block back to the OS.
Uses munmap to return the entire mmap'd region to the operating system. This deallocates all memory nodes at once, regardless of individual allocation status.
| void * hh::halloc::Block::allocate | ( | std::size_t | bytes, |
| MemoryNode * | node | ||
| ) |
Allocates memory from a specific node.
Allocates memory from a specific free node.
Removes node from RB-tree, potentially splits it if too large, and returns pointer to usable memory (after metadata).
| bytes | Size in bytes requested |
| node | The node to allocate from (typically from best_fit) |
This function performs the final allocation step after best_fit has found a suitable node:
| bytes | Number of bytes requested by user (excluding metadata) |
| node | Free node to allocate from (must be large enough) |
| MemoryNode * hh::halloc::Block::best_fit | ( | std::size_t | bytes | ) |
Finds best-fit free node for allocation.
Searches the RB-tree for the smallest free node that can fit the requested size.
| bytes | Size in bytes to allocate |
| void hh::halloc::Block::deallocate | ( | void * | ptr, |
| std::size_t | bytes | ||
| ) |
Deallocates previously allocated memory.
Deallocates previously allocated memory and merges with adjacent free blocks.
Marks the region as free and attempts to merge with adjacent free blocks to reduce fragmentation.
| ptr | Pointer returned from allocate() |
| bytes | Size parameter (currently unused) |
This function reverses the allocation:
Coalescing reduces fragmentation by combining adjacent free blocks into larger blocks.
| ptr | Pointer previously returned by allocate() (not the MemoryNode pointer) |
| bytes | Size parameter (unused, for interface compatibility) |
|
inline |
Gets pointer to the head node.
|
inline |
Gets total block size.
|
inline |
Logs the current state of the block to a file.
The log includes: Address: memory address of the block Total Size: of the block For each MemoryNode: Address | Size | Status (Free/Used) Summary statistics: Actual Free Space (The data used by the app) Total Used Space Headers Used Space Number of Free Nodes Number of Used Nodes
| logfile_dir | Path to the log file. |
Move assignment operator.
| other | Block to move from |