Modules

Relative_Pointers

moduleSource

Available on macOS, Linux, Windows, Wasm

#import "Relative_Pointers";

Pointers stored as a small offset from their own address, so a block of memory that holds them can be written to a file or moved and still make sense.

Example↗
Node :: struct {
    next: Relative_Pointer(s16, u8);
    value: u8;
}

nodes: [2] Node;
nodes[1].value = 9;
encode(*nodes[0].next, *nodes[1].value);
assert(decode(*nodes[0].next).* == 9);

Procedures#

(p: *Relative_Pointer, absolute: *p.Pointer_To_Type)

Points p at absolute, or clears it when absolute is null. The target must be within range of what Storage_Type can reach.

(relative_pointer_size: s32, address: *void) -> (pointer: *void, offset: s64)
(relative_pointer_size: s32, pointer_address: *void, base_address: *void) -> (pointer: *void, offset: s64)

Reads the relative pointer of relative_pointer_size bytes (1, 2, 4 or 8) at address. Returns the pointer, or null if it is unset, and its offset from address.

($type: Type, pointer_address: *void, target_address: *void)
($type: Type, pointer_address: *void, base_address: *void, target_address: *void)

Stores a relative pointer of integer type type at pointer_address that leads to target_address; null clears it.

slot: s16;
value: u8;
write_relative_pointer(s16, *slot, *value);
assert(decode_relative_pointer_static(s16, *slot) == *value);
↗

Types#

:: struct

A pointer to a Pointer_To_Type, stored as a signed Storage_Type (s8 to s64) offset from its own address. An offset of zero means null, so a pointer to its own address cannot be stored.

value: Storage_Type
:: struct

A string stored as a length and a relative pointer to its bytes, each a Storage_Type.

count: Storage_Type
data: Storage_Type