libmizu 0.2.0
Lock-free shared-memory IPC at thread speed in C11
Loading...
Searching...
No Matches
mizu_ext.h
Go to the documentation of this file.
1
45
46#ifndef MIZU_EXT_H
47#define MIZU_EXT_H
48
49#include <math.h>
50#include <string.h>
51
52#include "mizu.h"
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
61#if defined(__GNUC__) || defined(__clang__)
62# define MIZU_EXT_INLINE static inline __attribute__((unused))
63#else
64# define MIZU_EXT_INLINE static inline
65#endif
66
69#ifdef __cplusplus
70# define MIZU_EXT_LOAD_ACQ(p) \
71 std::atomic_load_explicit((p), std::memory_order_acquire)
72#else
73# define MIZU_EXT_LOAD_ACQ(p) \
74 atomic_load_explicit((p), memory_order_acquire)
75#endif
76
77// Binding-tier constants ---------------------------------------------------------
78
79#ifdef _WIN32
80# define MIZU_PREFIX_LITERAL "Local\\mizu_"
81#else
82# define MIZU_PREFIX_LITERAL "/mizu_"
83#endif
84
85#define MIZU_ALIGN64(x) (((x) + 63) & ~(size_t) 63)
86
92#define MIZU_ZC_FLOOR ((size_t) 32768)
93#define MIZU_ZC_FLOOR_RAW ((size_t) (256 << 10))
94
97#define MIZU_OPEN_CACHE_MAX 16
98
104#define MIZU_CODEC_MAGIC 0x52u
105#define MIZU_PYMIZU_CODEC_MAGIC 0x50u
106#define MIZU_INTEROP_MAGIC 0x49u
108
109// Language and capability registries ----------------------------------------------
110
119#define MIZU_LANG_NONE 0u
120#define MIZU_LANG_BYTES 1u
121#define MIZU_LANG_R 2u
122#define MIZU_LANG_PYTHON 3u
123
133#define MIZU_CAP_MIZS (1u << 0)
134#define MIZU_CAP_ATTRS (1u << 1)
136#define MIZU_CAP_MIZL (1u << 2)
137#define MIZU_CAP_TASKREF (1u << 3)
140#define MIZU_CAP_MIZL_REF (1u << 4)
142#define MIZU_CAP_MIZL_TSN (1u << 5)
144#define MIZU_CAP_MIZL_U64V (1u << 6)
146#define MIZU_CAP_LABELED (1u << 7)
150#define MIZU_CAP_ORDERED (1u << 8)
154
159#define MIZU_IDENT(lang, caps) \
160 ((uint64_t) (uint8_t) (lang) | ((uint64_t) (uint32_t) (caps) << 32))
161
162// Handle views -------------------------------------------------------------------
163
167typedef struct mizu_handle_s mizu_handle;
171
174enum { MIZU_HTYPE_CHANNEL = 1, MIZU_HTYPE_POOL = 2 };
175
187MIZU_API int mizu_handle_churn(const mizu_handle *);
192MIZU_API void mizu_handle_spill_info(const mizu_handle *,
193 uint32_t *fl_entries,
194 uint32_t *ledger_entries);
195
196// The binding seam ---------------------------------------------------------------
197
301
302typedef int (*mizu_stage_fn)(void *obj, mizu_slot_hdr *hdr,
303 uint8_t *payload, uint32_t inline_max,
304 mizu_handle *, void *ctx);
305
312#define MIZU_READ_CONSUME 1u
313
314typedef struct mizu_read_ctx_s {
315 uint32_t size;
316 int32_t outcome;
318 int32_t gone;
320 int32_t died_slot;
321 int64_t died_pid;
324 uint32_t flags;
325 void *reserved[4];
326} mizu_read_ctx;
327
328typedef void *(*mizu_read_fn)(const mizu_slot_hdr *, const uint8_t *payload,
329 size_t limit, mizu_read_ctx *);
330
331typedef int (*mizu_exec_fn)(const mizu_slot_hdr *hdr, const uint8_t *payload,
332 size_t limit, mizu_result_sink *, int catching,
333 mizu_read_ctx *ctx);
334typedef int (*mizu_check_fn)(void *ctx);
335typedef void (*mizu_park_fn)(void *ctx, int entering);
336typedef void (*mizu_sweep_fn)(void *ctx);
337typedef void (*mizu_drop_fn)(void *ctx, void *pin);
338
339typedef struct mizu_binding_s {
340 uint32_t size;
342 mizu_stage_fn stage;
343 mizu_read_fn read;
344 mizu_exec_fn exec;
345 mizu_check_fn check;
346 mizu_park_fn park;
347 mizu_sweep_fn sweep;
348 mizu_drop_fn drop;
349 void *ctx;
350 uint64_t ident;
352 uint32_t flags;
355
359#define MIZU_BINDING_SPLIT_WAITER 1u
360#define MIZU_BINDING_FLAGS_ALL MIZU_BINDING_SPLIT_WAITER
361
364
374MIZU_API uint64_t mizu_channel_peer_ident(const mizu_channel *);
375MIZU_API uint64_t mizu_pool_worker_ident(const mizu_pool *);
376
402MIZU_API void *mizu_stage_arena_alloc(mizu_handle *, size_t n, uint64_t *off);
403MIZU_API mizu_status mizu_stage_spill_get(mizu_handle *, size_t n,
404 mizu_shm **out);
405MIZU_API void mizu_stage_retain(mizu_handle *, mizu_shm *region);
406MIZU_API void mizu_stage_retain_zc(mizu_handle *, mizu_shm *region);
407MIZU_API void mizu_stage_pin(mizu_handle *, void *pin);
408MIZU_API void mizu_stage_reap(mizu_handle *);
409MIZU_API void mizu_stage_rollback(mizu_handle *);
410
428MIZU_API void *mizu_stage_raw_spill(mizu_handle *, uint64_t n, int wire_type,
429 mizu_slot_hdr *, uint8_t *payload,
430 uint32_t inline_max);
431#ifdef MIZU_EXT_NO_INLINES
432MIZU_API void *mizu_stage_raw(mizu_handle *, uint64_t n, int wire_type,
433 mizu_slot_hdr *, uint8_t *payload,
434 uint32_t inline_max);
435#else
436MIZU_EXT_INLINE void *mizu_stage_raw(mizu_handle *h, uint64_t n, int wire_type,
437 mizu_slot_hdr *hdr, uint8_t *payload,
438 uint32_t inline_max) {
439 if (n <= (uint64_t) inline_max) {
440 hdr->kind = MIZU_KIND_RAWVEC;
441 hdr->len = (uint32_t) n;
442 hdr->aux = (uint64_t) (uint32_t) wire_type;
443 return payload;
444 }
445 return mizu_stage_raw_spill(h, n, wire_type, hdr, payload, inline_max);
446}
447#endif
448
456MIZU_API mizu_shm *mizu_read_region(mizu_read_ctx *, const uint8_t *name,
457 uint32_t len);
458
478MIZU_API int mizu_result_publish_err(mizu_result_sink *, void *flattened,
479 uint32_t inline_n);
480MIZU_API void mizu_result_publish_lost(mizu_result_sink *);
481
482// The built-in bytes binding -----------------------------------------------------
483
490typedef struct mizu_bytes_s {
491 void *data;
492 size_t len;
494
506MIZU_API void mizu_bytes_free(mizu_bytes *);
507
508// The region handle struct ---------------------------------------------------------
509
516 void *addr;
517 size_t size;
518 char name[MIZU_NAME_MAX];
519 uint8_t name_len;
520 unsigned int pid;
521 uint8_t collapsed;
525#ifdef _WIN32
526 void *handle;
527#endif
528};
529
530// Parker (wait_*.c) ---------------------------------------------------------------
531
540
541typedef struct mizu_parker_s {
542 MIZU_ATOMIC(uint32_t) *epoch;
543#ifdef _WIN32
544 void *event;
545#endif
547
548typedef enum mizu_park_result_e { MIZU_PARK_WOKEN = 0, MIZU_PARK_TIMEOUT,
549 MIZU_PARK_INTR } mizu_park_result;
550
555 const char *region_name, int entity,
556 int create);
557MIZU_API void mizu_parker_detach(mizu_parker *pk);
558
564MIZU_API int mizu_park(mizu_parker *pk, uint32_t snapshot, long timeout_ms);
565MIZU_API void mizu_unpark(mizu_parker *pk);
573
574#ifdef MIZU_EXT_NO_INLINES
575MIZU_API uint32_t mizu_parker_snapshot(const mizu_parker *pk);
576#else
579 return MIZU_EXT_LOAD_ACQ(pk->epoch);
580}
581#endif
582
583// Death watch (wait_linux.c / wait_macos.c / wait_win32.c) ------------------------
584
594
595typedef struct mizu_death_watch_s mizu_death_watch;
596
597MIZU_API mizu_death_watch *mizu_death_watch_start(long pid,
598 MIZU_ATOMIC(int) *flag,
599 const mizu_parker *pk);
600MIZU_API void mizu_death_watch_stop(mizu_death_watch *w);
601
606
607// Notify endpoints (notify.c; the watches ride the wait_*.c listener machinery) ---
608
665 int *avail_out);
666MIZU_API mizu_status mizu_channel_notify_watch(mizu_channel *,
667 void (*cb)(void *), void *ctx);
668MIZU_API mizu_status mizu_channel_notify_unwatch(mizu_channel *);
669
676MIZU_API size_t mizu_channel_avail(mizu_channel *);
677MIZU_API mizu_status mizu_pool_notify_arm(mizu_pool *, int on);
678MIZU_API mizu_status mizu_pool_notify_watch(mizu_pool *,
679 void (*cb)(void *), void *ctx);
680MIZU_API mizu_status mizu_pool_notify_unwatch(mizu_pool *);
681MIZU_API mizu_status mizu_pool_task_notify(mizu_pool *, const mizu_task *,
682 size_t n, int on);
683
684// Batch collect (pool.c) ----------------------------------------------------------
685
701 const mizu_task *, size_t n,
702 void **values_out,
703 size_t *count_out,
704 size_t *fail_out);
705
706// Liveness lock (liveness.c) ---------------------------------------------------
707
714
715typedef enum mizu_live_probe_e { MIZU_LIVE_ACQUIRED = 0,
716 MIZU_LIVE_HELD = 1 } mizu_live_probe;
717
723MIZU_API const char *mizu_live_dir(void);
724
725MIZU_API int mizu_live_open(const char *path, intptr_t *out);
726MIZU_API int mizu_live_try(intptr_t h);
727MIZU_API void mizu_live_close(intptr_t h);
728
729// Preamble (preamble.c) ----------------------------------------------------------
730
735MIZU_API void mizu_preamble_write(void *region, const mizu_preamble *p);
736MIZU_API const char *mizu_preamble_validate(const void *region,
737 size_t region_size,
738 mizu_preamble *out);
739
740// Map support (pool.c; a binding's map rides these) ---------------------------------
741
747typedef struct mizu_pool_sig_s {
748 MIZU_ATOMIC(uint32_t) *help_wanted;
749 MIZU_ATOMIC(uint32_t) *shutdown;
750 MIZU_ATOMIC(int) *owner_dead;
752
761MIZU_API int mizu_pool_deque_pull(mizu_pool *, uint32_t n);
768MIZU_API int mizu_pool_map_caps(mizu_pool *, uint32_t *free_rs,
769 uint32_t *inj_cap, uint32_t *inline_entry);
773MIZU_API mizu_status mizu_pool_submit_flags(mizu_pool *, void *task_obj,
774 uint16_t flags, mizu_task *out,
775 double timeout_ms);
785MIZU_API void mizu_pool_submit_wake(mizu_pool *, size_t done);
786
787// Map morsel protocol (morsel.c) -------------------------------------------------
788
806
807#define MIZU_MORSEL_CANCEL_OFF ((uint64_t) 0)
808#define MIZU_MORSEL_GEN_OFF ((uint64_t) 4)
809#define MIZU_MORSEL_CURSOR_OFF ((uint64_t) 64)
810#define MIZU_MORSEL_CLAIM_OFF ((uint64_t) 128)
813#define MIZU_MORSEL_GEN_MASK ((uint32_t) 0x3FFFFFFF)
814
815enum { MIZU_MORSEL_IDLE = 0, MIZU_MORSEL_RUNNING, MIZU_MORSEL_ABANDONED };
816enum { MIZU_MORSEL_X_DESC = 0, MIZU_MORSEL_X_RAW };
817
822#define MIZU_MORSEL_T_TARGET 200e-6
823#define MIZU_MORSEL_BATCH_CAP 64
824
830#define MIZU_MORSEL_MAGIC 0x4D495A4Du
831
835typedef struct mizu_morsel_hdr_s {
836 uint32_t magic;
837 uint32_t version;
838 uint32_t flags;
839 uint32_t x_kind;
840 uint32_t x_type;
841 uint32_t out_type;
842 uint32_t out_elt;
843 uint32_t claim_n;
844 uint64_t n;
845 uint64_t desc_off, desc_len;
846 uint64_t x_off, x_len;
847 uint64_t out_off;
848 uint64_t out_m;
849 uint64_t morsel_size;
850 uint64_t n_morsels;
851 uint64_t state_off;
852 uint8_t pad[16];
854MIZU_STATIC_ASSERT(sizeof(mizu_morsel_hdr) == 128,
855 "mizu_morsel_hdr is the wire format");
856
865 uint64_t n, uint64_t morsel_size,
866 uint64_t desc_len, uint32_t x_type,
867 uint64_t x_len, uint32_t out_type,
868 uint64_t out_m, uint32_t claim_n);
872MIZU_API const char *mizu_morsel_hdr_check(const void *base, size_t size,
873 mizu_morsel_hdr *out);
874
879typedef struct mizu_morsel_sizer_s {
880 int32_t run_r;
881 uint32_t run_gen;
882 uint64_t k;
883 uint64_t k_last;
884 double t_last;
885 double cost;
886 int skip;
888MIZU_API void mizu_morsel_sizer_init(mizu_morsel_sizer *);
889
902 mizu_morsel_sizer *sz, uint32_t r, uint32_t gen,
903 const mizu_pool_sig *sig, uint64_t pin_k,
904 double now, uint64_t *m_out, uint64_t *k_out,
905 int *help_out);
906
914MIZU_API uint32_t mizu_morsel_reset(void *base, const mizu_morsel_hdr *h);
915
922 uint32_t r, uint32_t gen);
923
928MIZU_API int mizu_morsel_cancel_get(const void *base, const mizu_morsel_hdr *h);
931MIZU_API uint32_t mizu_morsel_generation(const void *base,
932 const mizu_morsel_hdr *h);
933MIZU_API uint64_t mizu_morsel_cursor(const void *base,
934 const mizu_morsel_hdr *h);
935MIZU_API uint32_t mizu_morsel_claim(const void *base, const mizu_morsel_hdr *h,
936 uint32_t r);
937
941 uint64_t k, uint64_t *lo, uint64_t *hi);
942
950typedef struct mizu_morsel_span_s { uint64_t lo, hi; } mizu_morsel_span;
951MIZU_API size_t mizu_morsel_lost(mizu_morsel_span *spans, size_t n,
952 uint64_t bound, mizu_morsel_span *out);
953
954// The result sink and the unwind path (pool.c) ----------------------------------
955
961 mizu_pool *p;
962 mizu_rs_hdr *rs;
963 unsigned char *payload;
964 uint32_t rs_index;
965 uint32_t inline_max;
966 uint16_t sub_slot;
967 uint64_t seq;
969 uint64_t task_id;
970};
971
976MIZU_API void mizu_pool_eval_mark(mizu_pool *, int in_flight);
984
985// Zero-copy refcount words ---------------------------------------------------------
986
992#ifdef MIZU_EXT_NO_INLINES
993MIZU_API MIZU_ATOMIC(uint32_t) *mizu_zc_rc(void *base);
994MIZU_API MIZU_ATOMIC(uint32_t) *mizu_zc_flags_(void *base);
995#else
996MIZU_EXT_INLINE MIZU_ATOMIC(uint32_t) *mizu_zc_rc(void *base) {
997 return (MIZU_ATOMIC(uint32_t) *) ((unsigned char *) base +
999}
1000MIZU_EXT_INLINE MIZU_ATOMIC(uint32_t) *mizu_zc_flags_(void *base) {
1001 return (MIZU_ATOMIC(uint32_t) *) ((unsigned char *) base +
1002 MIZU_ZC_FLAGS_OFF);
1003}
1004#endif
1005
1006// Wire-format helpers --------------------------------------------------------------
1007
1018
1022typedef struct mizu_mizs_geom_s {
1023 int64_t validity;
1024 int64_t offsets;
1025 int64_t encoding;
1026 int64_t data;
1028
1032typedef struct mizu_mizl_entry_s {
1033 int64_t data_offset;
1034 int64_t data_size;
1035 int32_t sexptype;
1036 int32_t attrs_size;
1037 int64_t length;
1038 int64_t valid[2];
1041
1044#ifdef MIZU_EXT_NO_INLINES
1046#else
1049 g.validity = 0;
1050 g.offsets = (int64_t) MIZU_ALIGN64((uint64_t) (n + 7) / 8);
1051 g.encoding = g.offsets + (int64_t) MIZU_ALIGN64(8 * ((uint64_t) n + 1));
1052 g.data = g.encoding + (int64_t) MIZU_ALIGN64((uint64_t) n);
1053 return g;
1054}
1055#endif
1056
1059 uint32_t flags;
1060 memcpy(&flags, (const unsigned char *) base + MIZU_HDR_FLAGS_OFF, 4);
1061 return (flags & ~MIZU_HDR_FLAG_S4) == 0;
1062}
1063
1067MIZU_EXT_INLINE int mizu_ext_valid_ok(int64_t off, int64_t count,
1068 uint64_t n, size_t size) {
1069 if (off == 0) return count == 0 || count == -1;
1070 if (count < 0 || (uint64_t) count > n || (off & 63) != 0) return 0;
1071 const uint64_t bytes = (n + 7) / 8;
1072 return (uint64_t) off <= (uint64_t) size &&
1073 bytes <= (uint64_t) size - (uint64_t) off;
1074}
1075
1080 switch (sexptype & ~(int32_t) MIZU_MIZL_S4) {
1081 case 0:
1082 case MIZU_TYPE_LGL:
1083 case MIZU_TYPE_INT:
1084 case MIZU_TYPE_REAL:
1085 case MIZU_TYPE_CPLX:
1086 case MIZU_TYPE_STR:
1087 case MIZU_TYPE_VEC:
1088 case MIZU_TYPE_RAW:
1089 case MIZU_TYPE_INT64:
1090 case 33: /* remote leaf */
1091 return 1;
1092 default:
1093 return 0;
1094 }
1095}
1096
1105 return ((bits >> 52) & 0x7FF) == 0x7FF && (uint32_t) bits == 0x7A2u;
1106}
1107MIZU_EXT_INLINE int mizu_ext_na_at(int type, const void *src, uint64_t i) {
1108 const unsigned char *p = (const unsigned char *) src;
1109 uint64_t bits, re, im;
1110 int32_t v32;
1111 int64_t v64;
1112 switch (type) {
1113 case MIZU_TYPE_LGL:
1114 case MIZU_TYPE_INT:
1115 memcpy(&v32, p + 4 * i, 4);
1116 return v32 == MIZU_NA_INT32;
1117 case MIZU_TYPE_REAL:
1118 memcpy(&bits, p + 8 * i, 8);
1119 return mizu_ext_na_real_bits(bits);
1120 case MIZU_TYPE_CPLX:
1121 memcpy(&re, p + 16 * i, 8);
1122 memcpy(&im, p + 16 * i + 8, 8);
1124 case MIZU_TYPE_INT64:
1125 memcpy(&v64, p + 8 * i, 8);
1126 return v64 == MIZU_NA_INT64;
1127 default:
1128 return 0;
1129 }
1130}
1131
1134MIZU_EXT_INLINE void mizu_ext_na_store(int type, void *dst, uint64_t i) {
1135 unsigned char *p = (unsigned char *) dst;
1136 const int32_t na32 = MIZU_NA_INT32;
1137 const int64_t na64 = MIZU_NA_INT64;
1138 const uint64_t nabits = MIZU_NA_REAL_BITS;
1139 switch (type) {
1140 case MIZU_TYPE_LGL:
1141 case MIZU_TYPE_INT:
1142 memcpy(p + 4 * i, &na32, 4);
1143 break;
1144 case MIZU_TYPE_REAL:
1145 memcpy(p + 8 * i, &nabits, 8);
1146 break;
1147 case MIZU_TYPE_CPLX:
1148 memcpy(p + 16 * i, &nabits, 8);
1149 memcpy(p + 16 * i + 8, &nabits, 8);
1150 break;
1151 case MIZU_TYPE_INT64:
1152 memcpy(p + 8 * i, &na64, 8);
1153 break;
1154 default:
1155 break;
1156 }
1157}
1158
1167MIZU_EXT_INLINE int mizu_ext_mizl_ent(const void *base, size_t size,
1168 int64_t i, mizu_mizl_entry *e) {
1169 const unsigned char *dir = (const unsigned char *) base +
1170 MIZU_HEADER_SIZE + 32 * (size_t) i;
1171 memcpy(&e->data_offset, dir, 8);
1172 memcpy(&e->data_size, dir + 8, 8);
1173 memcpy(&e->sexptype, dir + 16, 4);
1174 memcpy(&e->attrs_size, dir + 20, 4);
1175 memcpy(&e->length, dir + 24, 8);
1176 if (e->data_offset < 0 || (e->data_offset & 63) != 0 ||
1177 e->data_size < 0 ||
1178 (uint64_t) e->data_offset > (uint64_t) size ||
1179 (uint64_t) e->data_size >
1180 (uint64_t) size - (uint64_t) e->data_offset ||
1181 e->attrs_size < 0)
1182 return -1;
1183 if (!mizu_ext_mizl_tag_ok(e->sexptype)) return -1;
1184 const int32_t tag = e->sexptype & ~(int32_t) MIZU_MIZL_S4;
1185 if (tag == 33) { /* remote leaf: the span is the identifier */
1186 if ((e->sexptype & (int32_t) MIZU_MIZL_S4) != 0 || e->length < 0 ||
1187 e->data_size < 1 || e->data_size > 255)
1188 return -1;
1189 return 0;
1190 }
1191 if ((int64_t) e->attrs_size > e->data_size) return -1;
1192 const int64_t body = e->data_size - (int64_t) e->attrs_size;
1193 const size_t elt = mizu_type_elt_size(tag);
1194 if (elt != 0) {
1195 if (e->length < 0 || e->length > body / (int64_t) elt) return -1;
1196 } else if (tag == MIZU_TYPE_STR) {
1197 if (e->length < 0 || e->length > ((int64_t) 1 << 50) ||
1198 mizu_mizs_geometry(e->length).data > body)
1199 return -1;
1200 } else if (tag == MIZU_TYPE_VEC) {
1201 if (e->length < 0) return -1;
1202 }
1203 return 0;
1204}
1205
1209
1210MIZU_EXT_INLINE int mizu_ext_mizh_check_impl(const void *base, size_t size,
1211 int *wire_type, int64_t *n_elems,
1212 int64_t valid[2]) {
1213 if (size < MIZU_HEADER_SIZE) return -1;
1214 uint32_t magic;
1215 int32_t t32;
1216 int64_t len, attrs, voff, vcount;
1217 memcpy(&magic, base, 4);
1218 if (magic != MIZU_MAGIC_VEC) return -1;
1219 memcpy(&t32, (const unsigned char *) base + 4, 4);
1220 memcpy(&len, (const unsigned char *) base + 8, 8);
1221 memcpy(&attrs, (const unsigned char *) base + 16, 8);
1222 memcpy(&voff, (const unsigned char *) base + MIZU_HDR_VALID_OFF, 8);
1223 memcpy(&vcount, (const unsigned char *) base + MIZU_HDR_VALID_COUNT, 8);
1224 const size_t elt = mizu_type_elt_size(t32);
1225 if (elt == 0 || len < 0 || attrs < 0 ||
1226 len > ((int64_t) size - (int64_t) MIZU_HEADER_SIZE) / (int64_t) elt ||
1227 attrs > (int64_t) size - (int64_t) MIZU_HEADER_SIZE -
1228 len * (int64_t) elt)
1229 return -1;
1230 if (!mizu_ext_flags_known(base) ||
1231 !mizu_ext_valid_ok(voff, vcount, (uint64_t) len, size))
1232 return -1;
1233 *wire_type = t32;
1234 *n_elems = len;
1235 valid[0] = voff;
1236 valid[1] = vcount;
1237 return 0;
1238}
1239
1240MIZU_EXT_INLINE int mizu_ext_mizs_check_impl(const void *base, size_t size,
1241 int64_t *n, int64_t *str_size,
1242 int64_t *attrs_size) {
1243 if (size < MIZU_HEADER_SIZE) return -1;
1244 uint32_t magic;
1245 int32_t attrs;
1246 int64_t count, block;
1247 memcpy(&magic, base, 4);
1248 if (magic != MIZU_MAGIC_STR) return -1;
1249 memcpy(&attrs, (const unsigned char *) base + 4, 4);
1250 memcpy(&count, (const unsigned char *) base + 8, 8);
1251 memcpy(&block, (const unsigned char *) base + 16, 8);
1252 if (attrs < 0 || count < 0 || count > ((int64_t) 1 << 50) || block < 0)
1253 return -1;
1254 if (!mizu_ext_flags_known(base)) return -1;
1255 const int64_t fixed = mizu_mizs_geometry(count).data;
1256 if (fixed > block ||
1257 block > (int64_t) size - (int64_t) MIZU_HEADER_SIZE ||
1258 (int64_t) attrs >
1259 (int64_t) size - (int64_t) MIZU_HEADER_SIZE - block)
1260 return -1;
1261 *n = count;
1262 *str_size = block;
1263 *attrs_size = attrs;
1264 return 0;
1265}
1266
1267MIZU_EXT_INLINE int mizu_ext_mizl_check_impl(const void *base, size_t size,
1268 int64_t *n, int64_t *attrs_off,
1269 int64_t *attrs_size,
1270 int64_t valid[2]) {
1271 if (size < MIZU_HEADER_SIZE) return -1;
1272 uint32_t magic;
1273 int32_t n32;
1274 int64_t aoff, asz, voff, vcount;
1275 memcpy(&magic, base, 4);
1276 if (magic != MIZU_MAGIC_LIST) return -1;
1277 memcpy(&n32, (const unsigned char *) base + 4, 4);
1278 memcpy(&aoff, (const unsigned char *) base + 8, 8);
1279 memcpy(&asz, (const unsigned char *) base + 16, 8);
1280 memcpy(&voff, (const unsigned char *) base + MIZU_HDR_VALID_OFF, 8);
1281 memcpy(&vcount, (const unsigned char *) base + MIZU_HDR_VALID_COUNT, 8);
1282 if (n32 < 0 ||
1283 (uint64_t) (uint32_t) n32 >
1284 ((uint64_t) size - (uint64_t) MIZU_HEADER_SIZE) / 32)
1285 return -1;
1286 if (aoff < 0 || asz < 0 ||
1287 (uint64_t) aoff > (uint64_t) size ||
1288 (uint64_t) asz > (uint64_t) size - (uint64_t) aoff)
1289 return -1;
1290 if (!mizu_ext_flags_known(base)) return -1;
1291 uint64_t sum = 0;
1292 for (int64_t i = 0; i < (int64_t) n32; i++) {
1294 if (mizu_ext_mizl_ent(base, size, i, &e) != 0) return -1;
1295 const int32_t tag = e.sexptype & ~(int32_t) MIZU_MIZL_S4;
1296 /* vcount is an int64, so the bound needs the sum only up to
1297 INT64_MAX: saturate. Overlapping crafted leaves in a huge region
1298 can otherwise wrap the accumulation and over-reject. */
1299 if (mizu_type_elt_size(tag) != 0 && sum < (uint64_t) INT64_MAX) {
1300 const uint64_t len = (uint64_t) e.length;
1301 sum = len > (uint64_t) INT64_MAX - sum ? (uint64_t) INT64_MAX
1302 : sum + len;
1303 }
1304 }
1305 if (voff == 0) {
1306 if (vcount != 0 && vcount != -1) return -1;
1307 } else {
1308 const uint64_t bytes = (uint64_t) (uint32_t) n32 * 16;
1309 if (vcount < 0 || (uint64_t) vcount > sum || (voff & 63) != 0 ||
1310 (uint64_t) voff > (uint64_t) size ||
1311 bytes > (uint64_t) size - (uint64_t) voff)
1312 return -1;
1313 }
1314 *n = n32;
1315 *attrs_off = aoff;
1316 *attrs_size = asz;
1317 valid[0] = voff;
1318 valid[1] = vcount;
1319 return 0;
1320}
1321
1322MIZU_EXT_INLINE int mizu_ext_mizl_elem_impl(const void *base, size_t size,
1323 int64_t i,
1324 mizu_mizl_entry *elem) {
1325 if (size < MIZU_HEADER_SIZE) return -1;
1326 uint32_t magic;
1327 int32_t n32;
1328 int64_t voff, vcount;
1329 memcpy(&magic, base, 4);
1330 if (magic != MIZU_MAGIC_LIST) return -1;
1331 memcpy(&n32, (const unsigned char *) base + 4, 4);
1332 if (n32 < 0 || i < 0 || i >= (int64_t) n32 ||
1333 (uint64_t) (uint32_t) n32 >
1334 ((uint64_t) size - (uint64_t) MIZU_HEADER_SIZE) / 32)
1335 return -1;
1336 if (!mizu_ext_flags_known(base)) return -1;
1337 if (mizu_ext_mizl_ent(base, size, i, elem) != 0) return -1;
1338 memcpy(&voff, (const unsigned char *) base + MIZU_HDR_VALID_OFF, 8);
1339 memcpy(&vcount, (const unsigned char *) base + MIZU_HDR_VALID_COUNT, 8);
1340 if (voff == 0) {
1341 if (vcount != 0 && vcount != -1) return -1;
1342 elem->valid[0] = 0;
1343 elem->valid[1] = vcount;
1344 return 0;
1345 }
1346 if ((voff & 63) != 0) return -1;
1347 const uint64_t bytes = (uint64_t) (uint32_t) n32 * 16;
1348 if ((uint64_t) voff > (uint64_t) size ||
1349 bytes > (uint64_t) size - (uint64_t) voff)
1350 return -1;
1351 const unsigned char *tab = (const unsigned char *) base + voff;
1352 int64_t loff, lcount;
1353 memcpy(&loff, tab + 16 * (size_t) i, 8);
1354 memcpy(&lcount, tab + 16 * (size_t) i + 8, 8);
1355 /* a remote leaf (tag 33) carries a {0,0} / {0,-1} claim alone: a
1356 bitmap offset is region-local and cannot describe a remote column.
1357 STR / VEC / serialized leaves carry no table bitmap either: the
1358 string block, the nested header, and the blob each speak for
1359 themselves, and the writers stamp those rows {0,0} */
1360 const int32_t ltag = elem->sexptype & ~(int32_t) MIZU_MIZL_S4;
1361 if ((ltag == 33 || mizu_type_elt_size(ltag) == 0) && loff != 0)
1362 return -1;
1363 if (!mizu_ext_valid_ok(loff, lcount, (uint64_t) elem->length, size))
1364 return -1;
1365 elem->valid[0] = loff;
1366 elem->valid[1] = lcount;
1367 return 0;
1368}
1369
1370MIZU_EXT_INLINE uint64_t mizu_ext_na_build_impl(int type, uint8_t *bitmap,
1371 const void *src, uint64_t n,
1372 uint64_t bit_off) {
1373 uint64_t nulls = 0;
1374 for (uint64_t i = 0; i < n; i++) {
1375 const uint64_t bit = bit_off + i;
1376 if (mizu_ext_na_at(type, src, i)) {
1377 bitmap[bit >> 3] &= (uint8_t) ~(1u << (bit & 7));
1378 nulls++;
1379 } else {
1380 bitmap[bit >> 3] |= (uint8_t) (1u << (bit & 7));
1381 }
1382 }
1383 return nulls;
1384}
1385
1386MIZU_EXT_INLINE uint64_t mizu_ext_na_apply_impl(int type, void *dst,
1387 const void *src,
1388 const uint8_t *bitmap,
1389 uint64_t n) {
1390 const size_t elt = mizu_type_elt_size(type);
1391 if (elt == 0) return 0;
1392 if (dst != src) memcpy(dst, src, (size_t) n * elt);
1393 uint64_t nulls = 0;
1394 for (uint64_t i = 0; i < n; i++) {
1395 if (((bitmap[i >> 3] >> (i & 7)) & 1) == 0) {
1396 mizu_ext_na_store(type, dst, i);
1397 nulls++;
1398 }
1399 }
1400 return nulls;
1401}
1402
1403/* The one definition behind mizu_timeout_ms's two forms (documented
1404 there): the exported symbol and the inline cannot drift apart. */
1405MIZU_EXT_INLINE double mizu_ext_timeout_ms_impl(double seconds) {
1406 if (!isfinite(seconds)) return -1;
1407 if (seconds <= 0) return 0;
1408 const double ms = seconds * 1000;
1409 return isfinite(ms) ? ms : -1;
1410}
1411
1412#ifdef MIZU_EXT_NO_INLINES
1413MIZU_API double mizu_timeout_ms(double seconds);
1414MIZU_API void mizu_store_na_real(void *dst);
1415MIZU_API uint64_t mizu_aux_rawspill_pool(int type, uint32_t name_len);
1416MIZU_API uint64_t mizu_aux_shm_vec(int type, uint64_t total);
1417MIZU_API int mizu_aux_type(uint64_t aux);
1418MIZU_API uint64_t mizu_aux_hi(uint64_t aux);
1419MIZU_API void mizu_mizh_write(void *base, int wire_type, int64_t n_elems);
1420MIZU_API int mizu_mizh_check(const void *base, size_t size,
1421 int *wire_type, int64_t *n_elems, int64_t valid[2]);
1422MIZU_API void mizu_mizh_validity_set(void *base, int64_t off,
1423 int64_t count);
1424MIZU_API int mizu_mizs_check(const void *base, size_t size, int64_t *n,
1425 int64_t *str_size, int64_t *attrs_size);
1426MIZU_API int mizu_mizl_check(const void *base, size_t size, int64_t *n,
1427 int64_t *attrs_off, int64_t *attrs_size,
1428 int64_t valid[2]);
1429MIZU_API int mizu_mizl_elem(const void *base, size_t size, int64_t i,
1430 mizu_mizl_entry *elem);
1431MIZU_API uint64_t mizu_na_build(int type, uint8_t *bitmap, const void *src,
1432 uint64_t n, uint64_t bit_off);
1433MIZU_API uint64_t mizu_na_apply(int type, void *dst, const void *src,
1434 const uint8_t *bitmap, uint64_t n);
1435#else
1441MIZU_EXT_INLINE double mizu_timeout_ms(double seconds) {
1442 return mizu_ext_timeout_ms_impl(seconds);
1443}
1444MIZU_EXT_INLINE void mizu_store_na_real(void *dst) {
1445 const uint64_t bits = MIZU_NA_REAL_BITS;
1446 memcpy(dst, &bits, 8);
1447}
1449MIZU_EXT_INLINE uint64_t mizu_aux_rawspill_pool(int type, uint32_t name_len) {
1450 return (uint64_t) (uint32_t) type | ((uint64_t) name_len << 8);
1451}
1452
1453MIZU_EXT_INLINE uint64_t mizu_aux_shm_vec(int type, uint64_t total) {
1454 return (uint64_t) (uint32_t) type | (total << 8);
1455}
1456
1460 return (int) (aux & 0xff);
1461}
1462MIZU_EXT_INLINE uint64_t mizu_aux_hi(uint64_t aux) {
1463 return aux >> 8;
1464}
1469MIZU_EXT_INLINE void mizu_mizh_write(void *base, int wire_type,
1470 int64_t n_elems) {
1471 const uint32_t magic = MIZU_MAGIC_VEC;
1472 const int32_t t32 = wire_type;
1473 const int64_t zero64 = 0;
1474 memcpy(base, &magic, 4);
1475 memcpy((unsigned char *) base + 4, &t32, 4);
1476 memcpy((unsigned char *) base + 8, &n_elems, 8);
1477 memcpy((unsigned char *) base + 16, &zero64, 8);
1478 memset((unsigned char *) base + MIZU_ZC_REFCOUNT_OFF, 0,
1479 MIZU_HEADER_SIZE - MIZU_ZC_REFCOUNT_OFF);
1480}
1481
1487MIZU_EXT_INLINE int mizu_mizh_check(const void *base, size_t size,
1488 int *wire_type, int64_t *n_elems,
1489 int64_t valid[2]) {
1490 return mizu_ext_mizh_check_impl(base, size, wire_type, n_elems, valid);
1491}
1492
1495MIZU_EXT_INLINE void mizu_mizh_validity_set(void *base, int64_t off,
1496 int64_t count) {
1497 memcpy((unsigned char *) base + MIZU_HDR_VALID_OFF, &off, 8);
1498 memcpy((unsigned char *) base + MIZU_HDR_VALID_COUNT, &count, 8);
1499}
1500
1505MIZU_EXT_INLINE int mizu_mizs_check(const void *base, size_t size,
1506 int64_t *n, int64_t *str_size,
1507 int64_t *attrs_size) {
1508 return mizu_ext_mizs_check_impl(base, size, n, str_size, attrs_size);
1509}
1510
1515MIZU_EXT_INLINE int mizu_mizl_check(const void *base, size_t size,
1516 int64_t *n, int64_t *attrs_off,
1517 int64_t *attrs_size, int64_t valid[2]) {
1518 return mizu_ext_mizl_check_impl(base, size, n, attrs_off, attrs_size,
1519 valid);
1520}
1521
1525MIZU_EXT_INLINE int mizu_mizl_elem(const void *base, size_t size, int64_t i,
1526 mizu_mizl_entry *elem) {
1527 return mizu_ext_mizl_elem_impl(base, size, i, elem);
1528}
1529
1533MIZU_EXT_INLINE uint64_t mizu_na_build(int type, uint8_t *bitmap,
1534 const void *src, uint64_t n,
1535 uint64_t bit_off) {
1536 return mizu_ext_na_build_impl(type, bitmap, src, n, bit_off);
1537}
1538
1541MIZU_EXT_INLINE uint64_t mizu_na_apply(int type, void *dst, const void *src,
1542 const uint8_t *bitmap, uint64_t n) {
1543 return mizu_ext_na_apply_impl(type, dst, src, bitmap, n);
1544}
1545#endif
1546
1547// The interchange stream ('I') ---------------------------------------------------
1548
1561
1562#define MIZU_IX_VERSION 0x01u
1563#define MIZU_IX_DEPTH_MAX 64
1564
1566enum {
1567 MIZU_IX_TAG_NIL = 0x00,
1568 MIZU_IX_TAG_LGL1 = 0x01,
1569 MIZU_IX_TAG_INT = 0x02,
1570 MIZU_IX_TAG_REAL = 0x03,
1571 MIZU_IX_TAG_STR = 0x04,
1572 MIZU_IX_TAG_BYTES = 0x05,
1573 MIZU_IX_TAG_LGLV = 0x06,
1574 MIZU_IX_TAG_INTV = 0x07,
1575 MIZU_IX_TAG_REALV = 0x08,
1576 MIZU_IX_TAG_CPLXV = 0x09,
1577 MIZU_IX_TAG_RAWV = 0x0a,
1578 MIZU_IX_TAG_STRV = 0x0b,
1579 MIZU_IX_TAG_LIST = 0x0c,
1580 MIZU_IX_TAG_DICT = 0x0d,
1581 MIZU_IX_TAG_I64V = 0x0e,
1582 MIZU_IX_TAG_ATTR = 0x0f,
1583 MIZU_IX_TAG_CPLX = 0x10,
1584 MIZU_IX_TAG_ERR = 0x11,
1585 MIZU_IX_TAG_TASK = 0x12,
1586 MIZU_IX_TAG_REF = 0x13
1587};
1588
1599enum {
1600 MIZU_IX_NIL = 0,
1616};
1617
1618typedef struct mizu_ix_item_s {
1619 uint32_t kind;
1620 uint32_t type;
1621 int na;
1622 int key;
1623 const unsigned char *ptr;
1624 uint64_t len;
1625 uint64_t count;
1627 uint64_t u64[2];
1629 uint32_t target;
1630 uint32_t task_kind;
1631 uint32_t err_flags;
1632 uint64_t err_index;
1633 struct { const unsigned char *ptr; uint64_t len; } err_str[3];
1635} mizu_ix_item;
1636
1642typedef struct mizu_ix_s {
1643 const unsigned char *p;
1644 const unsigned char *end;
1645 uint32_t depth;
1646 int done;
1647 int err;
1648 struct {
1649 uint32_t kind;
1650 uint64_t remaining;
1651 } stack[MIZU_IX_DEPTH_MAX + 1];
1653
1659MIZU_API mizu_status mizu_ix_open(mizu_ix *cur, const void *buf, size_t len);
1664MIZU_API mizu_status mizu_ix_next(mizu_ix *cur, mizu_ix_item *item);
1674MIZU_API mizu_status mizu_ix_skip(mizu_ix *cur, const unsigned char **start,
1675 size_t *len);
1676
1688 size_t len, mizu_ix_item *item,
1689 int max_kind);
1696
1701#define MIZU_IX_ERR_OVERHEAD 25u
1702#define MIZU_IX_ERR_TYPE_SHARE 128u
1703
1709#define MIZU_IX_ATTR_NAMES "names"
1710#define MIZU_IX_ATTR_LEVELS "levels"
1711#define MIZU_IX_ATTR_DIM "dim"
1712#define MIZU_IX_ATTR_DIMNAMES "dimnames"
1713#define MIZU_IX_ATTR_CLASS "class"
1714#define MIZU_IX_ATTR_UNITS "units"
1715#define MIZU_IX_ATTR_ROWNAMES "row.names"
1716#define MIZU_IX_ATTR_TZONE "tzone"
1717#define MIZU_IX_ATTR_TSCALE "tscale"
1718#define MIZU_IX_ATTR_U64 "u64"
1719#define MIZU_IX_ATTR_TSP "tsp"
1720
1721#define MIZU_IX_CLASS_FACTOR "factor"
1722#define MIZU_IX_CLASS_ORDERED "ordered"
1723#define MIZU_IX_CLASS_DATE "Date"
1724#define MIZU_IX_CLASS_POSIXCT "POSIXct"
1725#define MIZU_IX_CLASS_POSIXT "POSIXt"
1726#define MIZU_IX_CLASS_DIFFTIME "difftime"
1727#define MIZU_IX_CLASS_DATAFRAME "data.frame"
1728#define MIZU_IX_CLASS_TABLE "table"
1729
1730#define MIZU_IX_UNIT_SECS "secs"
1731#define MIZU_IX_UNIT_MINS "mins"
1732#define MIZU_IX_UNIT_HOURS "hours"
1733#define MIZU_IX_UNIT_DAYS "days"
1734#define MIZU_IX_UNIT_WEEKS "weeks"
1735
1738
1739MIZU_EXT_INLINE int mizu_ext_ix_utf8_valid_impl(const void *s, size_t n) {
1742 const unsigned char *b = (const unsigned char *) s;
1743 size_t i = 0;
1744 while (i < n) {
1745 const unsigned char c = b[i];
1746 if (c < 0x80) {
1747 i++;
1748 } else if (c < 0xC2 || c >= 0xF5) { /* stray cont., overlong, or
1749 past U+10FFFF */
1750 return 0;
1751 } else if (c < 0xE0) { /* 2 bytes */
1752 if (i + 2 > n || (b[i + 1] & 0xC0) != 0x80) return 0;
1753 i += 2;
1754 } else if (c < 0xF0) { /* 3 bytes */
1755 if (i + 3 > n || (b[i + 1] & 0xC0) != 0x80 ||
1756 (b[i + 2] & 0xC0) != 0x80) return 0;
1757 if (c == 0xE0 && b[i + 1] < 0xA0) return 0; /* overlong */
1758 if (c == 0xED && b[i + 1] >= 0xA0) return 0; /* surrogate */
1759 i += 3;
1760 } else { /* 4 bytes: 0xF0..0xF4 */
1761 if (i + 4 > n || (b[i + 1] & 0xC0) != 0x80 ||
1762 (b[i + 2] & 0xC0) != 0x80 || (b[i + 3] & 0xC0) != 0x80)
1763 return 0;
1764 if (c == 0xF0 && b[i + 1] < 0x90) return 0; /* overlong */
1765 if (c == 0xF4 && b[i + 1] > 0x8F) return 0; /* > U+10FFFF */
1766 i += 4;
1767 }
1768 }
1769 return 1;
1770}
1771
1775 switch (wire_type) {
1776 case MIZU_TYPE_LGL: return MIZU_IX_TAG_LGLV;
1777 case MIZU_TYPE_INT: return MIZU_IX_TAG_INTV;
1778 case MIZU_TYPE_REAL: return MIZU_IX_TAG_REALV;
1779 case MIZU_TYPE_CPLX: return MIZU_IX_TAG_CPLXV;
1780 case MIZU_TYPE_RAW: return MIZU_IX_TAG_RAWV;
1781 case MIZU_TYPE_INT64: return MIZU_IX_TAG_I64V;
1782 default: return MIZU_IX_TAG_NIL;
1783 }
1784}
1785
1786MIZU_EXT_INLINE int mizu_ext_ix_attr_reserved_impl(const char *key,
1787 size_t len) {
1788 static const char *const reserved[] = {
1789 MIZU_IX_ATTR_NAMES, MIZU_IX_ATTR_LEVELS, MIZU_IX_ATTR_DIM,
1790 MIZU_IX_ATTR_DIMNAMES, MIZU_IX_ATTR_CLASS, MIZU_IX_ATTR_UNITS,
1791 MIZU_IX_ATTR_ROWNAMES, MIZU_IX_ATTR_TZONE, MIZU_IX_ATTR_TSCALE,
1792 MIZU_IX_ATTR_U64, MIZU_IX_ATTR_TSP
1793 };
1794 for (size_t i = 0; i < sizeof reserved / sizeof reserved[0]; i++)
1795 if (strlen(reserved[i]) == len && memcmp(key, reserved[i], len) == 0)
1796 return 1;
1797 return 0;
1798}
1799
1803
1805 if (dst != NULL) {
1806 dst[0] = (unsigned char) MIZU_INTEROP_MAGIC;
1807 dst[1] = (unsigned char) MIZU_IX_VERSION;
1808 }
1809 return 2;
1810}
1811
1813 uint32_t tag, uint64_t v) {
1815 if (dst != NULL) {
1816 dst[0] = (unsigned char) tag;
1817 memcpy(dst + 1, &v, 8);
1818 }
1819 return 9;
1820}
1821
1822MIZU_EXT_INLINE size_t mizu_ext_ix_put_nil_impl(unsigned char *dst) {
1823 if (dst != NULL) dst[0] = MIZU_IX_TAG_NIL;
1824 return 1;
1825}
1826
1827MIZU_EXT_INLINE size_t mizu_ext_ix_put_lgl_impl(unsigned char *dst,
1828 int value) {
1829 if (dst != NULL) {
1830 dst[0] = MIZU_IX_TAG_LGL1;
1831 dst[1] = (unsigned char) value;
1832 }
1833 return 2;
1834}
1835
1836MIZU_EXT_INLINE size_t mizu_ext_ix_put_int_impl(unsigned char *dst,
1837 int64_t value) {
1838 if (dst != NULL) {
1839 dst[0] = MIZU_IX_TAG_INT;
1840 memcpy(dst + 1, &value, 8);
1841 }
1842 return 9;
1843}
1844
1845MIZU_EXT_INLINE size_t mizu_ext_ix_put_real_impl(unsigned char *dst,
1846 double value) {
1847 if (dst != NULL) {
1848 dst[0] = MIZU_IX_TAG_REAL;
1849 memcpy(dst + 1, &value, 8);
1850 }
1851 return 9;
1852}
1853
1854MIZU_EXT_INLINE size_t mizu_ext_ix_put_cplx_impl(unsigned char *dst,
1855 double re, double im) {
1856 if (dst != NULL) {
1857 dst[0] = MIZU_IX_TAG_CPLX;
1858 memcpy(dst + 1, &re, 8);
1859 memcpy(dst + 9, &im, 8);
1860 }
1861 return 17;
1862}
1863
1864MIZU_EXT_INLINE size_t mizu_ext_ix_put_str_impl(unsigned char *dst,
1865 const void *s, int32_t len) {
1866 if (dst != NULL) {
1867 dst[0] = MIZU_IX_TAG_STR;
1868 memcpy(dst + 1, &len, 4);
1869 if (len > 0) memcpy(dst + 5, s, (size_t) len);
1870 }
1871 return (size_t) 5 + (len > 0 ? (size_t) len : 0);
1872}
1873
1874MIZU_EXT_INLINE size_t mizu_ext_ix_put_bytes_impl(unsigned char *dst,
1875 const void *data,
1876 uint64_t count) {
1877 if (dst != NULL) {
1878 dst[0] = MIZU_IX_TAG_BYTES;
1879 memcpy(dst + 1, &count, 8);
1880 if (count != 0) memcpy(dst + 9, data, (size_t) count);
1881 }
1882 return (size_t) 9 + (size_t) count;
1883}
1884
1885MIZU_EXT_INLINE size_t mizu_ext_ix_put_vec_impl(unsigned char *dst,
1886 int wire_type,
1887 const void *data,
1888 uint64_t count) {
1889 const int tag = mizu_ext_ix_tag_of_impl(wire_type);
1890 if (tag == MIZU_IX_TAG_NIL) return 0;
1891 const size_t elt = mizu_type_elt_size(wire_type);
1892 if (dst != NULL) {
1893 dst[0] = (unsigned char) tag;
1894 memcpy(dst + 1, &count, 8);
1895 if (count != 0) memcpy(dst + 9, data, (size_t) count * elt);
1896 }
1897 return (size_t) 9 + (size_t) count * elt;
1898}
1899
1900MIZU_EXT_INLINE size_t mizu_ext_ix_put_strv_begin_impl(unsigned char *dst,
1901 uint64_t count) {
1902 return mizu_ext_ix_put_tag8_impl(dst, MIZU_IX_TAG_STRV, count);
1903}
1904
1905MIZU_EXT_INLINE size_t mizu_ext_ix_put_strelt_impl(unsigned char *dst,
1906 const void *s,
1907 int32_t len) {
1908 if (dst != NULL) {
1909 memcpy(dst, &len, 4);
1910 if (len > 0) memcpy(dst + 4, s, (size_t) len);
1911 }
1912 return (size_t) 4 + (len > 0 ? (size_t) len : 0);
1913}
1914
1915MIZU_EXT_INLINE size_t mizu_ext_ix_put_list_begin_impl(unsigned char *dst,
1916 uint64_t count) {
1917 return mizu_ext_ix_put_tag8_impl(dst, MIZU_IX_TAG_LIST, count);
1918}
1919
1920MIZU_EXT_INLINE size_t mizu_ext_ix_put_dict_begin_impl(unsigned char *dst,
1921 uint64_t count) {
1922 return mizu_ext_ix_put_tag8_impl(dst, MIZU_IX_TAG_DICT, count);
1923}
1924
1925MIZU_EXT_INLINE size_t mizu_ext_ix_put_key_impl(unsigned char *dst,
1926 const void *s, uint32_t len) {
1927 if (dst != NULL) {
1928 memcpy(dst, &len, 4);
1929 if (len != 0) memcpy(dst + 4, s, (size_t) len);
1930 }
1931 return (size_t) 4 + (size_t) len;
1932}
1933
1934MIZU_EXT_INLINE size_t mizu_ext_ix_put_attr_impl(unsigned char *dst) {
1935 if (dst != NULL) dst[0] = MIZU_IX_TAG_ATTR;
1936 return 1;
1937}
1938
1939MIZU_EXT_INLINE size_t mizu_ext_ix_put_err_impl(unsigned char *dst,
1940 int has_index, uint64_t index,
1941 const void *type,
1942 uint32_t type_len,
1943 const void *message,
1944 uint32_t message_len,
1945 const void *detail,
1946 uint32_t detail_len) {
1947 if (dst != NULL) {
1948 const uint16_t flags = (uint16_t) (has_index != 0);
1949 dst[0] = MIZU_IX_TAG_ERR;
1950 memcpy(dst + 1, &flags, 2);
1951 size_t off = 3;
1952 if (has_index) {
1953 memcpy(dst + off, &index, 8);
1954 off += 8;
1955 }
1956 const void *strs[3] = { type, message, detail };
1957 const uint32_t lens[3] = { type_len, message_len, detail_len };
1958 for (int i = 0; i < 3; i++) {
1959 memcpy(dst + off, &lens[i], 4);
1960 off += 4;
1961 if (lens[i] != 0) {
1962 memcpy(dst + off, strs[i], (size_t) lens[i]);
1963 off += (size_t) lens[i];
1964 }
1965 }
1966 return off;
1967 }
1968 return (size_t) 3 + (has_index ? 8 : 0) +
1969 (size_t) 12 + (size_t) type_len + (size_t) message_len +
1970 (size_t) detail_len;
1971}
1972
1973MIZU_EXT_INLINE size_t mizu_ext_ix_put_task_impl(unsigned char *dst,
1974 int target, int kind,
1975 uint64_t ident) {
1976 if (dst != NULL) {
1977 const uint16_t zero = 0;
1978 dst[0] = MIZU_IX_TAG_TASK;
1979 dst[1] = (unsigned char) target;
1980 dst[2] = (unsigned char) kind;
1981 memcpy(dst + 3, &zero, 2);
1982 memcpy(dst + 5, &ident, 8);
1983 }
1984 return 13;
1985}
1986
1988 const void *name,
1989 uint32_t name_len) {
1992 if (name_len == 0 || name_len > 255) return 0;
1993 if (dst != NULL) {
1994 dst[0] = MIZU_IX_TAG_REF;
1995 dst[1] = (unsigned char) name_len;
1996 memcpy(dst + 2, name, (size_t) name_len);
1997 }
1998 return (size_t) 2 + name_len;
1999}
2000
2001MIZU_EXT_INLINE size_t mizu_ext_ix_err_floor_impl(const void *s, size_t n,
2002 size_t share) {
2005 if (n <= share) return n;
2006 const unsigned char *b = (const unsigned char *) s;
2007 size_t len = share;
2008 while (len > 0 && (b[len] & 0xC0) == 0x80) len--;
2009 return len;
2010}
2011
2013 unsigned char *dst, uint32_t inline_max,
2014 const void *type, size_t type_n,
2015 const void *msg, size_t msg_n,
2016 const void *detail, size_t detail_n,
2017 int has_index, uint64_t index) {
2023 const size_t budget = inline_max;
2024 const size_t avail =
2025 budget > MIZU_IX_ERR_OVERHEAD ? budget - MIZU_IX_ERR_OVERHEAD : 0;
2026 size_t cap =
2027 avail < MIZU_IX_ERR_TYPE_SHARE ? avail : MIZU_IX_ERR_TYPE_SHARE;
2028 const size_t tn = mizu_ext_ix_err_floor_impl(type, type_n, cap);
2029 cap = budget / 2;
2030 if (cap > avail - tn) cap = avail - tn;
2031 const size_t mn = mizu_ext_ix_err_floor_impl(msg, msg_n, cap);
2032 const size_t dn =
2033 mizu_ext_ix_err_floor_impl(detail, detail_n, avail - tn - mn);
2034 size_t n = mizu_ext_ix_put_header_impl(dst);
2035 n += mizu_ext_ix_put_err_impl(dst != NULL ? dst + n : NULL, has_index,
2036 index, type, (uint32_t) tn, msg,
2037 (uint32_t) mn, detail, (uint32_t) dn);
2038 return n;
2039}
2040
2041#ifdef MIZU_EXT_NO_INLINES
2042MIZU_API int mizu_ix_utf8_valid(const void *s, size_t n);
2043MIZU_API int mizu_ix_tag_of(int wire_type);
2044MIZU_API int mizu_ix_attr_reserved(const char *key, size_t len);
2045MIZU_API size_t mizu_ix_put_header(unsigned char *dst);
2046MIZU_API size_t mizu_ix_put_nil(unsigned char *dst);
2047MIZU_API size_t mizu_ix_put_lgl(unsigned char *dst, int value);
2048MIZU_API size_t mizu_ix_put_int(unsigned char *dst, int64_t value);
2049MIZU_API size_t mizu_ix_put_real(unsigned char *dst, double value);
2050MIZU_API size_t mizu_ix_put_cplx(unsigned char *dst, double re, double im);
2051MIZU_API size_t mizu_ix_put_str(unsigned char *dst, const void *s,
2052 int32_t len);
2053MIZU_API size_t mizu_ix_put_bytes(unsigned char *dst, const void *data,
2054 uint64_t count);
2055MIZU_API size_t mizu_ix_put_vec(unsigned char *dst, int wire_type,
2056 const void *data, uint64_t count);
2057MIZU_API size_t mizu_ix_put_strv_begin(unsigned char *dst, uint64_t count);
2058MIZU_API size_t mizu_ix_put_strelt(unsigned char *dst, const void *s,
2059 int32_t len);
2060MIZU_API size_t mizu_ix_put_list_begin(unsigned char *dst, uint64_t count);
2061MIZU_API size_t mizu_ix_put_dict_begin(unsigned char *dst, uint64_t count);
2062MIZU_API size_t mizu_ix_put_key(unsigned char *dst, const void *s,
2063 uint32_t len);
2064MIZU_API size_t mizu_ix_put_attr(unsigned char *dst);
2065MIZU_API size_t mizu_ix_put_err(unsigned char *dst, int has_index,
2066 uint64_t index,
2067 const void *type, uint32_t type_len,
2068 const void *message, uint32_t message_len,
2069 const void *detail, uint32_t detail_len);
2070MIZU_API size_t mizu_ix_put_task(unsigned char *dst, int target, int kind,
2071 uint64_t ident);
2072MIZU_API size_t mizu_ix_put_ref(unsigned char *dst, const void *name,
2073 uint32_t name_len);
2074MIZU_API size_t mizu_ix_write_err(unsigned char *dst, uint32_t inline_max,
2075 const void *type, size_t type_n,
2076 const void *msg, size_t msg_n,
2077 const void *detail, size_t detail_n,
2078 int has_index, uint64_t index);
2079#else
2090MIZU_EXT_INLINE int mizu_ix_utf8_valid(const void *s, size_t n) {
2091 return mizu_ext_ix_utf8_valid_impl(s, n);
2092}
2093
2098 return mizu_ext_ix_tag_of_impl(wire_type);
2099}
2100
2105MIZU_EXT_INLINE int mizu_ix_attr_reserved(const char *key, size_t len) {
2106 return mizu_ext_ix_attr_reserved_impl(key, len);
2107}
2108MIZU_EXT_INLINE size_t mizu_ix_put_header(unsigned char *dst) {
2109 return mizu_ext_ix_put_header_impl(dst);
2110}
2111MIZU_EXT_INLINE size_t mizu_ix_put_nil(unsigned char *dst) {
2112 return mizu_ext_ix_put_nil_impl(dst);
2113}
2115MIZU_EXT_INLINE size_t mizu_ix_put_lgl(unsigned char *dst, int value) {
2116 return mizu_ext_ix_put_lgl_impl(dst, value);
2117}
2118MIZU_EXT_INLINE size_t mizu_ix_put_int(unsigned char *dst, int64_t value) {
2119 return mizu_ext_ix_put_int_impl(dst, value);
2120}
2123MIZU_EXT_INLINE size_t mizu_ix_put_real(unsigned char *dst, double value) {
2124 return mizu_ext_ix_put_real_impl(dst, value);
2125}
2126MIZU_EXT_INLINE size_t mizu_ix_put_cplx(unsigned char *dst, double re,
2127 double im) {
2128 return mizu_ext_ix_put_cplx_impl(dst, re, im);
2129}
2131MIZU_EXT_INLINE size_t mizu_ix_put_str(unsigned char *dst, const void *s,
2132 int32_t len) {
2133 return mizu_ext_ix_put_str_impl(dst, s, len);
2134}
2135MIZU_EXT_INLINE size_t mizu_ix_put_bytes(unsigned char *dst,
2136 const void *data, uint64_t count) {
2137 return mizu_ext_ix_put_bytes_impl(dst, data, count);
2138}
2142MIZU_EXT_INLINE size_t mizu_ix_put_vec(unsigned char *dst, int wire_type,
2143 const void *data, uint64_t count) {
2144 return mizu_ext_ix_put_vec_impl(dst, wire_type, data, count);
2145}
2146MIZU_EXT_INLINE size_t mizu_ix_put_strv_begin(unsigned char *dst,
2147 uint64_t count) {
2148 return mizu_ext_ix_put_strv_begin_impl(dst, count);
2149}
2151MIZU_EXT_INLINE size_t mizu_ix_put_strelt(unsigned char *dst, const void *s,
2152 int32_t len) {
2153 return mizu_ext_ix_put_strelt_impl(dst, s, len);
2154}
2155MIZU_EXT_INLINE size_t mizu_ix_put_list_begin(unsigned char *dst,
2156 uint64_t count) {
2157 return mizu_ext_ix_put_list_begin_impl(dst, count);
2158}
2159MIZU_EXT_INLINE size_t mizu_ix_put_dict_begin(unsigned char *dst,
2160 uint64_t count) {
2161 return mizu_ext_ix_put_dict_begin_impl(dst, count);
2162}
2164MIZU_EXT_INLINE size_t mizu_ix_put_key(unsigned char *dst, const void *s,
2165 uint32_t len) {
2166 return mizu_ext_ix_put_key_impl(dst, s, len);
2167}
2168MIZU_EXT_INLINE size_t mizu_ix_put_attr(unsigned char *dst) {
2169 return mizu_ext_ix_put_attr_impl(dst);
2170}
2171MIZU_EXT_INLINE size_t mizu_ix_put_err(unsigned char *dst, int has_index,
2172 uint64_t index,
2173 const void *type, uint32_t type_len,
2174 const void *message,
2175 uint32_t message_len,
2176 const void *detail,
2177 uint32_t detail_len) {
2178 return mizu_ext_ix_put_err_impl(dst, has_index, index, type, type_len,
2179 message, message_len, detail, detail_len);
2180}
2183MIZU_EXT_INLINE size_t mizu_ix_put_task(unsigned char *dst, int target,
2184 int kind, uint64_t ident) {
2185 return mizu_ext_ix_put_task_impl(dst, target, kind, ident);
2186}
2187
2188MIZU_EXT_INLINE size_t mizu_ix_put_ref(unsigned char *dst, const void *name,
2189 uint32_t name_len) {
2190 return mizu_ext_ix_put_ref_impl(dst, name, name_len);
2191}
2192
2200MIZU_EXT_INLINE size_t mizu_ix_write_err(unsigned char *dst,
2201 uint32_t inline_max,
2202 const void *type, size_t type_n,
2203 const void *msg, size_t msg_n,
2204 const void *detail, size_t detail_n,
2205 int has_index, uint64_t index) {
2206 return mizu_ext_ix_write_err_impl(dst, inline_max, type, type_n, msg,
2207 msg_n, detail, detail_n, has_index,
2208 index);
2209}
2210#endif
2211
2212// The Arrow frame writer (arrow.c) -----------------------------------------------
2213
2221#ifndef ARROW_C_DATA_INTERFACE
2222#define ARROW_C_DATA_INTERFACE
2223typedef struct ArrowSchema {
2224 const char *format;
2225 const char *name;
2226 const char *metadata;
2227 int64_t flags;
2228 int64_t n_children;
2229 struct ArrowSchema **children;
2230 struct ArrowSchema *dictionary;
2231 void (*release)(struct ArrowSchema *);
2232 void *private_data;
2233} ArrowSchema;
2234
2235typedef struct ArrowArray {
2236 int64_t length;
2237 int64_t null_count;
2238 int64_t offset;
2239 int64_t n_buffers;
2240 int64_t n_children;
2241 const void **buffers;
2242 struct ArrowArray **children;
2243 struct ArrowArray *dictionary;
2244 void (*release)(struct ArrowArray *);
2245 void *private_data;
2246} ArrowArray;
2247#endif
2248
2249#ifndef ARROW_C_STREAM_INTERFACE
2250#define ARROW_C_STREAM_INTERFACE
2251typedef struct ArrowArrayStream {
2252 int (*get_schema)(struct ArrowArrayStream *, struct ArrowSchema *out);
2253 int (*get_next)(struct ArrowArrayStream *, struct ArrowArray *out);
2254 const char *(*get_last_error)(struct ArrowArrayStream *);
2255 void (*release)(struct ArrowArrayStream *);
2256 void *private_data;
2258#endif
2259
2260/* The element helpers: pure accessors over the ABI structs, dual-form like
2261 the 'I' cursor's, so the frame writer's own loops and a binding's 'I'
2262 emitters share the one definition. */
2263MIZU_EXT_INLINE int mizu_ext_arrow_valid_impl(const ArrowArray *arr,
2264 int64_t k) {
2265 if (arr->null_count == 0 || arr->buffers[0] == NULL) return 1;
2266 int64_t i = arr->offset + k;
2267 const uint8_t *bm = (const uint8_t *) arr->buffers[0];
2268 return (bm[i >> 3] >> (i & 7)) & 1;
2269}
2270
2271MIZU_EXT_INLINE int mizu_ext_arrow_str_at_impl(const ArrowArray *arr,
2272 int64_t k, int form,
2273 const uint8_t **ptr,
2274 int32_t *len) {
2275 if (!mizu_ext_arrow_valid_impl(arr, k)) return 0;
2276 int64_t i = arr->offset + k;
2277 if (form == 1) {
2278 const uint8_t *offs = (const uint8_t *) arr->buffers[1];
2279 int32_t o0, o1;
2280 memcpy(&o0, offs + 4 * i, 4);
2281 memcpy(&o1, offs + 4 * (i + 1), 4);
2282 if (o0 < 0 || o1 < o0) return -1;
2283 *ptr = (const uint8_t *) arr->buffers[2] + o0;
2284 *len = o1 - o0;
2285 } else if (form == 2) {
2286 const uint8_t *offs = (const uint8_t *) arr->buffers[1];
2287 int64_t o0, o1;
2288 memcpy(&o0, offs + 8 * i, 8);
2289 memcpy(&o1, offs + 8 * (i + 1), 8);
2290 if (o0 < 0 || o1 < o0 || o1 - o0 > INT32_MAX)
2291 return -1;
2292 *ptr = (const uint8_t *) arr->buffers[2] + o0;
2293 *len = (int32_t) (o1 - o0);
2294 } else {
2295 const uint8_t *vw = (const uint8_t *) arr->buffers[1] + i * 16;
2296 int32_t l;
2297 memcpy(&l, vw, 4);
2298 if (l < 0) return -1;
2299 if (l <= 12) {
2300 *ptr = vw + 4;
2301 } else {
2302 int32_t bi, off;
2303 memcpy(&bi, vw + 8, 4);
2304 memcpy(&off, vw + 12, 4);
2305 /* The variadic data buffers are slots [2, n_buffers): the C Data
2306 spec's trailing slot is the int64 sizes array, but a producer
2307 that skips it (a nanoarrow nested or standalone export) puts a
2308 data buffer there. The reference tells the layouts apart: a
2309 spec-layout view never names the trailing slot, so one that
2310 does proves the no-sizes layout, its span the producer's word;
2311 any other span the sizes array bounds by the declared size. */
2312 if (bi < 0 || off < 0 || 2 + (int64_t) bi >= arr->n_buffers)
2313 return -1;
2314 if (2 + bi < arr->n_buffers - 1) {
2315 const uint8_t *sizes =
2316 (const uint8_t *) arr->buffers[arr->n_buffers - 1];
2317 if (sizes != NULL) {
2318 int64_t sz;
2319 memcpy(&sz, sizes + 8 * (ptrdiff_t) bi, 8);
2320 if ((int64_t) off + l > sz) return -1;
2321 }
2322 }
2323 *ptr = (const uint8_t *) arr->buffers[2 + bi] + off;
2324 }
2325 *len = l;
2326 }
2327 return 1;
2328}
2329
2330#ifdef MIZU_EXT_NO_INLINES
2331MIZU_API int mizu_arrow_valid(const ArrowArray *, int64_t k);
2332MIZU_API int mizu_arrow_str_at(const ArrowArray *, int64_t k, int form,
2333 const uint8_t **ptr, int32_t *len);
2334#else
2336MIZU_EXT_INLINE int mizu_arrow_valid(const ArrowArray *arr, int64_t k) {
2337 return mizu_ext_arrow_valid_impl(arr, k);
2338}
2339
2350 int form, const uint8_t **ptr,
2351 int32_t *len) {
2352 return mizu_ext_arrow_str_at_impl(arr, k, form, ptr, len);
2353}
2354#endif
2355
2357#define MIZU_ARROW_FLAG_DICTIONARY_ORDERED 1
2358#define MIZU_ARROW_FLAG_NULLABLE 2
2359
2362enum {
2365 MIZU_CVT_I16_INT,
2375};
2376
2377typedef struct mizu_cvt_row_s {
2378 int wire;
2379 int cvt;
2380 uint8_t w_in;
2381 uint8_t w_out;
2382} mizu_cvt_row;
2383
2387typedef struct mizu_arrow_warn_s {
2388 uint64_t n_range;
2389 uint64_t n_intmin;
2390 uint64_t n_int64;
2392
2395MIZU_API const mizu_cvt_row *mizu_cvt_for_arrow(const char *fmt);
2396
2404MIZU_API void mizu_cvt_convert(uint8_t *dst, const uint8_t *src,
2405 const uint8_t *valid, uint64_t off, size_t n,
2406 const mizu_cvt_row *row,
2407 mizu_arrow_warn *warn);
2408
2414typedef struct mizu_arrow mizu_arrow;
2415
2420enum {
2427};
2428
2429typedef struct mizu_arrow_col_s {
2430 int kind;
2431 const mizu_cvt_row *row;
2435} mizu_arrow_col;
2436
2450 const ArrowSchema *schema,
2451 const ArrowArray *const *batches,
2452 size_t n_batches, uint32_t peer_caps);
2453
2462 const ArrowSchema *schema,
2463 const ArrowArray *const *batches,
2464 size_t n_batches,
2465 uint32_t peer_caps);
2466
2476
2484
2490 const ArrowSchema *schema,
2491 const ArrowArray *const *batches,
2492 size_t n_batches,
2493 uint32_t peer_caps);
2494
2497MIZU_API void mizu_arrow_col_info(const mizu_arrow *, int64_t col,
2498 mizu_arrow_col *out);
2499
2509MIZU_API size_t mizu_arrow_col_body(const mizu_arrow *, int64_t col,
2510 uint8_t *dst, mizu_arrow_warn *warn);
2511
2517MIZU_API size_t mizu_arrow_col_blob(const mizu_arrow *, int64_t col,
2518 uint8_t *dst);
2519
2526MIZU_API int64_t mizu_arrow_col_validity(const mizu_arrow *, int64_t col,
2527 uint8_t *dst);
2528
2536MIZU_API int64_t mizu_arrow_col_utf8(const mizu_arrow *, int64_t col,
2537 int32_t *offs, uint8_t *data);
2538
2545MIZU_API size_t mizu_arrow_write_ix_col(const mizu_arrow *, int64_t col,
2546 uint8_t *dst, mizu_arrow_warn *warn);
2547
2558MIZU_API size_t mizu_arrow_write_ix(const mizu_arrow *, uint8_t *dst,
2559 mizu_arrow_warn *warn);
2560
2570 const char *ident, size_t ident_len,
2571 void *ref_base, size_t ref_size,
2572 int64_t ref_leaf);
2573
2578 size_t len);
2579
2583MIZU_API void mizu_arrow_tz(mizu_arrow *, int64_t col, const char *tz);
2584
2589
2603 mizu_slot_hdr *hdr,
2604 unsigned char *payload,
2605 mizu_arrow_warn *warn);
2606MIZU_API void mizu_arrow_close(mizu_arrow *);
2607
2608// Utilities ----------------------------------------------------------------------
2609
2612MIZU_API double mizu_now(void);
2613MIZU_API long mizu_self_pid(void);
2614
2617MIZU_API void mizu_err_describe(mizu_errcat, const char **summary,
2618 const char **hint);
2619
2622MIZU_API void mizu_rng_jump(int *seed);
2623
2626
2627#ifdef __cplusplus
2628}
2629#endif
2630
2631#endif /* MIZU_EXT_H */
libmizu public API: SPSC channels and work-stealing task pools between processes on one machine.
#define MIZU_NA_INT32
The missing-value sentinels of the atomic wire types (the R ABI fixes the bit patterns; a binding of ...
Definition mizu.h:338
@ MIZU_TYPE_CPLX
interleaved float64 re/im
Definition mizu.h:328
@ MIZU_TYPE_RAW
bytes
Definition mizu.h:331
@ MIZU_TYPE_REAL
float64; a specific NaN payload is the sentinel
Definition mizu.h:327
@ MIZU_TYPE_LGL
int32 logical; INT_MIN is the missing sentinel
Definition mizu.h:325
@ MIZU_TYPE_STR
string vector (MIZS layout)
Definition mizu.h:329
@ MIZU_TYPE_VEC
list tree (MIZL layout)
Definition mizu.h:330
@ MIZU_TYPE_INT64
int64; INT64_MIN is the missing sentinel
Definition mizu.h:332
@ MIZU_TYPE_INT
int32; INT_MIN is the missing sentinel
Definition mizu.h:326
#define MIZU_MAGIC_VEC
"MIZH"
Definition mizu.h:414
#define MIZU_MAGIC_STR
"MIZS"
Definition mizu.h:415
#define MIZU_HDR_VALID_OFF
Header bytes [40-47] and [48-55] are the optional validity-bitmap section of every MIZH and MIZL head...
Definition mizu.h:386
struct mizu_binding_s mizu_binding
The binding callbacks struct, taken by pointer at create/attach/join.
Definition mizu.h:167
enum mizu_errcat_e mizu_errcat
Portable failure categories for MIZU_ERR (the platform layer classifies errno / GetLastError).
#define MIZU_MIZL_S4
MIZH (atomic vector): [4-7] i32 wire type, [8-15] i64 element count, [16-23] i64 attrs blob size; bar...
Definition mizu.h:412
#define MIZU_NAME_MAX
fits Windows worst case + NUL; Darwin PSHMNAMLEN
Definition mizu.h:419
#define MIZU_NA_INT64
NA_integer64_ sentinel.
Definition mizu.h:339
#define MIZU_API
Export macro.
Definition mizu.h:105
#define MIZU_ZC_REFCOUNT_OFF
Zero-copy view protocol: mizu-owned bytes [24-31] of every MIZU* region header (reserved [24-63] in t...
Definition mizu.h:351
#define MIZU_MAGIC_LIST
"MIZL"
Definition mizu.h:416
size_t mizu_type_elt_size(int type)
Element size of an atomic wire type, 0 for non-atomic.
struct mizu_task_s mizu_task
A task handle: an 8-byte value identifying one result slot at one sequence, with a 40-bit sequence in...
enum mizu_status_e mizu_status
The verb result.
#define MIZU_ATOMIC(T)
The wire-format structs carry atomic words.
Definition mizu.h:86
#define MIZU_NA_REAL_BITS
NA_real_ (a NaN payload).
Definition mizu.h:340
#define MIZU_HDR_FLAGS_OFF
Region magics (first 4 bytes): atomic vector, string vector, list tree.
Definition mizu.h:367
static size_t mizu_ix_put_str(unsigned char *dst, const void *s, int32_t len)
The 0x04 string scalar; len -1 is NA (s may then be NULL).
Definition mizu_ext.h:2131
void mizu_binding_bytes(mizu_binding *)
Fill binding with the bytes binding: stage/read only; exec, check, park, sweep, drop are NULL (a byte...
mizu_status mizu_arrow_open_lazy(mizu_arrow **out, const ArrowSchema *schema, const ArrowArray *const *batches, size_t n_batches, uint32_t peer_caps)
The open without the string walks, for a binding that registers remote overrides (mizu_arrow_remote) ...
mizu_status mizu_pool_collect_terminal(mizu_pool *, const mizu_task *, size_t n, void **values_out, size_t *count_out, size_t *fail_out)
The event-driven drain's batch collect: mizu_pool_collect's claim half over a task set,...
#define MIZU_IX_ATTR_NAMES
The attribute vocabulary (DESIGN.md's byte-shape helper registry): the shape whitelist's wire strings...
Definition mizu_ext.h:1709
mizu_status mizu_ixt_want_dict(mizu_ix *cur, mizu_ix_item *item)
The next item must be a DICT begin (the named field).
static uint64_t mizu_na_build(int type, uint8_t *bitmap, const void *src, uint64_t n, uint64_t bit_off)
The sentinels-to-bitmap scan: bit (bit_off + i) of bitmap records element i of src,...
Definition mizu_ext.h:1533
static uint32_t * mizu_zc_rc(void *base)
The zc refcount / flags words of a MIZU* region header (bytes [24-31] of the reserved band,...
Definition mizu_ext.h:996
static mizu_mizs_geom mizu_mizs_geometry(int64_t n)
The string block's section offsets for n strings (each section 64-byte aligned from the block start).
Definition mizu_ext.h:1047
static size_t mizu_ix_put_strelt(unsigned char *dst, const void *s, int32_t len)
A bare strv element; len -1 is NA.
Definition mizu_ext.h:2151
int64_t mizu_arrow_col_utf8(const mizu_arrow *, int64_t col, int32_t *offs, uint8_t *data)
A null-free utf8 or large_utf8 column's strings over every batch, concatenated for a binding's own st...
@ MIZU_ARROW_TS
timestamp -> REAL seconds + the POSIXct blob
Definition mizu_ext.h:2425
@ MIZU_ARROW_TD
duration -> REAL seconds + the difftime blob
Definition mizu_ext.h:2426
@ MIZU_ARROW_CVT
fixed-width, through the cvt row
Definition mizu_ext.h:2421
@ MIZU_ARROW_DATE
date32 -> REAL days + the Date blob
Definition mizu_ext.h:2424
@ MIZU_ARROW_DICT
dictionary<strings> -> a factor
Definition mizu_ext.h:2423
@ MIZU_ARROW_STR
utf8 / large_utf8 / string_view -> MIZS
Definition mizu_ext.h:2422
static uint64_t mizu_aux_shm_vec(int type, uint64_t total)
aux = layout type tag | exact used bytes << 8.
Definition mizu_ext.h:1453
mizu_status mizu_pool_submit_flags(mizu_pool *, void *task_obj, uint16_t flags, mizu_task *out, double timeout_ms)
The map runner's submit: mizu_pool_submit plus the entry flags (MIZU_ENTRY_RUNNER marks a map's join ...
int mizu_pool_help_once(mizu_pool *)
One doorbell help beat (claims a map runner and re-homes it onto the helper's own deque) and the test...
mizu_status mizu_ix_skip(mizu_ix *cur, const unsigned char **start, size_t *len)
Consume one complete value at the cursor's position without building it, reporting its byte span: *st...
static int mizu_ext_ix_tag_of_impl(int wire_type)
Definition mizu_ext.h:1772
mizu_status mizu_arrow_stage(mizu_handle *, mizu_arrow *, mizu_slot_hdr *hdr, unsigned char *payload, mizu_arrow_warn *warn)
The one-call stage over the handle seam, valid only during a stage_fn call.
static int mizu_arrow_str_at(const ArrowArray *arr, int64_t k, int form, const uint8_t **ptr, int32_t *len)
The string forms' per-batch element access: utf8 (i32 offsets), large_utf8 (i64), string_view (16-byt...
Definition mizu_ext.h:2349
static uint64_t mizu_aux_rawspill_pool(int type, uint32_t name_len)
aux = wire type tag | region name length << 8.
Definition mizu_ext.h:1449
size_t mizu_arrow_size_floor(const mizu_arrow *)
A lower bound on the fully local region span (every remote override ignored), O(columns) and valid st...
#define MIZU_EXT_INLINE
Some static inlines below go unused in some TUs; the amalgamation folds this header into the single m...
Definition mizu_ext.h:64
struct mizu_mizl_entry_s mizu_mizl_entry
One MIZL directory entry plus its validity-table pair (mizu.h documents the 32-byte entry): mizu_mizl...
struct mizu_pool_sig_s mizu_pool_sig
The opaque pool-signal trio a map runner loads relaxed once per batch transition: the help_wanted doo...
uint32_t mizu_morsel_generation(const void *base, const mizu_morsel_hdr *h)
Single reads of the mutable words: the generation (masked), the cursor clamped to n_morsels,...
static size_t mizu_ext_ix_put_ref_impl(unsigned char *dst, const void *name, uint32_t name_len)
Definition mizu_ext.h:1987
void mizu_pool_eval_mark(mizu_pool *, int in_flight)
The eval-in-flight marker a binding sets around its catching = 0 task eval only (R: around Rf_eval,...
mizu_status mizu_ix_open(mizu_ix *cur, const void *buf, size_t len)
Open a cursor over a whole stream: the magic and version checks, the unknown version taking the infor...
static size_t mizu_ix_put_ref(unsigned char *dst, const void *name, uint32_t name_len)
The ref leaf: the region identifier span (1-255 bytes; 0 returns 0).
Definition mizu_ext.h:2188
void mizu_unpark_all(mizu_parker *pk)
The split-waiter form: wakes every sleeper on the epoch word.
void mizu_morsel_cancel_set(void *base, const mizu_morsel_hdr *h)
The cancel word: set by the submitter on timeout / interrupt / death, and by an erroring runner itsel...
static int mizu_ext_valid_ok(int64_t off, int64_t count, uint64_t n, size_t size)
The three-state validity pair of an MIZH header or one MIZL leaf-table entry: {0, 0}...
Definition mizu_ext.h:1067
#define MIZU_INTEROP_MAGIC
'I': the interchange stream (DESIGN.md's Interchange codec section)
Definition mizu_ext.h:106
void mizu_pool_submit_wake(mizu_pool *, size_t done)
The batch submit's protocol wake, for a binding that abandons mizu_pool_submit_batch_fn mid-burst (a ...
static int mizu_ext_mizh_check_impl(const void *base, size_t size, int *wire_type, int64_t *n_elems, int64_t valid[2])
The bodies behind the larger dual-form functions below: both the header inline and the exported symbo...
Definition mizu_ext.h:1210
void mizu_cvt_convert(uint8_t *dst, const uint8_t *src, const uint8_t *valid, uint64_t off, size_t n, const mizu_cvt_row *row, mizu_arrow_warn *warn)
One run's convert into a reserved destination: the BIT_LGL / masked / plain dispatch.
static double mizu_timeout_ms(double seconds)
seconds (a binding's convention; <= 0 polls, non-finite waits indefinitely) to the core's timeout_ms ...
Definition mizu_ext.h:1441
const char * mizu_live_dir(void)
Directory for liveness lock files: the MIZU_LIVENESS_DIR override (read-through, checked every call) ...
void mizu_tune(void)
malloc tuning (tune.c), called by the binding at load.
static size_t mizu_ext_ix_put_tag8_impl(unsigned char *dst, uint32_t tag, uint64_t v)
Definition mizu_ext.h:1812
struct mizu_arrow mizu_arrow
The Arrow -> MIZL frame writer: a classified, batch-vetted frame plus its size-pass results,...
Definition mizu_ext.h:2414
static size_t mizu_ext_ix_put_header_impl(unsigned char *dst)
The interop emit helpers' bodies (the interchange stream section below): every count and value follow...
Definition mizu_ext.h:1804
mizu_status mizu_arrow_validate(mizu_arrow *)
The string walks a lazy open deferred: every string column not yet walked and not registered remote.
static int mizu_ext_flags_known(const void *base)
The [32-35] format flags word admits only the assigned S4 bit.
Definition mizu_ext.h:1058
const char * mizu_morsel_hdr_check(const void *base, size_t size, mizu_morsel_hdr *out)
Validate a region's header against the mapping size, including section placement.
static int mizu_ix_tag_of(int wire_type)
The wire-type -> vector-tag mapping: MIZU_TYPE_LGL / INT / REAL / CPLX / RAW / INT64 -> 0x06 / 0x07 /...
Definition mizu_ext.h:2097
@ MIZU_IX_ATTR
begin: one value item, then one DICT begin
Definition mizu_ext.h:1612
@ MIZU_IX_CPLX
u64[0..1]: the re / im f64 bits
Definition mizu_ext.h:1604
@ MIZU_IX_BYTES
the 0x05 bytes scalar
Definition mizu_ext.h:1607
@ MIZU_IX_REAL
u64[0]: the f64 bits
Definition mizu_ext.h:1603
@ MIZU_IX_LIST
begin: count value items follow
Definition mizu_ext.h:1610
@ MIZU_IX_STR1
the 0x04 string scalar
Definition mizu_ext.h:1605
@ MIZU_IX_TASK
header: count fields follow as ordinary items
Definition mizu_ext.h:1614
@ MIZU_IX_VEC
0x06-0x0a, 0x0e: type is the MIZU_TYPE_* tag
Definition mizu_ext.h:1608
@ MIZU_IX_LGL
u64[0]: 0, 1, or 2 (NA)
Definition mizu_ext.h:1601
@ MIZU_IX_ERR
the err fields; legal at the top level only
Definition mizu_ext.h:1613
@ MIZU_IX_DICT
begin: count (STR key, value) pairs follow
Definition mizu_ext.h:1611
@ MIZU_IX_REF
the 0x13 region identifier span (ptr, len)
Definition mizu_ext.h:1615
@ MIZU_IX_STR
a bare string (strv element, or dict key: key=1)
Definition mizu_ext.h:1606
@ MIZU_IX_INT
u64[0]: the i64
Definition mizu_ext.h:1602
@ MIZU_IX_STRV
begin: count STR items follow
Definition mizu_ext.h:1609
int mizu_handle_kind(const mizu_handle *)
Handle queries standing in for field access into the private handle struct.
uint32_t mizu_morsel_reset(void *base, const mizu_morsel_hdr *h)
Prepared-run re-arm, O(1) in n (no per-morsel state exists to clear): bump the generation,...
mizu_status mizu_ixt_want_code(mizu_ix *cur, mizu_ix_item *item)
The next item must be a non-NA STR1 (the 0x04 code string).
struct mizu_death_watch_s mizu_death_watch
Translates a watched pid's exit into *flag = 1 plus a directed unpark of pk (optional,...
Definition mizu_ext.h:595
mizu_status mizu_ix_next(mizu_ix *cur, mizu_ix_item *item)
The next item: bounds, depth, UTF-8 and grammar checks per the spec.
void * mizu_stage_arena_alloc(mizu_handle *, size_t n, uint64_t *off)
Core services for a stager (per handle), valid only during a stage_fn call.
struct mizu_morsel_sizer_s mizu_morsel_sizer
Batch-sizing state: process-private, never wire state, reset at each run's first-call CLAIM CAS.
int mizu_morsel_abandon(void *base, const mizu_morsel_hdr *h, uint32_t r, uint32_t gen)
The exhausted-runner trim's CAS, folding its own trigger: a no-op unless the cursor is exhausted or t...
size_t mizu_arrow_write_ix_col(const mizu_arrow *, int64_t col, uint8_t *dst, mizu_arrow_warn *warn)
One column's 'I' value form (the inline small-table path): the plain vectors bare (the cvt tag off th...
#define MIZU_IX_VERSION
The byte-level half of DESIGN.md's Interchange codec section: a validating pull cursor (mizu_ix_open ...
Definition mizu_ext.h:1562
static int mizu_aux_type(uint64_t aux)
The decode half of the aux split (low byte the type tag, the kind's field above it): the tag,...
Definition mizu_ext.h:1459
static int mizu_mizs_check(const void *base, size_t size, int64_t *n, int64_t *str_size, int64_t *attrs_size)
Validate the MIZS header at base against the region: magic, the format flags word,...
Definition mizu_ext.h:1505
size_t mizu_channel_avail(mizu_channel *)
The consumer's published-not-yet-consumed message count: one acquire load on a local-cache miss,...
mizu_status mizu_arrow_open(mizu_arrow **out, const ArrowSchema *schema, const ArrowArray *const *batches, size_t n_batches, uint32_t peer_caps)
Classify every column off the schema (a struct schema; the type map of DESIGN.md's frame section),...
struct mizu_handle_s mizu_handle
Generic handle view for the stager services; the core passes the handle a binding's callbacks were re...
Definition mizu_ext.h:167
static int mizu_arrow_valid(const ArrowArray *arr, int64_t k)
The validity bit for element k of a batch (Arrow LSB-first).
Definition mizu_ext.h:2336
void mizu_morsel_span_of(const mizu_morsel_hdr *h, uint64_t m, uint64_t k, uint64_t *lo, uint64_t *hi)
A batch's element range, 0-based half-open: [m * morsel_size, min((m + k) morsel_size,...
static uint32_t mizu_parker_snapshot(const mizu_parker *pk)
The park handshake's first step (dual form; see the banner).
Definition mizu_ext.h:578
size_t mizu_arrow_write_ix(const mizu_arrow *, uint8_t *dst, mizu_arrow_warn *warn)
One complete 'I' stream of the whole frame (the small-table path): the header, the attr mark,...
void mizu_preamble_write(void *region, const mizu_preamble *p)
Host writes at create, immutable thereafter; the peer validates before any shared atomic is read or w...
static int mizu_ix_utf8_valid(const void *s, size_t n)
The emit helpers: each returns its byte count, writing only when dst is not NULL, so a binding's two-...
Definition mizu_ext.h:2090
int64_t mizu_handle_keep_out(const mizu_handle *)
The tx-keeper count: outstanding staging retains on a channel handle (the reap gate's own count,...
int(* mizu_stage_fn)(void *obj, mizu_slot_hdr *hdr, uint8_t *payload, uint32_t inline_max, mizu_handle *, void *ctx)
The core never sees a language object.
Definition mizu_ext.h:302
static size_t mizu_ix_put_lgl(unsigned char *dst, int value)
value on the wire: 0, 1, or 2 (NA).
Definition mizu_ext.h:2115
double mizu_now(void)
Monotonic seconds (the spin/deadline clock) and the current pid (cached, with an atfork reset).
mizu_status mizu_ix_end(mizu_ix *cur)
The finishing check: exactly one value per stream.
uint64_t mizu_morsel_layout(mizu_morsel_hdr *h, uint64_t n, uint64_t morsel_size, uint64_t desc_len, uint32_t x_type, uint64_t x_len, uint32_t out_type, uint64_t out_m, uint32_t claim_n)
Fill *h with the section layout for a map of n elements (morsel_size the geometry input,...
static size_t mizu_ext_ix_write_err_impl(unsigned char *dst, uint32_t inline_max, const void *type, size_t type_n, const void *msg, size_t msg_n, const void *detail, size_t detail_n, int has_index, uint64_t index)
Definition mizu_ext.h:2012
static size_t mizu_ix_write_err(unsigned char *dst, uint32_t inline_max, const void *type, size_t type_n, const void *msg, size_t msg_n, const void *detail, size_t detail_n, int has_index, uint64_t index)
A complete 'I' err stream in one call: the two-byte header, then the 0x11 err item,...
Definition mizu_ext.h:2200
size_t mizu_arrow_col_body(const mizu_arrow *, int64_t col, uint8_t *dst, mizu_arrow_warn *warn)
One column's by-value body bytes over every batch: the cvt lanes, a dictionary's 1-based codes,...
struct mizu_morsel_span_s mizu_morsel_span
Worker-death lost set: issued = [0, bound), lost = issued minus the union of the collected batch hist...
static void mizu_ext_na_store(int type, void *dst, uint64_t i)
Write the wire type's missing sentinel over element i (the CPLX pair both parts).
Definition mizu_ext.h:1134
void mizu_arrow_col_info(const mizu_arrow *, int64_t col, mizu_arrow_col *out)
The per-column classification, for a binding's own emitters over the same batches (pymizu's inline fr...
static size_t mizu_ix_put_vec(unsigned char *dst, int wire_type, const void *data, uint64_t count)
The fixed-width vector tags: wire_type one of MIZU_TYPE_LGL / INT / REAL / CPLX / RAW / INT64 (any ot...
Definition mizu_ext.h:2142
static int mizu_mizl_elem(const void *base, size_t size, int64_t i, mizu_mizl_entry *elem)
One bounds-checked MIZL directory entry (index i), with its validity-table pair: a {0,...
Definition mizu_ext.h:1525
enum mizu_live_probe_e mizu_live_probe
Exclusive flock (POSIX) / LockFileEx (Windows) held for a process's entire lifetime and released by t...
mizu_pool_sig * mizu_pool_signals(mizu_pool *)
A malloc'd copy of the signal trio (the caller frees).
void mizu_err_describe(mizu_errcat, const char **summary, const char **hint)
Category + remediation text for an MIZU_ERRCAT (what the handle / thread-local error slot records).
mizu_status mizu_ixt_open(mizu_ix *cur, const void *buf, size_t len, mizu_ix_item *item, int max_kind)
The task-stream decode shim (DESIGN.md's byte-shape helper registry): the per-field shape checks ever...
struct mizu_result_sink_s mizu_result_sink
Valid only during an exec callback.
Definition mizu_ext.h:170
static size_t mizu_ix_put_key(unsigned char *dst, const void *s, uint32_t len)
A bare dict key: never NA, so the length is unsigned.
Definition mizu_ext.h:2164
void * mizu_stage_raw_spill(mizu_handle *, uint64_t n, int wire_type, mizu_slot_hdr *, uint8_t *payload, uint32_t inline_max)
Raw-tier reservation (stage_raw.c), valid only during stage_fn: choose the tier for n bare bytes of w...
mizu_status mizu_channel_notify_arm(mizu_channel *, int on, int *avail_out)
Event-driven wakes for a binding's async integration: an armed consumer owns a named,...
size_t mizu_arrow_col_blob(const mizu_arrow *, int64_t col, uint8_t *dst)
One column's attribute dict as a bare 0x0d dict value (the §3.5 blob without its stream header,...
void mizu_arrow_tz(mizu_arrow *, int64_t col, const char *tz)
The tzone override: the POSIXct leaf blob writes tz verbatim (a same-language sender's naivety-preser...
static size_t mizu_ix_put_real(unsigned char *dst, double value)
real and cplx take doubles and memcpy them: NaN payloads are bitwise-preserved.
Definition mizu_ext.h:2123
int mizu_morsel_next(void *base, const mizu_morsel_hdr *h, mizu_morsel_sizer *sz, uint32_t r, uint32_t gen, const mizu_pool_sig *sig, uint64_t pin_k, double now, uint64_t *m_out, uint64_t *k_out, int *help_out)
One whole batch transition (generation-fenced lane claim, cancel and pool-signal checks,...
size_t mizu_arrow_size(mizu_arrow *)
The region span in bytes: runs the size pass on first call (the result memoizes; an override register...
static size_t mizu_ix_put_task(unsigned char *dst, int target, int kind, uint64_t ident)
The task header: target language byte, kind byte, the reserved u16 flags written zero,...
Definition mizu_ext.h:2183
static size_t mizu_ext_ix_err_floor_impl(const void *s, size_t n, size_t share)
Definition mizu_ext.h:2001
void mizu_death_listener_teardown(void)
Library-unload teardown; joins the Linux epoll thread (no-op elsewhere: macOS dispatch sources and Wi...
int mizu_pool_map_caps(mizu_pool *, uint32_t *free_rs, uint32_t *inj_cap, uint32_t *inline_entry)
A map's batch-sizing inputs: claims the worker's nested-submitter slot when unclaimed (as a first nes...
struct mizu_bytes_s mizu_bytes
A byte buffer.
void mizu_binding_init(mizu_binding *)
Zero and size-stamp a binding struct.
struct mizu_ix_s mizu_ix
The pull cursor.
static int mizu_ix_attr_reserved(const char *key, size_t len)
The reserved attribute keys: every MIZU_IX_ATTR_* key (the whitelist vocabulary) plus "tsp": 1 when t...
Definition mizu_ext.h:2105
struct mizu_arrow_warn_s mizu_arrow_warn
The conversion warning counts, accumulated by mizu_cvt_convert and the frame writer; the binding rais...
static int mizu_mizl_check(const void *base, size_t size, int64_t *n, int64_t *attrs_off, int64_t *attrs_size, int64_t valid[2])
Validate the MIZL header at base and its directory's extent against the region: magic,...
Definition mizu_ext.h:1515
int mizu_pool_unwind_sink(mizu_pool *, mizu_result_sink *out)
The unwind path for a binding whose catching = 0 exec abandons (R's longjmp): when the eval marker sa...
mizu_shm * mizu_read_region(mizu_read_ctx *, const uint8_t *name, uint32_t len)
Read-side service, invoked through the read_ctx handed to read_fn: a borrowed consumer mapping for a ...
mizu_status mizu_arrow_open_vector(mizu_arrow **out, const ArrowSchema *schema, const ArrowArray *const *batches, size_t n_batches, uint32_t peer_caps)
The bare-vector counterpart, for a binding's per-column writers over a one-column struct wrapper.
int mizu_parker_attach(mizu_parker *pk, uint32_t *epoch, const char *region_name, int entity, int create)
region_name/entity name the Windows event ("<region>.pk.<entity>"), created by the region's host (cre...
static void mizu_mizh_validity_set(void *base, int64_t off, int64_t count)
Stamp the validity pair of an MIZH or MIZL header (the one write site): {0, 0} absent,...
Definition mizu_ext.h:1495
struct mizu_parker_s mizu_parker
One parker per waiting entity: a 32-bit monotonic epoch word in the shared region plus,...
mizu_live_probe_e
Exclusive flock (POSIX) / LockFileEx (Windows) held for a process's entire lifetime and released by t...
Definition mizu_ext.h:715
#define MIZU_EXT_LOAD_ACQ(p)
The dual-form inlines touch atomics; spell the acquire load per language (mizu.h's MIZU_ATOMIC gives ...
Definition mizu_ext.h:73
struct mizu_morsel_hdr_s mizu_morsel_hdr
The 128-byte region header.
void mizu_arrow_root_blob(mizu_arrow *, const void *blob, size_t len)
The root-blob override: a complete 'I' stream for the frame attributes the caller built (pymizu's cus...
mizu_status mizu_ixt_want_list(mizu_ix *cur, mizu_ix_item *item)
The next item must be a LIST begin (the positional field).
static uint64_t mizu_na_apply(int type, void *dst, const void *src, const uint8_t *bitmap, uint64_t n)
The bitmap-to-sentinels copy: n elements land at dst, the clear bits' elements written as the wire ty...
Definition mizu_ext.h:1541
const mizu_cvt_row * mizu_cvt_for_arrow(const char *fmt)
The Arrow-format keying of the conversion table (every supported row a single character); NULL when t...
static int mizu_ext_mizl_ent(const void *base, size_t size, int64_t i, mizu_mizl_entry *e)
One MIZL directory entry's checks, shared by mizu_mizl_check's pass and mizu_mizl_elem: alignment and...
Definition mizu_ext.h:1167
static int mizu_ext_na_real_bits(uint64_t bits)
One per-element NA test per NA-capable wire type (anything else is never null): INT32_MIN for LGL and...
Definition mizu_ext.h:1104
struct mizu_mizs_geom_s mizu_mizs_geom
The small pure helpers over mizu.h's wire-format conventions, dual form like the zc accessors above (...
#define MIZU_IX_ERR_OVERHEAD
The err framer's budget constants (mizu_ix_write_err below), the wire authority for the bounded err i...
Definition mizu_ext.h:1701
void mizu_rng_jump(int *seed)
One 2^127-step L'Ecuyer-CMRG stream jump in place over a 6-word state (rng_jump.c; a binding's map de...
static int mizu_ext_mizl_tag_ok(int32_t sexptype)
A directory entry's sexptype: MIZU_MIZL_S4 masked off, the remainder a listed tag: 0 (a serialized le...
Definition mizu_ext.h:1079
static int mizu_mizh_check(const void *base, size_t size, int *wire_type, int64_t *n_elems, int64_t valid[2])
Validate the MIZH header at base: region size, magic, a known atomic wire type, the element/attribute...
Definition mizu_ext.h:1487
int mizu_park(mizu_parker *pk, uint32_t snapshot, long timeout_ms)
Sleeps while the epoch still equals snapshot, up to timeout_ms (0 = poll: never sleeps; < 0 = indefin...
@ MIZU_CVT_U16_INT
zero-widen
Definition mizu_ext.h:2366
@ MIZU_CVT_COPY
identity: one memcpy per run
Definition mizu_ext.h:2363
@ MIZU_CVT_U32_REAL
exact
Definition mizu_ext.h:2367
@ MIZU_CVT_BOOL8_LGL
one byte per source lane (numpy '?
Definition mizu_ext.h:2372
@ MIZU_CVT_F32_REAL
exact
Definition mizu_ext.h:2371
@ MIZU_CVT_U64_INT64
in-range exact; past INT64_MAX the int64 NA sentinel, counted in n_range
Definition mizu_ext.h:2368
@ MIZU_CVT_BIT_LGL
one bit per source lane (Arrow 'b') -> int32 0/1
Definition mizu_ext.h:2373
@ MIZU_CVT_I8_INT
sign-widen
Definition mizu_ext.h:2364
@ MIZU_CVT_C64_CPLX
float re/im -> double re/im
Definition mizu_ext.h:2374
@ MIZU_CVT_F16_REAL
exact (half decodes into double's wider fields)
Definition mizu_ext.h:2370
uint64_t mizu_channel_peer_ident(const mizu_channel *)
The identity words (the language registry above).
static void mizu_mizh_write(void *base, int wire_type, int64_t n_elems)
The flat MIZH header: magic, type, element count, zero attrs, and the reserved band [24,...
Definition mizu_ext.h:1469
int mizu_arrow_remote(mizu_arrow *, int64_t col, const char *ident, size_t ident_len, void *ref_base, size_t ref_size, int64_t ref_leaf)
A remote-leaf override (pymizu's provenance match): column col crosses as directory tag 33: the ident...
int64_t mizu_arrow_col_validity(const mizu_arrow *, int64_t col, uint8_t *dst)
One column's validity over every batch, concatenated from bit 0 into dst ((rows + 7) / 8 bytes,...
static int mizu_ext_ix_utf8_valid_impl(const void *s, size_t n)
The byte-shape helpers' bodies (DESIGN.md's byte-shape helper registry): pure byte math over the wire...
Definition mizu_ext.h:1739
int mizu_result_publish(mizu_result_sink *, void *value)
Result publish, valid during exec only.
Definition mizu_ext.h:2251
Definition mizu_ext.h:2235
Definition mizu_ext.h:2223
Definition mizu_ext.h:2429
int u64_verbatim
CVT 'L': the MIZU_CAP_MIZL_U64V verdict (the leaf writes INT64 + the u64 blob attr,...
Definition mizu_ext.h:2433
int str_form
STR / DICT: 1 utf8, 2 large_utf8, 3 view.
Definition mizu_ext.h:2432
int kind
MIZU_ARROW_*.
Definition mizu_ext.h:2430
const mizu_cvt_row * row
CVT.
Definition mizu_ext.h:2431
The conversion warning counts, accumulated by mizu_cvt_convert and the frame writer; the binding rais...
Definition mizu_ext.h:2387
uint64_t n_int64
a genuine int64 INT64_MIN: reads as NA in R
Definition mizu_ext.h:2390
uint64_t n_intmin
a genuine int32 INT32_MIN: reads as NA in R
Definition mizu_ext.h:2389
uint64_t n_range
uint64 past INT64_MAX -> the int64 NA sentinel
Definition mizu_ext.h:2388
Definition mizu_ext.h:339
mizu_exec_fn exec
pool workers only; NULL on submitter handles
Definition mizu_ext.h:344
uint64_t ident
MIZU_IDENT word; a zero language byte is rejected at create, attach and join.
Definition mizu_ext.h:350
mizu_check_fn check
interrupt poll; NULL for plain-C consumers
Definition mizu_ext.h:345
mizu_sweep_fn sweep
idle cache drop; usually NULL
Definition mizu_ext.h:347
uint32_t size
sizeof(mizu_binding); set with mizu_binding_init and checked at create/attach/join
Definition mizu_ext.h:340
mizu_park_fn park
around-park lock release; usually NULL
Definition mizu_ext.h:346
mizu_drop_fn drop
pin release; NULL when the binding never pins
Definition mizu_ext.h:348
void * ctx
opaque to the core
Definition mizu_ext.h:349
uint32_t flags
MIZU_BINDING_* bits (the seam comment above); zero from mizu_binding_init.
Definition mizu_ext.h:352
A byte buffer.
Definition mizu_ext.h:490
Definition mizu_ext.h:2377
int cvt
MIZU_CVT_*.
Definition mizu_ext.h:2379
int wire
the MIZU_TYPE_* the row produces
Definition mizu_ext.h:2378
uint8_t w_in
source element bytes (0: bit-packed, MIZU_CVT_BIT_LGL)
Definition mizu_ext.h:2380
Definition mizu_ext.h:1618
uint64_t len
STR1 / STR / REF: the span's byte length.
Definition mizu_ext.h:1624
int key
STR: a dict key (an strv element has 0).
Definition mizu_ext.h:1622
uint32_t task_kind
TASK: the kind byte.
Definition mizu_ext.h:1630
uint32_t kind
MIZU_IX_*.
Definition mizu_ext.h:1619
uint32_t err_flags
ERR: bit 0 = index present.
Definition mizu_ext.h:1631
uint64_t err_index
ERR: valid when err_flags bit 0 is set.
Definition mizu_ext.h:1632
uint32_t target
TASK: the target language byte.
Definition mizu_ext.h:1629
uint32_t type
VEC: the MIZU_TYPE_* wire type.
Definition mizu_ext.h:1620
uint64_t count
BYTES / VEC / STRV / LIST / DICT: the element (pair) count; TASK: the field arity.
Definition mizu_ext.h:1625
const unsigned char * ptr
STR1 / STR / BYTES / VEC / REF: the data span.
Definition mizu_ext.h:1623
uint64_t u64[2]
LGL: u64[0] in {0, 1, 2}; INT / REAL: u64[0]; CPLX: both; TASK: u64[0] = submitter identity.
Definition mizu_ext.h:1627
int na
STR1 / STR: the -1 (NA) form.
Definition mizu_ext.h:1621
The pull cursor.
Definition mizu_ext.h:1642
int done
the root value completed
Definition mizu_ext.h:1646
uint64_t remaining
items owned at this level
Definition mizu_ext.h:1650
int err
latched: all later calls fail
Definition mizu_ext.h:1647
uint32_t kind
the MIZU_IX_* begin kind
Definition mizu_ext.h:1649
One MIZL directory entry plus its validity-table pair (mizu.h documents the 32-byte entry): mizu_mizl...
Definition mizu_ext.h:1032
int64_t data_offset
64-byte aligned
Definition mizu_ext.h:1033
int64_t valid[2]
{0, 0} absent, {0, -1} known-NA-free, else {bitmap offset, null count}
Definition mizu_ext.h:1038
The small pure helpers over mizu.h's wire-format conventions, dual form like the zc accessors above (...
Definition mizu_ext.h:1022
The 128-byte region header.
Definition mizu_ext.h:835
uint32_t out_type
template element wire type; 0 = no output
Definition mizu_ext.h:841
uint32_t version
MIZU_ABI_VERSION.
Definition mizu_ext.h:837
uint32_t claim_n
CLAIM word count (runner ordinal bound).
Definition mizu_ext.h:843
uint32_t out_elt
template element size, bytes
Definition mizu_ext.h:842
uint32_t magic
MIZU_MORSEL_MAGIC.
Definition mizu_ext.h:836
uint64_t state_off
morsel state section offset
Definition mizu_ext.h:851
uint64_t morsel_size
elements per morsel
Definition mizu_ext.h:849
uint64_t out_m
template length: values per element
Definition mizu_ext.h:848
uint32_t x_type
raw x section wire type (MIZU_TYPE_*)
Definition mizu_ext.h:840
uint32_t x_kind
MIZU_MORSEL_X_*.
Definition mizu_ext.h:839
uint64_t n
map elements
Definition mizu_ext.h:844
uint64_t n_morsels
ceiling(n / morsel_size)
Definition mizu_ext.h:850
uint32_t flags
reserved, 0
Definition mizu_ext.h:838
Batch-sizing state: process-private, never wire state, reset at each run's first-call CLAIM CAS.
Definition mizu_ext.h:879
uint64_t k_last
morsels issued last transition
Definition mizu_ext.h:883
double cost
est.
Definition mizu_ext.h:885
uint64_t k
current batch size, morsels
Definition mizu_ext.h:882
int32_t run_r
ordinal whose ramp this is (-1 = none)
Definition mizu_ext.h:880
int skip
last interval contained a help: no update
Definition mizu_ext.h:886
double t_last
mizu_now() at the last issue
Definition mizu_ext.h:884
Worker-death lost set: issued = [0, bound), lost = issued minus the union of the collected batch hist...
Definition mizu_ext.h:950
One parker per waiting entity: a 32-bit monotonic epoch word in the shared region plus,...
Definition mizu_ext.h:541
uint32_t * epoch
in the shared region
Definition mizu_ext.h:542
The opaque pool-signal trio a map runner loads relaxed once per batch transition: the help_wanted doo...
Definition mizu_ext.h:747
Definition mizu_ext.h:314
uint32_t flags
binding-set MIZU_READ_*; zero on entry
Definition mizu_ext.h:324
void * binding_ctx
the handle's binding.ctx
Definition mizu_ext.h:323
int32_t gone
set by mizu_read_region on a vanished region; read_fn propagates by returning NULL
Definition mizu_ext.h:318
int32_t died_slot
MIZU_RS_DIED: the claimant worker slot (-1).
Definition mizu_ext.h:320
int64_t died_pid
MIZU_RS_DIED: its pid (0 when unknown).
Definition mizu_ext.h:321
mizu_handle * handle
the reading handle
Definition mizu_ext.h:322
uint32_t size
core-set: sizeof the struct it knows
Definition mizu_ext.h:315
void * reserved[4]
zero; future growth without a soname bump
Definition mizu_ext.h:325
int32_t outcome
pool: mizu_rs_status of the result (channel: MIZU_RS_OK)
Definition mizu_ext.h:316
The result-sink struct: the core fills it from a claimed entry and hands it to exec_fn; the binding p...
Definition mizu_ext.h:960
unsigned char * payload
rs + sizeof(mizu_rs_hdr): the frame buffer
Definition mizu_ext.h:963
uint64_t seq
the claimed incarnation (the state word's sequence; the publish CAS expects RUNNING,...
Definition mizu_ext.h:967
uint32_t inline_max
the slot's payload capacity
Definition mizu_ext.h:965
uint16_t sub_slot
the task's submitter (zc keying, probes)
Definition mizu_ext.h:966
The public opaque type in mizu.h; defined here for bindings (the core's control-region mappings embed...
Definition mizu_ext.h:515
uint8_t collapsed
Linux: whole 2 MiB extents of addr are huge (set by a successful MADV_COLLAPSE; one-way latch — a sha...
Definition mizu_ext.h:521
unsigned int pid
creator PID: fork guard (POSIX only)
Definition mizu_ext.h:520
uint32_t kind
mizu_kind
Definition mizu.h:312