cptr::user_cage

A cage of 2^SizeLog2 bytes, compressed like cage<Tag, SizeLog2, Shift>, whose allocations go to Allocator.

Synopsis

Declared in <cptr/user_cage.hpp>

template<
    class Tag,
    region_allocator Allocator,
    unsigned int SizeLog2 = 32,
    unsigned int Shift = 0>
class [[visibility("default")]] user_cage;

Description

struct my_tag {}; using my_cage = cptr::user_cage<my_tag, my_allocator>; cptr::list<int, my_cage> list;

The allocator is given the region from the second 64 KiB span to the start of the last one, reserved but not committed (see cptr::vm). Offset 0 stays outside it, so it remains null, and so does the last span, so that one past the end of any block still compresses. With Shift > 0, every block must be aligned to 2^Shift; user_cage raises the alignment it asks for accordingly.

Type Aliases

Name
allocator_type
tag

Static Member Functions

NameDescription
allocate Throws std::bad_alloc when the allocator returns nullptr.
allocator The allocator, constructed by init(). It is never destroyed, so objects with static storage duration can free into it during exit.
base
compress The cage is aligned to its own size, so a pointer's offset is its low SizeLog2 bits: compressing is a mask and a shift, and nullptr maps to 0 with no branch. A pointer from outside the cage is caught only by CPTR_CHECK. In a constant expression only nullptr can be compressed.
contains
deallocate
decompress Zero maps back to nullptr. Clang turns the null check into a conditional move; GCC into a branch, which is cheap while it predicts well, as it does when most pointers are not null. In a constant expression only 0 can be decompressed.
decompress_nonnull
init Reserve the cage and construct the allocator. Called lazily by allocate(); returns false if the address space could not be reserved or the allocator's constructor threw. A failed call gives the address space back, and a later call tries again.
try_allocate As allocate(), but returns nullptr on failure. A request for zero bytes asks the allocator for one, so every allocation is distinct.

Static Data Members

Name
granularity
shift
size
span_size
usable_end