Skip to main content

nativeview_store/node/
row.rs

1//! Hot 32-byte per-node [`NodeRow`] header and flag bit layout, compile-time locked to
2//! [`nativeview_abi::NvNodeRow`] for zero-copy FFI publication.
3
4use crate::id::{NodeId, TableSet};
5use crate::span_pool::ChildSpan;
6use crate::table::TableSlot;
7
8/// Node is allocated (exported as `nativeview_abi::NV_NODE_ROW_FLAG_LIVE`).
9pub const LIVE: u8 = 1 << 0;
10/// Node is an inline descendant inside a paragraph root (exported as `nativeview_abi::NV_NODE_ROW_FLAG_INLINE`).
11pub const INLINE: u8 = 1 << 1;
12/// Node's child list changed during the current frame (already queued).
13pub(crate) const CHILDREN_DIRTY: u8 = 1 << 2;
14/// Live node is connected to [`NodeId::ROOT`] (valid only when `LIVE != 0`; shares bit 3 with
15/// `DEAD_MASK`, which is valid only when `LIVE == 0`).
16pub const CONNECTED: u8 = 1 << 3;
17/// 2-bit mutually exclusive dead-slot state mask (bits `3..=4`, valid only when `LIVE == 0`).
18pub(crate) const DEAD_MASK: u8 = 0b11 << 3;
19/// Dead slot queued in `pending_free` for end-of-frame recycling.
20pub(crate) const DEAD_PENDING_FREE: u8 = 1 << 3;
21/// Dead slot currently queued in the FIFO `free` list.
22pub(crate) const DEAD_IN_FREE: u8 = 2 << 3;
23/// Dead slot reached generation `255` and is permanently retired (prevents ABA wraparound).
24pub(crate) const DEAD_RETIRED: u8 = 3 << 3;
25/// Node was created during the current frame (cleared in `commit_into`, never crosses FFI).
26pub(crate) const CREATED: u8 = 1 << 5;
27/// Scratch flag used during `try_set_children` for O(K) zero-alloc duplicate detection.
28pub(crate) const CHILD_SEEN: u8 = 1 << 6;
29/// Scratch flag used during `try_set_children` for O(K + depth) zero-alloc cycle detection.
30pub(crate) const ANCESTOR_MARK: u8 = 1 << 7;
31
32const _: () = {
33    assert!(LIVE == nativeview_abi::NV_NODE_ROW_FLAG_LIVE);
34    assert!(INLINE == nativeview_abi::NV_NODE_ROW_FLAG_INLINE);
35    assert!((LIVE & INLINE) == 0);
36    assert!(
37        ((LIVE | INLINE) & (CHILDREN_DIRTY | CONNECTED | DEAD_MASK | CREATED | CHILD_SEEN | ANCESTOR_MARK))
38            == 0
39    );
40    assert!((CHILDREN_DIRTY & (CONNECTED | DEAD_MASK | CREATED | CHILD_SEEN | ANCESTOR_MARK)) == 0);
41    assert!((CONNECTED & (CREATED | CHILD_SEEN | ANCESTOR_MARK)) == 0);
42    assert!((DEAD_MASK & (CREATED | CHILD_SEEN | ANCESTOR_MARK)) == 0);
43    assert!((CREATED & (CHILD_SEEN | ANCESTOR_MARK)) == 0);
44    assert!((CHILD_SEEN & ANCESTOR_MARK) == 0);
45};
46
47/// Per-node hot record: exactly 32 bytes, 32-byte aligned (two per cache line, never straddling).
48#[repr(C, align(32))]
49#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
50pub struct NodeRow {
51    /// Bit `slot` set: table `slot` is attached.
52    pub tables: TableSet,
53    /// Parent `NodeId` (`NodeId::NULL` = detached / root).
54    pub parent: NodeId,
55    /// Ordered child list span in `NodeStore::children`.
56    pub children: ChildSpan,
57    pub generation: u8,
58    /// Highest-priority realizing [`TableSlot`] (`TableSlot::NONE` = plain container), see
59    /// [`crate::RealizationOrder`].
60    pub primary_slot: TableSlot,
61    /// Union of `nativeview_abi::table::effect` bits changed this frame.
62    pub effects: u8,
63    pub(crate) flags: u8,
64}
65
66impl NodeRow {
67    #[inline(always)]
68    pub const fn is_live(&self) -> bool {
69        (self.flags & LIVE) != 0
70    }
71
72    #[inline(always)]
73    pub const fn is_inline(&self) -> bool {
74        (self.flags & INLINE) != 0
75    }
76
77    #[inline(always)]
78    pub const fn is_connected(&self) -> bool {
79        (self.flags & (LIVE | CONNECTED)) == (LIVE | CONNECTED)
80    }
81}
82
83// Complete compile-time proof that `NodeRow` and `nativeview_abi::NvNodeRow` are bit-for-bit identical
84// in size, alignment compatibility, little-endian `[u32; 4]` bitset encoding, and EVERY field
85// offset (including `pub(crate)` `flags` and `ChildSpan::{off, len}`) for zero-copy C FFI export.
86const _: () = {
87    use core::mem::{align_of, offset_of, size_of};
88    use nativeview_abi::NvNodeRow;
89    assert!(cfg!(target_endian = "little"));
90    assert!(size_of::<NodeRow>() == 32);
91    assert!(align_of::<NodeRow>() == 32);
92    assert!(size_of::<NodeRow>() == size_of::<NvNodeRow>());
93    assert!(align_of::<NodeRow>() >= align_of::<NvNodeRow>());
94    assert!(size_of::<TableSet>() == size_of::<[u32; 4]>());
95    assert!(offset_of!(NodeRow, tables) == offset_of!(NvNodeRow, table_set));
96    assert!(offset_of!(NodeRow, parent) == offset_of!(NvNodeRow, parent));
97    assert!(
98        offset_of!(NodeRow, children) + offset_of!(ChildSpan, off)
99            == offset_of!(NvNodeRow, child_off)
100    );
101    assert!(
102        offset_of!(NodeRow, children) + offset_of!(ChildSpan, len)
103            == offset_of!(NvNodeRow, child_len)
104    );
105    assert!(offset_of!(NodeRow, generation) == offset_of!(NvNodeRow, generation));
106    assert!(offset_of!(NodeRow, primary_slot) == offset_of!(NvNodeRow, primary_slot));
107    assert!(offset_of!(NodeRow, effects) == offset_of!(NvNodeRow, effects));
108    assert!(offset_of!(NodeRow, flags) == offset_of!(NvNodeRow, flags));
109    assert!(LIVE == nativeview_abi::NV_NODE_ROW_FLAG_LIVE);
110    assert!(INLINE == nativeview_abi::NV_NODE_ROW_FLAG_INLINE);
111};