skip to content
==> include/mesh/place_compose.h <==
1/* @ajax/mesh-place-compose: moving a placed program's bytes (place.rs): its state regions read and
2 * written through the view, its frame parts gathered and scattered, and the Composer, which matches the
3 * parts of a frame arriving from several machines and yields a frame only when it has every part, all
4 * of the same frame. */
5#ifndef MESH_PLACE_COMPOSE_H
6#define MESH_PLACE_COMPOSE_H
7
8#include "mesh/place_spec.h"
9#include "mesh/place_wire.h"
10
11#include <stdbool.h>
12#include <stdint.h>
13
14/* Reads (view offset, bytes) into out (len bytes). */
15typedef void (*mpc_read_fn)(void *ctx, uint32_t off, uint32_t len, uint8_t *out);
16/* Writes bytes at a view offset. */
17typedef void (*mpc_write_fn)(void *ctx, uint32_t off, const uint8_t *b, uint32_t len);
18
19/* The state regions, concatenated. */
20mpw_bytes mpc_read_state(const mps_spec *s, mpc_read_fn read, void *ctx);
21/* Scatters concatenated state back to its regions; false (with *err) on a wrong length. */
22bool mpc_write_state(const mps_spec *s, const uint8_t *b, size_t n, mpc_write_fn write, void *ctx, char **err);
23/* The parts in mask (ascending), each part's piece of every output region. */
24mpw_bytes mpc_gather(const mps_spec *s, uint32_t mask, mpc_read_fn read, void *ctx);
25/* Places gathered parts into a show buffer (the output regions concatenated). */
26bool mpc_scatter(const mps_spec *s, uint8_t *buf, size_t nbuf, uint32_t mask, const uint8_t *data, size_t ndata, char **err);
27/* SHB_SYS for parts mask all rendered by sys (4 bits per part b < 8). */
28uint32_t mpc_sys_nibbles(uint32_t mask, uint32_t sys);
29
30/* Frames whose other parts have not arrived are kept this many deep. */
31#define MPC_PENDING 8
32
33typedef struct {
34    uint32_t frame, have, sys;
35    uint8_t *buf;
36    uint64_t t0;
37} mpc_pending;
38
39typedef struct {
40    mpc_pending q[MPC_PENDING];
41    size_t n;
42} mpc_composer;
43
44/* A frame with every part: the frame, SHB_SYS (who rendered each part), the buffer (taken), and when
45 * its first part came. */
46typedef struct {
47    uint32_t frame, sys;
48    uint8_t *buf;
49    size_t nbuf;
50    uint64_t t0;
51} mpc_whole;
52
53/* Adds parts of a frame: 1 with *whole when the frame is complete, 0 when not yet, -1 with *err. */
54int mpc_add(mpc_composer *c, const mps_spec *s, uint32_t frame, uint32_t parts, uint32_t sys, const uint8_t *data, size_t n, uint64_t now, mpc_whole *whole,
55            char **err);
56void mpc_clear(mpc_composer *c);
57
58#endif