cptr

compressed-ptr: 4-byte pointers and containers for 64-bit C++20.

Description

Objects live in a cage, a reserved region of up to 4 GiB (or 64 GiB with a shift). A pointer into the cage is stored as a 32-bit offset, which halves the size of every link. Start here:

The one rule: only compress pointers to objects allocated in the cage. Debug builds assert it.


#include "cptr/all.hpp"

struct node {
  cptr::ptr_pair<node> children;  // two 4-byte pointers
  int key;
};

int main() {
  cptr::list<int> l{1, 2, 3};             // like std::list, half the memory
  cptr::ptr<node> n = cptr::make<node>();  // a node in the cage
  n->key = l.back();
  cptr::destroy(n);
}

Guide, benchmarks and examples: https://mvanslobbe.github.io/compressed-ptr/

Namespaces

NameDescription
detail Implementation classes. Use the aliases in cptr, such as cptr::list.
vm Virtual memory helpers for allocators that manage a cage's region. The region starts out reserved but inaccessible: commit a range before using it.

Types

NameDescription
allocator A standard allocator that places objects in the cage, so that any std container's elements can be pointed to with a 4-byte cptr::ptr.
atomic_ptr A compressed pointer that can be updated atomically.
basic_ptr A nullable pointer to a T inside Cage, stored in 4 bytes. Use it as ptr<T> or strict_ptr<T>, declared below.
bump_allocator A thread-safe bump (monotonic) allocator for user_cage: allocation is a pointer increment, deallocate() does nothing, and memory is committed in 1 MiB steps as it is used. Suited to data that is built once and freed all at once, or never. Its lock is held across fork(), so a child process can allocate even if another thread was allocating when it forked.
cage 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.
default_cage The default cage: 4 GiB, byte granularity.
deleter A deleter that calls destroy(), so that std::unique_ptr can own an object created by make(). Its pointer is ptr, so the unique_ptr itself is 4 bytes.
fancy_allocator An allocator whose pointer type is cptr::ptr, so that containers that take their link type from the allocator get 4-byte links.
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.
nonnull_ptr Like ptr, but never null, which makes decompression a single add.
ptr_pair Two compressed pointers in 8 bytes, e.g. a tree node's children or a list node's links.
ptr_with_payload A compressed pointer and a 32-bit payload in 8 bytes, e.g. a node pointer and a count, hash or flags.
tagged_ptr A compressed pointer with TagBits small tag bits stored in its alignment bits, all in 4 bytes. T must be aligned to 2^(TagBits + Cage::shift).
user_cage A cage of 2^SizeLog2 bytes, compressed like cage<Tag, SizeLog2, Shift>, whose allocations go to Allocator.

Type Aliases

NameDescription
forward_list A singly linked list with 4-byte links, like std::forward_list without the allocator parameter.
list A doubly linked list with 4-byte links, like std::list without the allocator parameter.
ordered_map An ordered map with 4-byte links, like std::map without the allocator parameter and node handles.
ptr The usual compressed pointer: converts implicitly from and to T*.
strict_ptr Compressing a T* needs an explicit strict_ptr<T>(p).
unordered_map A hash map with 4-byte links, like std::unordered_map without the allocator parameter, node handles and the bucket interface.

Functions

NameDescription
destroy destroy overloads
destroy_array Destroy and free n elements created by make_array(n). n must be the count passed to make_array(), and p the pointer it returned (not a pointer to a base class: as for delete[], that is undefined). Null is a no-op.
make Allocate and construct a T inside the cage, as new T(args...) does on the heap; free it with destroy().
make_array Allocate n value-initialised T in one block inside the cage, as new Tn does on the heap; free them with destroy_array() and the same n. The count is not stored, which keeps the block the size of the elements, so it is the caller's to keep, as for std::allocator.
operator+ The pointer n elements further on.
operator- Subtraction operators
operator== Equality operators
operator<=> Three-way comparison operators

Concepts

NameDescription
region_allocator What user_cage needs from an allocator. It is constructed once, with the part of the cage it may hand out, and must be safe to call from every thread that uses the cage.