Skip to content

File dp_state.h

FileList > doppler > dp_state.h

Go to the source code of this file

The standard state bytes interface for doppler.More...

  • #include "doppler/clib_common.h"

Classes

Type Name
struct dp_reader_t
Bounds-checked read cursor over a state blob; the mirror of dp_writer_t with the same sticky-error model (an overrun setserr , and later reads leave their destination untouched).
struct dp_state_hdr_t
Common 16-byte envelope at the head of every state blob.
struct dp_writer_t
Bounds-checked write cursor over a caller-owned state blob.

Public Static Functions

Type Name
void dp_r_bytes (dp_reader_t * r, void * dst, size_t n)
void dp_r_cf32 (dp_reader_t * r, float _Complex * p, size_t n)
void dp_r_f32 (dp_reader_t * r, float * p, size_t n)
double dp_r_f64 (dp_reader_t * r)
const void * dp_r_reserve (dp_reader_t * r, size_t n)
uint32_t dp_r_u32 (dp_reader_t * r)
uint64_t dp_r_u64 (dp_reader_t * r)
dp_reader_t dp_reader_init (const void * blob, size_t cap)
int dp_ring_head_ok (size_t head, size_t cap)
A dual-write delay ring's head, as a blob may carry it: in range.
int dp_state_validate (const void * blob, size_t expect_bytes, uint32_t magic, uint16_t version)
Validate a blob's envelope before trusting its payload.
void dp_w_bytes (dp_writer_t * w, const void * src, size_t n)
void dp_w_cf32 (dp_writer_t * w, const float _Complex * p, size_t n)
void dp_w_f32 (dp_writer_t * w, const float * p, size_t n)
void dp_w_f64 (dp_writer_t * w, double v)
void dp_w_hdr (dp_writer_t * w, uint32_t magic, uint16_t version, size_t total)
void * dp_w_reserve (dp_writer_t * w, size_t n)
void dp_w_u32 (dp_writer_t * w, uint32_t v)
void dp_w_u64 (dp_writer_t * w, uint64_t v)
void dp_w_zeros (dp_writer_t * w, size_t n)
dp_writer_t dp_writer_init (void * blob, size_t cap)

Macros

Type Name
define DP_DEFINE_POD_STATE (pfx, STATE_T, MAGIC, VERSION) [**DP\_DEFINE\_POD\_STATE\_CHECKED**](dp__state_8h.md#define-dp_define_pod_state_checked) (pfx, STATE\_T, MAGIC, VERSION, [**DP\_POD\_ANY\_OK**](dp__state_8h.md#define-dp_pod_any_ok))
DP_DEFINE_POD_STATE_CHECKED with no predicate.
define DP_DEFINE_POD_STATE_CHECKED (pfx, STATE_T, MAGIC, VERSION, OK) /* multi line expression */
Define the whole-struct state triplet for a pointer-free POD object, refusing a snapshot its object could never hold.
define DP_DEFINE_POD_STATE_TLM (pfx, STATE_T, MAGIC, VERSION, MEMBER) /* multi line expression */
DP_DEFINE_POD_STATE_TLM_CHECKED with no predicate.
define DP_DEFINE_POD_STATE_TLM_CHECKED (pfx, STATE_T, MAGIC, VERSION, MEMBER, OK) /* multi line expression */
Whole-struct POD triplet for an object carrying a live telemetry attachment (or any other non-state member that must not travel in the blob), refusing a snapshot its object could never hold.
define DP_DEFINE_RUN (pfx, STATE_T, IN_T, OUT_T) /* multi line expression */
Define the standard <pfx>_run pure-transducer wrapper.
define DP_FOURCC (a, b, c, d) /* multi line expression */
define DP_GET_OPEN (MAGIC, VERSION, BYTES) /* multi line expression */
define DP_POD_ANY_OK (p) ((void)(p), 1)
define DP_R_CHILD (r, pfx, child_ptr) /* multi line expression */
define DP_SET_OPEN (MAGIC, VERSION, BYTES) /* multi line expression */
define DP_STATE_ENDIAN 1u /\* little-endian (x86-64, arm64 — doppler targets) \*/
define DP_W_CHILD (w, pfx, child_ptr) /* multi line expression */

Detailed Description

Serialization is module-specific (only lo knows it holds a phase, only fir knows it holds a delay line); the bytes interface around it is not. This header owns that universal layer, once, for every serializable object:

  • a self-describing envelope (dp_state_hdr_t: magic / version / endian / size) that prefixes every state blob,
  • writer / reader cursors that turn hand-packing into a few bounds-checked calls,
  • dp_state_validate() — the one check every set_state opens with, so a wrong-object / wrong-config / foreign-endian blob is rejected, never silently reinterpreted, and
  • DP_DEFINE_RUN() — the identical obj_run pure-transducer wrapper.

The per-object ABI (the contract jm's serializable binding and the Rust FFI call) stays:

size_t obj_state_bytes(const obj_state_t *); void obj_get_state (const obj_state_t *, void *blob); int obj_set_state (obj_state_t *, const void *blob); // DP_OK / -err

Blob layout, every object: [ dp_state_hdr_t ] [ module payload ] Compositions embed children as self-contained sub-blobs (each carries its own header): [ hdr ] [ extra? ] [ child blob ]... with state_bytes = sizeof(hdr) + sizeof(extra) + Σ child_state_bytes.

Blobs are native-endian POD for same-machine / same-arch resume (thread, process, pod). The endian byte is stamped and rejected on mismatch; there is deliberately no cross-endian byte-swap.

Public Static Functions Documentation

function dp_r_bytes

static inline void dp_r_bytes (
    dp_reader_t * r,
    void * dst,
    size_t n
) 

Copy n bytes into dst; on overrun copy nothing and set err.


function dp_r_cf32

static inline void dp_r_cf32 (
    dp_reader_t * r,
    float _Complex * p,
    size_t n
) 

Read n complex float samples into p.


function dp_r_f32

static inline void dp_r_f32 (
    dp_reader_t * r,
    float * p,
    size_t n
) 

Read n float samples into p.


function dp_r_f64

static inline double dp_r_f64 (
    dp_reader_t * r
) 

Read a native double (0.0 on overrun).


function dp_r_reserve

static inline const void * dp_r_reserve (
    dp_reader_t * r,
    size_t n
) 

Borrow n bytes in place (for a child's set_state to read); NULL on overrun.


function dp_r_u32

static inline uint32_t dp_r_u32 (
    dp_reader_t * r
) 

Read a native-endian uint32_t (0 on overrun).


function dp_r_u64

static inline uint64_t dp_r_u64 (
    dp_reader_t * r
) 

Read a native-endian uint64_t (0 on overrun).


function dp_reader_init

static inline dp_reader_t dp_reader_init (
    const void * blob,
    size_t cap
) 

Start a reader at offset 0 over blob of cap bytes.


function dp_ring_head_ok

A dual-write delay ring's head, as a blob may carry it: in range.

static inline int dp_ring_head_ok (
    size_t head,
    size_t cap
) 

Every dual-write ring (buf[2 * cap], written at head and head + cap, read as the window &buf[head]) keeps head < cap. A blob's head is the one field such a ring indexes with, and the envelope never reads it, so a set_state checks it here before writing anything (#2142, #2111).

Parameters:

  • head The head decoded from the blob.
  • cap The ring's capacity, which create() fixed.

Returns:

Non-zero when head indexes the ring.


function dp_state_validate

Validate a blob's envelope before trusting its payload.

static inline int dp_state_validate (
    const void * blob,
    size_t expect_bytes,
    uint32_t magic,
    uint16_t version
) 

Every obj_set_state opens with this. expect_bytes is the receiving object's obj_state_bytes() — a blob from a different object (magic), format (version), endianness, or config/size (bytes) is rejected rather than reinterpreted. Only the header is inspected: the caller must already know blob holds at least sizeof(dp_state_hdr_t) readable bytes.

Parameters:

  • blob Blob to check; its first bytes are the header.
  • expect_bytes The receiving object's obj_state_bytes().
  • magic The receiving object's FourCC tag.
  • version The receiving object's blob format version.

Returns:

DP_OK, or DP_ERR_INVALID on any mismatch (or when expect_bytes is smaller than the header).


function dp_w_bytes

static inline void dp_w_bytes (
    dp_writer_t * w,
    const void * src,
    size_t n
) 

Copy n bytes from src; on overrun write nothing and set err.


function dp_w_cf32

static inline void dp_w_cf32 (
    dp_writer_t * w,
    const float _Complex * p,
    size_t n
) 

Write n complex float samples.


function dp_w_f32

static inline void dp_w_f32 (
    dp_writer_t * w,
    const float * p,
    size_t n
) 

Write n float samples.


function dp_w_f64

static inline void dp_w_f64 (
    dp_writer_t * w,
    double v
) 

Write a native double.


function dp_w_hdr

static inline void dp_w_hdr (
    dp_writer_t * w,
    uint32_t magic,
    uint16_t version,
    size_t total
) 

Stamp the standard envelope. total must equal obj_state_bytes().


function dp_w_reserve

static inline void * dp_w_reserve (
    dp_writer_t * w,
    size_t n
) 

Reserve n bytes and return the region (for a child's get_state to fill); NULL on overrun.


function dp_w_u32

static inline void dp_w_u32 (
    dp_writer_t * w,
    uint32_t v
) 

Write a native-endian uint32_t.


function dp_w_u64

static inline void dp_w_u64 (
    dp_writer_t * w,
    uint64_t v
) 

Write a native-endian uint64_t.


function dp_w_zeros

static inline void dp_w_zeros (
    dp_writer_t * w,
    size_t n
) 

Write n zero bytes: the part of a fixed-size region no live value fills. A byte get_state leaves alone is a byte of the caller's heap shipped in the blob (doppler#1471), and one copied from a buffer's unused tail is whatever that buffer held before either way the blob stops being a function of the object (#2076). Like dp_w_reserve, an overrun writes nothing and sets the writer's sticky error.


function dp_writer_init

static inline dp_writer_t dp_writer_init (
    void * blob,
    size_t cap
) 

Start a writer at offset 0 over blob of cap bytes.


Macro Definition Documentation

define DP_DEFINE_POD_STATE

DP_DEFINE_POD_STATE_CHECKED with no predicate.

#define DP_DEFINE_POD_STATE (
    pfx,
    STATE_T,
    MAGIC,
    VERSION
) `DP_DEFINE_POD_STATE_CHECKED (pfx, STATE_T, MAGIC, VERSION, DP_POD_ANY_OK )`

Correct only for an object none of whose values a blob can make dangerous or unreachable; an object whose blob can carry such a value needs the checked form with a real predicate.


define DP_DEFINE_POD_STATE_CHECKED

Define the whole-struct state triplet for a pointer-free POD object, refusing a snapshot its object could never hold.

#define DP_DEFINE_POD_STATE_CHECKED (
    pfx,
    STATE_T,
    MAGIC,
    VERSION,
    OK
) `/* multi line expression */`

Generates <pfx>_state_bytes/get_state/set_state that snapshot the entire STATE_T after the envelope. Correct only when the struct holds no pointers (the snapshot would capture a stale address): the running state and the derived config are serialized, and config restores identically into an identically-built instance. For a struct with pointers or a composition, hand-write the triplet (pack running fields / delegate to children) instead.

The envelope checks the blob's size, magic, version and endianness, never a payload field, so a blob with one field forged keeps its size and passes it (#2142: boxcar's pos then wrote past its ring). set_state therefore decodes the payload into a zeroed temporary, calls the predicate int OK (const STATE_T *) on it true for every state create() and the object's own methods can reach, and the same rule create() applies, stated over a stored struct (for example dp_loop_filter_state_ok(), #2641) and commits over s only when it holds. Otherwise set_state returns DP_ERR_INVALID and s is byte-identical to before.

A composition that embeds a checked child by value ANDs the child's predicate into its own, or a forged child field gets in through it.


define DP_DEFINE_POD_STATE_TLM

DP_DEFINE_POD_STATE_TLM_CHECKED with no predicate.

#define DP_DEFINE_POD_STATE_TLM (
    pfx,
    STATE_T,
    MAGIC,
    VERSION,
    MEMBER
) `DP_DEFINE_POD_STATE_TLM_CHECKED (pfx, STATE_T, MAGIC, VERSION, MEMBER,      \ DP_POD_ANY_OK )`


define DP_DEFINE_POD_STATE_TLM_CHECKED

Whole-struct POD triplet for an object carrying a live telemetry attachment (or any other non-state member that must not travel in the blob), refusing a snapshot its object could never hold.

#define DP_DEFINE_POD_STATE_TLM_CHECKED (
    pfx,
    STATE_T,
    MAGIC,
    VERSION,
    MEMBER,
    OK
) `/* multi line expression */`

Identical to DP_DEFINE_POD_STATE_CHECKED except that MEMBER a struct member holding live pointers/ids (e.g. an object's tlm attachment) is zeroed in the serialized copy (deterministic blobs, no leaked address) and kept live across set_state (a restore must not clobber the receiving instance's attachment with the sender's stale one).


define DP_DEFINE_RUN

Define the standard <pfx>_run pure-transducer wrapper.

#define DP_DEFINE_RUN (
    pfx,
    STATE_T,
    IN_T,
    OUT_T
) `/* multi line expression */`

Generates size_t <pfx>_run(state, state_in, state_out, in, n_in, out, max_out): restore state_in (or keep current), call <pfx>_execute, then export state_out — the (state_in, input) -> (state_out, output) face for elastic fan-out. For objects whose middle step is a single _execute; an object with a different step shape (e.g. acq's frame/push) defines its own.


define DP_FOURCC

#define DP_FOURCC (
    a,
    b,
    c,
    d
) `/* multi line expression */`

FourCC type tag, e.g. DP_FOURCC('A','C','Q','R'). Stored little-end-first so the bytes read as "ACQR" in a hex dump on a little-endian host.


define DP_GET_OPEN

#define DP_GET_OPEN (
    MAGIC,
    VERSION,
    BYTES
) `/* multi line expression */`

Open a hand-written get_state: declare writer _w and stamp the header.


define DP_POD_ANY_OK

#define DP_POD_ANY_OK (
    p
) `((void)(p), 1)`

A POD predicate that accepts every snapshot: the unchecked form's.


define DP_R_CHILD

#define DP_R_CHILD (
    r,
    pfx,
    child_ptr
) `/* multi line expression */`

Restore a child from the next nested sub-blob at reader r.

Nest a child's self-contained sub-blob. A composition's state_bytes sums <child>_state_bytes(child_ptr); its get/set then writes/reads each child as a nested envelope. child_ptr may be a pointer member or the address of an embedded-by-value member (&s->lf). DP_R_CHILD returns DP_ERR_INVALID from the enclosing set_state if the child rejects, so the whole restore is atomic-by-validation.


define DP_SET_OPEN

#define DP_SET_OPEN (
    MAGIC,
    VERSION,
    BYTES
) `/* multi line expression */`

Open a hand-written set_state: validate, then declare reader _r past the header (returns the validation error from the enclosing function).

For a hand-written triplet that packs a subset of fields: the running state and every mutator's value, while create() restores the config no mutator reaches (docs/design/state-serialization.md, "What goes in the blob", #2022). Use inside <obj>_get_state(s, blob) / <obj>_set_state(s, blob) (those exact param names): open, then pack/unpack the running fields via &_w / &_r, and return DP_OK; from set.

void foo_get_state (const foo_state_t *s, void *blob) { DP_GET_OPEN (FOO_MAGIC, FOO_VERSION, foo_state_bytes (s)); dp_w_f64 (&_w, s->gain); } int foo_set_state (foo_state_t *s, const void *blob) { DP_SET_OPEN (FOO_MAGIC, FOO_VERSION, foo_state_bytes (s)); s->gain = dp_r_f64 (&_r); return DP_OK; }


define DP_STATE_ENDIAN

#define DP_STATE_ENDIAN `1u /* little-endian (x86-64, arm64 — doppler targets) */`

Host byte-order tag stamped in every blob: 1 = little, 2 = big.


define DP_W_CHILD

#define DP_W_CHILD (
    w,
    pfx,
    child_ptr
) `/* multi line expression */`

Write a child's state as a nested sub-blob at writer w.



The documentation for this class was generated from the following file native/inc/doppler/dp_state.h