A cage of 2^SizeLog2 bytes. Compressed pointers are 32-bit offsets shifted right by Shift, so a cage can be up to 2^(32 + Shift) bytes as long as all compressed pointers are aligned to 2^Shift.
Declared in <cptr/cage.hpp>
template<
class Tag,
unsigned int SizeLog2 = 32,
unsigned int Shift = 0>
class [[visibility("default")]] cage;
Tag makes each cage a distinct type with its own static base address, so decompression is a load of a global and an add. Define one cage type per heap you want to compress, for example:
struct my_heap : cptr::cage<my_heap> {};
| Name |
|---|
tag |
| Name | Description |
|---|---|
allocate | Allocate memory inside the cage. Throws std::bad_alloc when the cage is full or cannot be reserved. |
allocated_bytes | Bytes handed out and not yet freed, rounded up to the size class. While other threads allocate and free, this is a snapshot that can be off by the blocks they move meanwhile. |
base | Zero until init() succeeds. Any thread that obtained a pointer into the cage is ordered after init(), so reading the base needs no atomics. |
committed_bytes | Bytes of the cage backed by committed memory. |
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 | Free memory from allocate(). The size is not needed: the span table records how every span is used. |
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. Called lazily by allocate(); call it early to fail fast. Returns false if the address space could not be reserved. |
resource | A std::pmr::memory_resource that allocates from this cage. It is never destroyed, so containers with static storage duration can still free into it during program exit, after the thread caches are gone: such frees go straight to the slabs. |
trim | Return the memory the allocator keeps for reuse to the OS: this thread's cached blocks go back to their slabs, and every slab with no live block is released, including the slab each size class is currently carving blocks from, as is the committed but unused end of the bump region. Blocks cached by other threads count as live; they go back when those threads exit. Without trim(), a slab that becomes empty is kept for reuse until it has been empty for a quarter of a second (checked whenever a slab becomes empty) or more than 64 MiB of empty slabs are kept. |
try_allocate | As allocate(), but returns nullptr on failure. |
| Name | Description |
|---|---|
granularity | Every compressible pointer must be aligned to this. |
shift | |
size | |
span_count | |
span_size | The first span is never handed out, so offset zero stays nullptr. The last is not either, so that one past the end of any block is inside the cage and compresses; for a 4 GiB cage the end itself would wrap to 0. |
usable_end |
| Name | Description |
|---|---|
default_cage | The default cage: 4 GiB, byte granularity. |