|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Memory-mapped file arena allocator. More...
#include <ah-map-arena.H>
Public Types | |
| using | iterator = char * |
| Iterator type for traversing allocated memory. | |
| using | const_iterator = const char * |
| Const iterator type. | |
| using | size_type = size_t |
| Size type for memory sizes. | |
Public Member Functions | |
| void | init (const std::string &file_path_name, void *addr=nullptr) |
| Initialize the arena with a backing file. | |
| void | init_and_erase (const std::string &file_path_name) |
| Initialize and erase any existing data. | |
| MapArena (const std::string &file_path_name) | |
| Construct an arena with a backing file. | |
| MapArena () noexcept=default | |
| Default constructor - creates an uninitialized arena. | |
| MapArena (const MapArena &)=delete | |
| Deleted copy constructor (arena cannot be copied). | |
| MapArena & | operator= (const MapArena &)=delete |
| Deleted copy assignment (arena cannot be copied). | |
| MapArena (MapArena &&other) noexcept | |
| Move constructor. | |
| MapArena & | operator= (MapArena &&other) noexcept |
| Move assignment operator. | |
| ~MapArena () | |
| Destructor - unmaps memory and closes file. | |
| iterator | begin () noexcept |
| Get iterator to the beginning of allocated memory. | |
| const_iterator | begin () const noexcept |
| Get const iterator to the beginning. | |
| iterator | end () noexcept |
| Get iterator past the last allocated byte. | |
| const_iterator | end () const noexcept |
| Get const iterator past the last allocated byte. | |
| char * | base () const noexcept |
| Get the base address of the mapped region. | |
| size_type | avail () const noexcept |
| Get the available memory in the current mapping. | |
| char * | reserve (const size_type sz) |
| Reserve memory for allocation. | |
| void | commit (const size_type sz) noexcept |
| Commit a previous reservation. | |
| void | sync () noexcept |
| Ensure data is persisted to disk. | |
| size_type | size () const noexcept |
| Get the total committed (allocated) size. | |
| size_type | capacity () const noexcept |
| Get the current capacity (mapped region size). | |
| bool | empty () const noexcept |
| Check if the arena is empty. | |
| bool | is_initialized () const noexcept |
| Check if the arena has been initialized. | |
| void * | mapped_addr () const noexcept |
| Get the mapped memory address. | |
| int | file_descriptor () const noexcept |
| Get the file descriptor. | |
Static Public Attributes | |
| static constexpr size_t | initial_rgn_size = 4 * 1024 |
| Initial region size in bytes (4 KB). | |
Private Member Functions | |
| bool | remap (const size_t sz) |
| Remap the memory region to accommodate more allocations. | |
Private Attributes | |
| char * | rgn_ptr = nullptr |
| Pointer to the mapped memory region. | |
| size_t | end_ = 0 |
| Allocation offset from rgn_ptr. | |
| size_t | rgn_size = initial_rgn_size |
| Current mapped region size. | |
| int | fd = -1 |
| File descriptor for the backing file. | |
Friends | |
| std::ostream & | operator<< (std::ostream &o, const MapArena &s) |
| Output operator for debugging. | |
Memory-mapped file arena allocator.
MapArena provides a persistent arena allocator backed by a memory-mapped file. It allows efficient allocation of memory chunks that are automatically persisted to disk, enabling crash recovery and data persistence across program restarts.
The arena uses a simple bump allocator model:
reserve(sz) returns a pointer to sz bytes of memorycommit(sz) marks sz bytes as allocated, advancing the allocation pointerThis class is NOT thread-safe. External synchronization is required for concurrent access from multiple threads.
reserve() may become invalid after subsequent reserve() calls that trigger a remapcommit() multiple times without intervening reserve() or committing more than was reserved leads to undefined behavior.Definition at line 127 of file ah-map-arena.H.
Const iterator type.
Definition at line 177 of file ah-map-arena.H.
Iterator type for traversing allocated memory.
Definition at line 174 of file ah-map-arena.H.
Size type for memory sizes.
Definition at line 180 of file ah-map-arena.H.
|
inlineexplicit |
Construct an arena with a backing file.
Creates or opens the specified file and maps it to memory. If the file already exists, it is assumed to be a recovery scenario and the stored state is recovered.
| [in] | file_path_name | Path to the backing file. |
| std::runtime_error | If initialization fails. |
Definition at line 252 of file ah-map-arena.H.
References Aleph::init, and Aleph::maps().
|
defaultnoexcept |
Default constructor - creates an uninitialized arena.
The arena must be initialized with init() before use.
Deleted copy constructor (arena cannot be copied).
|
inlinenoexcept |
Move constructor.
Transfers ownership of the mapped memory from another arena.
| [in,out] | other | Arena to move from (left in uninitialized state). |
Definition at line 275 of file ah-map-arena.H.
References initial_rgn_size, and Aleph::maps().
|
inline |
Destructor - unmaps memory and closes file.
Definition at line 317 of file ah-map-arena.H.
References fd, Aleph::maps(), rgn_ptr, and rgn_size.
|
inlinenoexcept |
Get the available memory in the current mapping.
Returns how many bytes can be allocated without triggering a remap.
Definition at line 364 of file ah-map-arena.H.
References end_, and rgn_size.
Referenced by print_arena_status(), remap(), reserve(), and TEST_F().
|
inlinenoexcept |
Get the base address of the mapped region.
Equivalent to begin(). Returns the first address of reserved memory, or the first available address if nothing has been reserved.
Definition at line 356 of file ah-map-arena.H.
References rgn_ptr.
|
inlinenoexcept |
Get const iterator to the beginning.
Definition at line 335 of file ah-map-arena.H.
References rgn_ptr.
|
inlinenoexcept |
Get iterator to the beginning of allocated memory.
Definition at line 329 of file ah-map-arena.H.
References rgn_ptr.
Referenced by demo_arena_growth(), demo_basic_operations(), demo_log_buffer(), demo_structured_data(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Get the current capacity (mapped region size).
Definition at line 431 of file ah-map-arena.H.
References rgn_size.
Referenced by demo_arena_growth(), demo_memory_stats(), print_arena_status(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
Commit a previous reservation.
Finalizes an allocation by advancing the end pointer. After commit(), the next reserve() will return addresses beyond the committed bytes.
| [in] | sz | Number of bytes to commit (must be <= reserved amount). |
Definition at line 403 of file ah-map-arena.H.
References end_.
Referenced by demo_arena_growth(), demo_basic_operations(), demo_log_buffer(), demo_memory_stats(), demo_structured_data(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Check if the arena is empty.
Definition at line 440 of file ah-map-arena.H.
References end_.
Referenced by print_arena_status(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Get const iterator past the last allocated byte.
Definition at line 347 of file ah-map-arena.H.
|
inlinenoexcept |
Get iterator past the last allocated byte.
Definition at line 341 of file ah-map-arena.H.
Referenced by demo_basic_operations(), demo_log_buffer(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Initialize the arena with a backing file.
Opens or creates the specified file and maps it to memory. If the file already exists, recovers the previous allocation state.
| [in] | file_path_name | Path to the backing file. |
| [in] | addr | Optional hint address for mmap (usually nullptr). |
| std::runtime_error | If file operations or mmap fails. |
Definition at line 192 of file ah-map-arena.H.
References ah_runtime_error_unless, end_, fd, Aleph::maps(), rgn_ptr, and rgn_size.
Initialize and erase any existing data.
Removes the file if it exists, then initializes a fresh arena.
| [in] | file_path_name | Path to the backing file. |
| std::runtime_error | If initialization fails. |
Definition at line 236 of file ah-map-arena.H.
References Aleph::init, and Aleph::maps().
Referenced by TEST_F().
|
inlinenoexcept |
|
inlinenoexcept |
Deleted copy assignment (arena cannot be copied).
Move assignment operator.
| [in,out] | other | Arena to move from. |
Definition at line 290 of file ah-map-arena.H.
References end_, fd, initial_rgn_size, Aleph::maps(), rgn_ptr, and rgn_size.
Remap the memory region to accommodate more allocations.
Doubles the region size repeatedly until it can hold sz additional bytes.
| [in] | sz | Required additional size in bytes. |
Definition at line 148 of file ah-map-arena.H.
References ah_runtime_error_unless, avail(), end_, fd, Aleph::maps(), rgn_ptr, and rgn_size.
Referenced by reserve().
Reserve memory for allocation.
Returns a pointer to sz bytes of memory. The memory is not yet committed; you must call commit() to finalize the allocation.
| [in] | sz | Number of bytes to reserve. |
Definition at line 383 of file ah-map-arena.H.
References avail(), end_, remap(), and rgn_ptr.
Referenced by demo_arena_growth(), demo_basic_operations(), demo_log_buffer(), demo_memory_stats(), demo_structured_data(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Get the total committed (allocated) size.
Definition at line 422 of file ah-map-arena.H.
References end_.
Referenced by demo_arena_growth(), demo_memory_stats(), print_arena_status(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Ensure data is persisted to disk.
Calls msync() to flush changes to the backing file. Uses MS_ASYNC for better performance (non-blocking).
Definition at line 413 of file ah-map-arena.H.
References Aleph::maps(), rgn_ptr, and rgn_size.
Referenced by demo_basic_operations(), TEST_F(), TEST_F(), and TEST_F().
Output operator for debugging.
| [in,out] | o | Output stream. |
| [in] | s | Arena to output. |
Definition at line 478 of file ah-map-arena.H.
|
private |
|
private |
File descriptor for the backing file.
Definition at line 139 of file ah-map-arena.H.
Referenced by ~MapArena(), file_descriptor(), init(), operator=(), and remap().
Initial region size in bytes (4 KB).
Definition at line 132 of file ah-map-arena.H.
Referenced by MapArena(), demo_arena_growth(), operator=(), TEST(), TEST_F(), TEST_F(), and TEST_F().
Pointer to the mapped memory region.
Definition at line 136 of file ah-map-arena.H.
Referenced by ~MapArena(), base(), begin(), begin(), end(), end(), init(), is_initialized(), mapped_addr(), operator=(), remap(), reserve(), and sync().
|
private |
Current mapped region size.
Definition at line 138 of file ah-map-arena.H.
Referenced by ~MapArena(), avail(), capacity(), init(), operator=(), remap(), and sync().