|
| | Halloc () |
| | Default constructor - creates allocator with one initial block.
|
| |
| | Halloc (const Halloc &other) |
| | Copy constructor - shares underlying BlocksContainer.
|
| |
| template<typename U > |
| | Halloc (const Halloc< U, BlockSize, MaxNumBlocks > &other) |
| | Rebind copy constructor - shares BlocksContainer across types.
|
| |
| Halloc & | operator= (const Halloc &other) |
| | Assignment operator.
|
| |
| T * | allocate (std::size_t count) |
| | Allocates memory for 'count' objects of type T.
|
| |
| void | deallocate (T *ptr, std::size_t count) |
| | Deallocates memory previously allocated for 'count' objects.
|
| |
| bool | operator== (const Halloc &other) const |
| | Equality comparison - checks if allocators share same container.
|
| |
| bool | operator!= (const Halloc &other) const |
| | Inequality comparison.
|
| |
| | ~Halloc () |
| | Destructor - releases all blocks back to the OS.
|
| |
| void | log_container_state (const char *logfile_dir) const |
| | Logs the current state of the container to a file.
|
| |
template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
class hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >
Type-safe allocator using Red-Black tree for best-fit allocation.
This class provides a high-level allocator interface compatible with STL containers. It manages multiple memory blocks (via BlocksContainer) and provides type-safe allocation/deallocation for objects of type T.
Key features:
- Best-fit allocation strategy (minimizes fragmentation)
- O(log n) allocation and deallocation (Red-Black tree)
- Automatic block coalescing (merges adjacent free blocks)
- Automatic block creation up to MaxNumBlocks limit
- Thread-unsafe (caller must synchronize)
- Template Parameters
-
| T | Type of objects to allocate (default: void for raw bytes) |
| BlockSize | Size of each memory block in bytes (default: 256 MB) |
| MaxNumBlocks | Maximum number of blocks (default: 4) |
- Note
- Compatible with STL containers via std::allocator_traits
template<typename T , int BlockSize, int MaxNumBlocks>
Default constructor - creates allocator with one initial block.
Constructor - initializes allocator with BlocksContainer.
- Postcondition
- blocks contains one initialized block of size BlockSize
Creates a new BlocksContainer via shared_ptr. The container automatically initializes with one block. Multiple Halloc instances can share the same container via copy construction.
- Template Parameters
-
| T | Type of objects to allocate |
| BlockSize | Size of each block in bytes |
| MaxNumBlocks | Maximum number of blocks |
- Postcondition
- blocks points to a new BlocksContainer with one block of size BlockSize
template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
template<typename U >
Rebind copy constructor - shares BlocksContainer across types.
Allows containers to create allocators for different types (e.g., vector<int> creates allocators for internal structures).
- Template Parameters
-
- Parameters
-
| other | Allocator of different type to copy from |
template<typename T , int BlockSize, int MaxNumBlocks>
Destructor - releases all blocks back to the OS.
Destructor - releases resources.
Each Block destructor will call munmap to return memory to the operating system.
- Postcondition
- All memory blocks are returned to OS
- Warning
- Do not use any pointers allocated by this allocator after destruction
When the last Halloc instance sharing a BlocksContainer is destroyed, the shared_ptr automatically destructs the BlocksContainer, which releases all blocks via their destructors (munmap).
- Template Parameters
-
| T | Type of objects |
| BlockSize | Size of each block in bytes |
| MaxNumBlocks | Maximum number of blocks |
- Postcondition
- If this was the last reference, all memory is returned to the OS
template<typename T , int BlockSize, int MaxNumBlocks>
Allocates memory for 'count' objects of type T.
Requests count * sizeof(T) bytes from the underlying BlocksContainer. If no suitable block exists and space is available, creates a new block.
- Parameters
-
| count | Number of objects to allocate space for |
- Returns
- Pointer to allocated memory, or nullptr if allocation fails
- Precondition
- count > 0
- Postcondition
- If successful, returned pointer is valid and aligned for T
-
If unsuccessful (nullptr), no state was modified
- Note
- Allocation fails if:
- count * sizeof(T) > BlockSize
- All blocks full and MaxNumBlocks reached
-
Does NOT construct objects (use placement new if needed)
Calculates total size as count * sizeof(T) and requests allocation from the BlocksContainer. The container performs best-fit search across all blocks and creates new blocks if needed.
- Template Parameters
-
| T | Type of objects to allocate |
| BlockSize | Size of each block in bytes |
| MaxNumBlocks | Maximum number of blocks |
- Parameters
-
| count | Number of objects to allocate space for |
- Returns
- Typed pointer to allocated memory, or nullptr if allocation fails
- Note
- Does NOT call constructors - caller must use placement new
template<typename T , int BlockSize, int MaxNumBlocks>
| void hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::deallocate |
( |
T * |
ptr, |
|
|
std::size_t |
count |
|
) |
| |
Deallocates memory previously allocated for 'count' objects.
Deallocates memory for 'count' objects of type T.
Returns memory to the owning block, marks it as free, and attempts to merge with adjacent free blocks to reduce fragmentation.
- Parameters
-
- Precondition
- ptr != nullptr
-
ptr was returned by this allocator's allocate()
-
count matches the value passed to allocate()
- Postcondition
- Memory is freed and merged with adjacent free blocks
- Note
- Destroy objects (call destructors manually if needed)
- Warning
- Undefined behavior if ptr was not allocated by this allocator
Calculates total size as count * sizeof(T) and passes to BlocksContainer for deallocation. The container finds the owning block and performs deallocation with automatic coalescing.
- Template Parameters
-
| T | Type of objects (must match allocate call) |
| BlockSize | Size of each block in bytes |
| MaxNumBlocks | Maximum number of blocks |
- Parameters
-
| ptr | Pointer to deallocate |
| count | Number of objects (must match allocate call) |
- Note
- Does NOT call destructors - caller must destroy objects manually