cptr::mapped_cage

A cage of 2^SizeLog2 bytes whose memory is the file opened with open(). It has the interface the containers and cptr::ptr need, plus a root pointer stored in the file to find the data again after reopening.

Synopsis

Declared in <cptr/mapped_cage.hpp>

template<
    class Tag,
    unsigned int SizeLog2 = 32>
class [[visibility("default")]] mapped_cage;

Description

struct store_tag {}; using store = cptr::mapped_cage<store_tag>; store::open("data.cage"); auto* r = store::root<my_root>(); // nullptr in a new file

The file is created sparse at the full cage size; only pages written to take disk space. Everything that must survive reopening has to live in the cage and point into it with cptr::ptr: a cptr::list or cptr::ordered_map of trivially copyable values qualifies, a std::string does not.

The allocator's state is in the file too, behind a spin lock in the file, so several processes may open, create and allocate in the same file at once (see open()). The containers themselves are not synchronised. A process that dies while holding the spin lock leaves the file locked, and one that dies part way through an allocation can leave the allocator state inconsistent; open() rejects state that is out of range, not every inconsistency. Freed small blocks are kept per size class; freed large runs are kept in one list, first fit, without coalescing.

Type Aliases

Name
tag

Static Member Functions

NameDescription
allocate
allocated_bytes Bytes handed out and not yet freed, rounded up to the size class.
base Zero while no file is open.
close Write the file's changed pages back and unmap it. Pointers into the cage are invalid afterwards.
compress A mask, as the mapping is aligned to the cage size; see cage::compress().
contains
deallocate
decompress See cage::decompress().
decompress_nonnull
flush Write changed pages back to the file without unmapping it.
is_open
open Map the file at path as the cage, creating it if it does not exist. Fails if a file is already open, if the file is not a cage of this size, if its allocator state is out of range (a corrupt file), or if address space or the file cannot be had.
previous_base Where the file was mapped the last time before this open(), or 0 for a new file.
root The object the file's user designated with set_root(), or nullptr, also while no file is open.
set_root Designate the object root() returns. Does nothing while no file is open.
try_allocate Returns nullptr when the cage is full, no file is open, or align is above 64 KiB (the span size).

Static Data Members

NameDescription
granularity
shift
size
span_count
span_size
usable_end The last span is never handed out, so one past the end of any block compresses; see cage::usable_end.