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

Type-safe allocator using Red-Black tree for best-fit allocation. More...

#include <Halloc.hpp>

Classes

struct  rebind
 Rebind allocator to allocate different type U. More...
 

Public Types

using value_type = T
 Type of allocated objects.
 
using pointer = T *
 Pointer to value_type.
 
using const_pointer = const T *
 Const pointer to value_type.
 
using reference = T &
 Reference to value_type.
 
using const_reference = const T &
 Const reference to value_type.
 
using size_type = std::size_t
 Type for sizes.
 
using difference_type = std::ptrdiff_t
 Type for pointer differences.
 

Public Member Functions

 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.
 
Hallocoperator= (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.
 

Friends

template<typename U , int BS, int MNB>
class Halloc
 

Detailed Description

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:

Template Parameters
TType of objects to allocate (default: void for raw bytes)
BlockSizeSize of each memory block in bytes (default: 256 MB)
MaxNumBlocksMaximum number of blocks (default: 4)
Note
Compatible with STL containers via std::allocator_traits

Member Typedef Documentation

◆ const_pointer

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
using hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::const_pointer = const T*

Const pointer to value_type.

◆ const_reference

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
using hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::const_reference = const T&

Const reference to value_type.

◆ difference_type

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
using hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::difference_type = std::ptrdiff_t

Type for pointer differences.

◆ pointer

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
using hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::pointer = T*

Pointer to value_type.

◆ reference

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
using hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::reference = T&

Reference to value_type.

◆ size_type

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
using hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::size_type = std::size_t

Type for sizes.

◆ value_type

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
using hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::value_type = T

Type of allocated objects.

Constructor & Destructor Documentation

◆ Halloc() [1/3]

template<typename T , int BlockSize, int MaxNumBlocks>
hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::Halloc ( )

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
TType of objects to allocate
BlockSizeSize of each block in bytes
MaxNumBlocksMaximum number of blocks
Postcondition
blocks points to a new BlocksContainer with one block of size BlockSize

◆ Halloc() [2/3]

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::Halloc ( const Halloc< T, BlockSize, MaxNumBlocks > &  other)
inline

Copy constructor - shares underlying BlocksContainer.

Parameters
otherAllocator to copy from
Postcondition
this->blocks points to same container as other.blocks

◆ Halloc() [3/3]

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
template<typename U >
hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::Halloc ( const Halloc< U, BlockSize, MaxNumBlocks > &  other)
inline

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
UDifferent value_type
Parameters
otherAllocator of different type to copy from

◆ ~Halloc()

template<typename T , int BlockSize, int MaxNumBlocks>
hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::~Halloc ( )

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
TType of objects
BlockSizeSize of each block in bytes
MaxNumBlocksMaximum number of blocks
Postcondition
If this was the last reference, all memory is returned to the OS

Member Function Documentation

◆ allocate()

template<typename T , int BlockSize, int MaxNumBlocks>
T * hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::allocate ( std::size_t  count)

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
countNumber 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
TType of objects to allocate
BlockSizeSize of each block in bytes
MaxNumBlocksMaximum number of blocks
Parameters
countNumber 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

◆ deallocate()

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
ptrPointer previously returned by allocate()
countNumber of objects (must match allocate() call)
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
TType of objects (must match allocate call)
BlockSizeSize of each block in bytes
MaxNumBlocksMaximum number of blocks
Parameters
ptrPointer to deallocate
countNumber of objects (must match allocate call)
Note
Does NOT call destructors - caller must destroy objects manually

◆ log_container_state()

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
void hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::log_container_state ( const char *  logfile_dir) const
inline

Logs the current state of the container to a file.

Parameters
logfile_dirDirectory to write the log file

◆ operator!=()

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
bool hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::operator!= ( const Halloc< T, BlockSize, MaxNumBlocks > &  other) const
inline

Inequality comparison.

Parameters
otherAnother Halloc instance
Returns
true if allocators don't share same container, false otherwise

◆ operator=()

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
Halloc & hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::operator= ( const Halloc< T, BlockSize, MaxNumBlocks > &  other)
inline

Assignment operator.

Parameters
otherAllocator to assign from
Returns
Reference to this

◆ operator==()

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
bool hh::halloc::Halloc< T, BlockSize, MaxNumBlocks >::operator== ( const Halloc< T, BlockSize, MaxNumBlocks > &  other) const
inline

Equality comparison - checks if allocators share same container.

Two allocators are equal if they can deallocate each other's allocations, which is true when they share the same BlocksContainer.

Parameters
otherAnother Halloc instance
Returns
true if both share same BlocksContainer, false otherwise

Friends And Related Symbol Documentation

◆ Halloc

template<typename T = void, int BlockSize = DEFAULT_BLOCK_SIZE, int MaxNumBlocks = DEFAULT_MAX_NUM_BLOCKS>
template<typename U , int BS, int MNB>
friend class Halloc
friend

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