|
HAllocator
A Simple C++ Memory Allocator
|
Classes | |
| struct | MemNode |
| Metadata structure for each memory block. More... | |
Typedefs | |
| using | MemSizeT = unsigned long long |
| Type definition for memory size. | |
Functions | |
| bool | is_free (MemSizeT &size) |
| Check if block is free using bit 63. | |
| void | make_free (MemSizeT &size) |
| Mark block as free by setting bit 63. | |
| void | make_used (MemSizeT &size) |
| Mark block as used by clearing bit 63. | |
| MemSizeT | get_size (MemSizeT &size) |
| Extract actual size by masking off bit 63. | |
| MemSizeT | add (MemSizeT a, MemSizeT b) |
| Add two sizes (clearing free bits first). | |
| MemSizeT | sub (MemSizeT a, MemSizeT b) |
| Subtract two sizes (clearing free bits first). | |
| void * | sbrk_then_alloc (MemSizeT size) |
| Request memory from OS using sbrk and allocate. | |
| void | coalesce_nodes (MemNode *nd) |
| Merge a free node with adjacent free nodes. | |
| void * | free (void *ptr) |
| Free a memory block and merge with adjacent free blocks. | |
| void | shrink_then_align (MemNode *nd, MemSizeT size) |
| Shrink a block and create a new free block from remainder. | |
| void * | try_alloc (MemSizeT size) |
| Allocate memory using first-fit strategy. | |
| void | mem_copy (void *dest, const void *src, size_t n) |
| Copy n bytes from source to destination. | |
| void * | try_realloc (void *ptr, MemSizeT size) |
| Reallocate a memory block to a new size. | |
| void | mem_set (void *ptr, int value, size_t num) |
| Set num bytes to specified value. | |
| void * | try_calloc (size_t num, size_t size) |
| Allocate and zero-initialize an array. | |
| void | alloc_print () |
| Print allocation status table for debugging. | |
Variables | |
| MemNode * | __head = nullptr |
| Pointer to the head of the memory block linked list. | |
| MemNode * | __tail = nullptr |
| constexpr MemSizeT | MIN_FRAGMENT_SIZE = 32 |
| Minimum fragment size to consider splitting a block. | |
| constexpr MemSizeT | BLOCK_SIZE = 4096 |
| Size of each memory block requested from OS via sbrk. | |
| constexpr MemSizeT | MEM_NODE_SIZE = sizeof(MemNode) |
| Size of the MemNode structure. | |
| using hh::basic_alloc::MemSizeT = typedef unsigned long long |
Type definition for memory size.
Add two sizes (clearing free bits first).
Add two memory sizes with overflow checking.
| a | First operand |
| b | Second operand |
| a | First operand |
| b | Second operand |
| std::overflow_error | if addition overflows |
| void hh::basic_alloc::alloc_print | ( | ) |
Print allocation status table for debugging.
Print allocation status for debugging.
Displays all blocks with:
Displays all blocks (free and used) with their sizes and addresses.
| void hh::basic_alloc::coalesce_nodes | ( | MemNode * | nd | ) |
Merge a free node with adjacent free nodes.
Merge adjacent free blocks to reduce fragmentation.
Attempts to coalesce:
This reduces fragmentation by combining adjacent free blocks.
| nd | Node to merge (must be free) |
Attempts to merge the given free node with:
| nd | Node to merge (must be free) |
| void * hh::basic_alloc::free | ( | void * | ptr | ) |
Free a memory block and merge with adjacent free blocks.
Free a previously allocated memory block.
| ptr | Pointer to memory (returned by try_alloc) |
Marks the block as free and attempts to merge with adjacent free blocks.
| ptr | Pointer to memory (returned by try_alloc) |
|
inline |
|
inline |
|
inline |
| void hh::basic_alloc::mem_copy | ( | void * | dest, |
| const void * | src, | ||
| size_t | n | ||
| ) |
Copy n bytes from source to destination.
Copy memory from source to destination.
| dest | Destination pointer |
| src | Source pointer |
| n | Number of bytes to copy |
| dest | Destination pointer |
| src | Source pointer |
| n | Number of bytes to copy |
| void hh::basic_alloc::mem_set | ( | void * | ptr, |
| int | value, | ||
| size_t | num | ||
| ) |
Set num bytes to specified value.
Set a block of memory to a specific value.
| ptr | Pointer to memory |
| value | Value to set (cast to unsigned char) |
| num | Number of bytes to set |
| ptr | Pointer to memory block |
| value | Value to set (cast to unsigned char) |
| num | Number of bytes to set |
| void * hh::basic_alloc::sbrk_then_alloc | ( | MemSizeT | size | ) |
Request memory from OS using sbrk and allocate.
Request memory from OS and allocate.
Extends program break by size + metadata, creates new MemNode, and adds it to the tail of the linked list.
| size | Requested allocation size (excluding metadata) |
| std::bad_alloc | if sbrk fails |
Uses sbrk() to extend the program break by at least size bytes (rounded up to BLOCK_SIZE multiples). Creates a new MemNode and returns pointer to usable memory.
| size | Number of bytes requested |
Shrink a block and create a new free block from remainder.
If the fragment is large enough (> MIN_FRAGMENT_SIZE + metadata), splits the block:
| nd | Node to shrink |
| size | Desired size for node |
If the node is significantly larger than requested size, splits it:
| nd | Node to shrink |
| size | Desired size for node |
Subtract two sizes (clearing free bits first).
Subtract two memory sizes with underflow checking.
| a | Minuend |
| b | Subtrahend |
| a | Minuend |
| b | Subtrahend |
| std::underflow_error | if subtraction underflows |
| void * hh::basic_alloc::try_alloc | ( | MemSizeT | size | ) |
Allocate memory using first-fit strategy.
Allocate a memory block using first-fit strategy.
Searches linked list for first free block large enough. If found, allocates from that block (splitting if necessary). If not found, requests more memory from OS via sbrk.
| size | Number of bytes to allocate |
Searches the linked list for the first free block large enough to satisfy the request. If found, splits if necessary and marks as used. If not found, requests more memory from OS via sbrk_then_alloc.
| size | Number of bytes to allocate |
| void * hh::basic_alloc::try_calloc | ( | size_t | num, |
| size_t | size | ||
| ) |
Allocate and zero-initialize an array.
| num | Number of elements |
| size | Size of each element |
Allocates num * size bytes and initializes all bytes to zero.
| num | Number of elements |
| size | Size of each element |
| void * hh::basic_alloc::try_realloc | ( | void * | ptr, |
| MemSizeT | size | ||
| ) |
Reallocate a memory block to a new size.
If new size fits in current block, shrinks in place. Otherwise, allocates new block, copies data, and frees old block.
| ptr | Pointer to existing allocation (or nullptr) |
| size | New size in bytes |
Attempts to resize the block in place if possible. Otherwise:
| ptr | Pointer to existing allocation |
| size | New size in bytes |
| MemNode * hh::basic_alloc::__head = nullptr |
Pointer to the head of the memory block linked list.
| MemNode * hh::basic_alloc::__tail = nullptr |
|
constexpr |
Size of each memory block requested from OS via sbrk.
Size of the MemNode structure.
|
constexpr |
Minimum fragment size to consider splitting a block.