SEDSnet 4.2.0

A memory safe, no_std-capable networking stack with routing, discovery, reliability, and Rust/C/Python bindings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
//! A private, bounded packet arena. Live Rust objects are never relocated.
//! Buffers move through handles; a pinned view prevents movement until dropped.
//! Large buffers can be excluded from compaction. No allocation occurs in plain
//! store/read/pin/compact/drop operations after arena construction.
use crate::{TelemetryError, TelemetryResult, lock::RouterMutex};
use alloc::{sync::Arc, vec::Vec};
use core::{cell::UnsafeCell, fmt, ops::Deref, ptr};

fn pressure() -> TelemetryError {
    TelemetryError::Io("memory pressure")
}
#[derive(Clone, Copy, Default)]
struct Slot {
    offset: usize,
    len: usize,
    refs: usize,
    pins: usize,
    compressed: bool,
    next: Option<usize>,
    source: usize,
}
struct State {
    bytes: Vec<UnsafeCell<u8>>,
    slots: Vec<Slot>,
    movable_limit: usize,
    moves: usize,
    rejected: usize,
    head: Option<usize>,
}
/// Accounting includes the fixed arena and handle table, not just live payloads.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct StoreStats {
    pub capacity: usize,
    pub live_bytes: usize,
    pub largest_gap: usize,
    pub live_handles: usize,
    pub pinned_bytes: usize,
    pub moves: usize,
    pub rejected: usize,
    pub reserved_bytes: usize,
}
/// Thread-safe, fixed-capacity arena. Configure it before starting network workers.
/// Embedded access uses SEDSnet's existing telemetry_lock/unlock hooks: they must
/// support nesting, including calls made while a router lock is held.
pub struct PacketStore {
    state: RouterMutex<State>,
}
// All metadata and byte writes are serialized. Only immutable pinned byte views
// escape the lock; the corresponding slots cannot move or be overwritten.

impl fmt::Debug for PacketStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.stats().fmt(f)
    }
}
impl State {
    fn reserve(&mut self, len: usize, compressed: bool, source: usize) -> TelemetryResult<usize> {
        let Some(slot) = self.slots.iter().position(|s| s.refs == 0) else {
            self.rejected = self.rejected.wrapping_add(1);
            return Err(pressure());
        };
        let mut offset = self.gap(len);
        if offset.is_none() {
            self.compact();
            offset = self.gap(len);
        }
        let Some(offset) = offset else {
            self.rejected = self.rejected.wrapping_add(1);
            return Err(pressure());
        };
        let mut previous = None;
        let mut next = self.head;
        while let Some(i) = next {
            if self.slots[i].offset > offset {
                break;
            }
            previous = Some(i);
            next = self.slots[i].next;
        }
        self.slots[slot] = Slot {
            offset,
            len,
            refs: 1,
            pins: 0,
            compressed,
            next,
            source,
        };
        if let Some(i) = previous {
            self.slots[i].next = Some(slot);
        } else {
            self.head = Some(slot);
        }
        Ok(slot)
    }
    fn gap(&self, need: usize) -> Option<usize> {
        let mut end = 0;
        let mut cursor = self.head;
        while let Some(i) = cursor {
            let slot = self.slots[i];
            if slot.offset - end >= need {
                return Some(end);
            }
            end = slot.offset + slot.len;
            cursor = slot.next;
        }
        (self.bytes.len() - end >= need).then_some(end)
    }

    fn compact(&mut self) {
        let mut cursor = self.head;
        let mut end = 0;
        while let Some(i) = cursor {
            let s = self.slots[i];
            cursor = s.next;
            if s.pins == 0 && s.len <= self.movable_limit && s.offset != end {
                // SAFETY: destination is a free gap or this slot's old bytes.
                // Ascending traversal never crosses a preceding pinned slot.
                // UnsafeCell allows disjoint writes while another slot is borrowed.
                unsafe {
                    ptr::copy(
                        self.bytes.as_ptr().cast::<u8>().add(s.offset),
                        self.bytes.as_ptr().cast::<u8>().add(end).cast_mut(),
                        s.len,
                    );
                }
                self.slots[i].offset = end;
                self.moves = self.moves.wrapping_add(1);
            } else {
                end = s.offset;
            }
            end += s.len;
        }
    }
}
impl PacketStore {
    /// Reserve payload bytes and a fixed handle table once. `movable_limit`
    /// excludes larger allocations from compaction (usize::MAX moves all sizes).
    /// The returned Arc/control block is a startup allocation, not a hot-path one.
    pub fn new(
        capacity: usize,
        handles: usize,
        movable_limit: usize,
    ) -> TelemetryResult<Arc<Self>> {
        if capacity == 0 || handles == 0 {
            return Err(pressure());
        }
        let mut bytes = Vec::new();
        bytes.try_reserve_exact(capacity).map_err(|_| pressure())?;
        bytes.resize_with(capacity, || UnsafeCell::new(0));
        let mut slots = Vec::new();
        slots.try_reserve_exact(handles).map_err(|_| pressure())?;
        slots.resize(handles, Slot::default());
        let store = Arc::new(Self {
            state: RouterMutex::new(State {
                bytes,
                slots,
                movable_limit,
                moves: 0,
                rejected: 0,
                head: None,
            }),
        });
        // Some host mutex implementations allocate on first lock. Pay that
        // cost at startup, before the allocation-free arena operations.
        #[cfg(feature = "std")]
        drop(store.state.lock());
        Ok(store)
    }
    /// Copy a payload into the arena; compact automatically only if a contiguous
    /// gap is missing. Exhaustion returns an error, never evicts another handle.
    pub fn store(self: &Arc<Self>, bytes: &[u8]) -> TelemetryResult<StoredBytes> {
        self.insert(bytes, bytes.len(), false, 0)
    }
    /// Coalesce parking of shared Arc owners into one arena handle. The address
    /// is a hint only: content is compared so allocator address reuse cannot
    /// alias unrelated payloads. No Weak is retained (it would retain Arc storage).
    pub fn store_shared(self: &Arc<Self>, bytes: &Arc<[u8]>) -> TelemetryResult<StoredBytes> {
        self.insert(
            bytes,
            bytes.len(),
            false,
            Arc::as_ptr(bytes) as *const u8 as usize,
        )
    }
    fn insert(
        self: &Arc<Self>,
        bytes: &[u8],
        logical: usize,
        compressed: bool,
        source: usize,
    ) -> TelemetryResult<StoredBytes> {
        if bytes.is_empty() {
            return Ok(StoredBytes {
                store: self.clone(),
                slot: None,
                len: logical,
            });
        }
        let mut st = self.state.lock();
        if source != 0
            && let Some(i) = st.slots.iter().position(|s| {
                s.refs != 0
                    && !s.compressed
                    && s.source == source
                    && s.len == bytes.len()
                    && unsafe {
                        core::slice::from_raw_parts(
                            st.bytes.as_ptr().cast::<u8>().add(s.offset),
                            s.len,
                        )
                    } == bytes
            })
        {
            st.slots[i].refs = st.slots[i]
                .refs
                .checked_add(1)
                .expect("packet handle overflow");
            return Ok(StoredBytes {
                store: self.clone(),
                slot: Some(i),
                len: logical,
            });
        }
        let slot = st.reserve(bytes.len(), compressed, source)?;
        let offset = st.slots[slot].offset;
        unsafe {
            ptr::copy_nonoverlapping(
                bytes.as_ptr(),
                st.bytes.as_ptr().cast::<u8>().add(offset).cast_mut(),
                bytes.len(),
            );
        }
        Ok(StoredBytes {
            store: self.clone(),
            slot: Some(slot),
            len: logical,
        })
    }
    pub fn compact(&self) {
        self.state.lock().compact();
    }
    pub fn stats(&self) -> StoreStats {
        let st = self.state.lock();
        let mut end = 0;
        let mut largest = 0;
        let mut cursor = st.head;
        while let Some(i) = cursor {
            let s = st.slots[i];
            largest = largest.max(s.offset - end);
            end = s.offset + s.len;
            cursor = s.next;
        }
        largest = largest.max(st.bytes.len() - end);
        StoreStats {
            capacity: st.bytes.len(),
            live_bytes: st.slots.iter().filter(|s| s.refs != 0).map(|s| s.len).sum(),
            live_handles: st.slots.iter().filter(|s| s.refs != 0).count(),
            pinned_bytes: st
                .slots
                .iter()
                .filter(|s| s.refs != 0 && s.pins != 0)
                .map(|s| s.len)
                .sum(),
            largest_gap: largest,
            moves: st.moves,
            rejected: st.rejected,
            reserved_bytes: st.bytes.capacity()
                + st.slots.capacity() * core::mem::size_of::<Slot>(),
        }
    }
}
/// Shared ownership of one arena slot. Cloning allocates no payload/control block.
pub struct StoredBytes {
    store: Arc<PacketStore>,
    slot: Option<usize>,
    len: usize,
}
impl Clone for StoredBytes {
    fn clone(&self) -> Self {
        if let Some(i) = self.slot {
            let mut st = self.store.state.lock();
            st.slots[i].refs = st.slots[i]
                .refs
                .checked_add(1)
                .expect("packet handle overflow");
        }
        Self {
            store: self.store.clone(),
            slot: self.slot,
            len: self.len,
        }
    }
}
impl Drop for StoredBytes {
    fn drop(&mut self) {
        if let Some(i) = self.slot {
            let mut st = self.store.state.lock();
            st.slots[i].refs -= 1;
            if st.slots[i].refs == 0 {
                debug_assert_eq!(st.slots[i].pins, 0);
                if st.head == Some(i) {
                    st.head = st.slots[i].next;
                } else {
                    let mut cursor = st.head;
                    while let Some(previous) = cursor {
                        if st.slots[previous].next == Some(i) {
                            st.slots[previous].next = st.slots[i].next;
                            break;
                        }
                        cursor = st.slots[previous].next;
                    }
                }
                st.slots[i] = Slot::default();
            }
        }
    }
}
impl fmt::Debug for StoredBytes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StoredBytes")
            .field("len", &self.len)
            .field("stored_len", &self.stored_len())
            .finish()
    }
}
impl StoredBytes {
    pub fn len(&self) -> usize {
        self.len
    }
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }
    pub fn stored_len(&self) -> usize {
        self.slot
            .map_or(0, |i| self.store.state.lock().slots[i].len)
    }
    pub fn is_compressed(&self) -> bool {
        self.slot
            .is_some_and(|i| self.store.state.lock().slots[i].compressed)
    }
    /// A raw view is available only for plain payloads. Compressed field reads use
    /// read_range instead, so no second full-size decompressed buffer is retained.
    pub fn pin(&self) -> TelemetryResult<PinnedBytes> {
        if let Some(i) = self.slot {
            let mut st = self.store.state.lock();
            if st.slots[i].compressed {
                return Err(TelemetryError::Io(
                    "compressed payload requires range decoding",
                ));
            }
            st.slots[i].pins = st.slots[i]
                .pins
                .checked_add(1)
                .expect("packet pin overflow");
        }
        Ok(PinnedBytes {
            handle: self.clone(),
        })
    }
    #[cfg(feature = "compact-packet-compression")]
    fn pin_encoded(&self) -> PinnedBytes {
        if let Some(i) = self.slot {
            let mut st = self.store.state.lock();
            st.slots[i].pins = st.slots[i]
                .pins
                .checked_add(1)
                .expect("packet pin overflow");
        }
        PinnedBytes {
            handle: self.clone(),
        }
    }
    /// Read a plain byte range into caller storage, without allocating.
    pub fn read_range(&self, offset: usize, out: &mut [u8]) -> TelemetryResult<()> {
        if offset
            .checked_add(out.len())
            .is_none_or(|end| end > self.len)
        {
            return Err(TelemetryError::Unpack("payload range out of bounds"));
        }
        let view = self.pin()?;
        out.copy_from_slice(&view[offset..offset + out.len()]);
        Ok(())
    }
}
/// Immutable view. Its allocation cannot move, be reused, or disappear until drop.
/// Owned views may be kept for synchronous FFI or asynchronous DMA, but the view
/// must outlive all raw-pointer consumers (and be dropped outside an ISR unless
/// the configured lock hooks permit it).
pub struct PinnedBytes {
    handle: StoredBytes,
}
impl Deref for PinnedBytes {
    type Target = [u8];
    fn deref(&self) -> &[u8] {
        let Some(i) = self.handle.slot else {
            return &[];
        };
        let st = self.handle.store.state.lock();
        let s = st.slots[i];
        unsafe { core::slice::from_raw_parts(st.bytes.as_ptr().cast::<u8>().add(s.offset), s.len) }
    }
}
impl Drop for PinnedBytes {
    fn drop(&mut self) {
        if let Some(i) = self.handle.slot {
            self.handle.store.state.lock().slots[i].pins -= 1;
        }
    }
}

// Optional process-wide arena for queue parking. Replacing it affects new
// parking only: old handles retain their original arena until dropped.
static DEFAULT_STORE: RouterMutex<Option<Arc<PacketStore>>> = RouterMutex::constant(None);
pub fn set_default_store(store: Option<Arc<PacketStore>>) {
    *DEFAULT_STORE.lock() = store;
}
pub(crate) fn default_store() -> Option<Arc<PacketStore>> {
    DEFAULT_STORE.lock().clone()
}

/// An ordinary slice API pins on first access. Exclusive queue parking releases
/// that pin. Concurrent readers can never invalidate each other's references.
pub struct ParkedPayload {
    handle: StoredBytes,
    view: RouterMutex<Option<PinnedBytes>>,
}
impl ParkedPayload {
    pub(crate) fn new(handle: StoredBytes) -> Self {
        assert!(!handle.is_compressed());
        Self {
            handle,
            view: RouterMutex::new(None),
        }
    }
    pub fn len(&self) -> usize {
        self.handle.len()
    }
    pub fn is_empty(&self) -> bool {
        self.handle.is_empty()
    }
    pub fn as_slice(&self) -> &[u8] {
        let mut view = self.view.lock();
        if view.is_none() {
            *view = Some(self.handle.pin().expect("plain parked payload"));
        }
        let bytes = view.as_ref().unwrap().deref();
        let pointer = bytes.as_ptr();
        let len = bytes.len();
        // Pin remains in self until an exclusive &mut self releases it or self is
        // dropped, so the returned borrow cannot overlap unpinning.
        unsafe { core::slice::from_raw_parts(pointer, len) }
    }
    pub fn park(&mut self) {
        *self.view.lock() = None;
    }
    pub fn read_range(&self, offset: usize, out: &mut [u8]) -> TelemetryResult<()> {
        self.handle.read_range(offset, out)
    }
}
impl Clone for ParkedPayload {
    fn clone(&self) -> Self {
        Self::new(self.handle.clone())
    }
}
impl fmt::Debug for ParkedPayload {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.handle.fmt(f)
    }
}

#[cfg(feature = "compact-packet-compression")]
mod compression {
    use super::*;
    use zstd_safe::zstd_sys as sys;

    fn zstd_result(n: usize) -> TelemetryResult<usize> {
        // SAFETY: error inspection has no pointer arguments.
        if unsafe { sys::ZSTD_isError(n) } != 0 {
            Err(TelemetryError::Io("fixed codec workspace exhausted"))
        } else {
            Ok(n)
        }
    }
    fn aligned_size(bytes: usize) -> TelemetryResult<usize> {
        bytes.checked_add(7).map(|n| n & !7).ok_or_else(pressure)
    }
    fn words(bytes: usize) -> TelemetryResult<Vec<u64>> {
        let mut v = Vec::new();
        let count = bytes / 8;
        v.try_reserve_exact(count).map_err(|_| pressure())?;
        v.resize(count, 0);
        Ok(v)
    }
    fn buffer(bytes: usize) -> TelemetryResult<Vec<u8>> {
        let mut v = Vec::new();
        v.try_reserve_exact(bytes).map_err(|_| pressure())?;
        v.resize(bytes, 0);
        Ok(v)
    }

    /// Preflight reservation for one bounded codec. Excludes packet arena bytes
    /// and caller field output; includes context buffers, scratch and this object.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub struct CodecMemory {
        pub workspace_bytes: usize,
        pub encode_scratch_bytes: usize,
        pub decode_scratch_bytes: usize,
        pub total_bytes: usize,
    }

    /// Fixed Zstd contexts. Zstd cannot malloc or resize these static workspaces.
    /// Vec<u64> guarantees the required alignment; the allocation is never resized.
    /// Pointers are derived per call, so moving the Rust owner is safe. No Zstd
    /// free function is called for static contexts; Vec owns their lifetime.
    pub struct PacketCodec {
        encoder: Vec<u64>,
        decoder: Vec<u64>,
        chunk: usize,
        decoded_chunks: usize,
    }
    impl PacketCodec {
        fn sizes(chunk: usize) -> TelemetryResult<(usize, usize)> {
            if chunk == 0 || chunk > u32::MAX as usize {
                return Err(TelemetryError::Io("invalid compression chunk size"));
            }
            // SAFETY: Zstd's estimators take values only. Use exactly the same
            // compression parameters as the static context below, no dictionary.
            let params = unsafe { sys::ZSTD_getCParams(1, chunk as u64, 0) };
            let encoder = zstd_result(unsafe { sys::ZSTD_estimateCCtxSize_usingCParams(params) })?;
            let decoder = zstd_result(unsafe { sys::ZSTD_estimateDCtxSize() })?;
            Ok((aligned_size(encoder)?, aligned_size(decoder)?))
        }
        pub fn memory_requirements(chunk: usize) -> TelemetryResult<CodecMemory> {
            let (encoder, decoder) = Self::sizes(chunk)?;
            let workspace_bytes = encoder.checked_add(decoder).ok_or_else(pressure)?;
            let encode_scratch_bytes = zstd_safe::compress_bound(chunk);
            let decode_scratch_bytes = chunk;
            let total_bytes = workspace_bytes
                .checked_add(encode_scratch_bytes)
                .and_then(|n| n.checked_add(decode_scratch_bytes))
                .and_then(|n| n.checked_add(core::mem::size_of::<BoundedPacketCodec>()))
                .ok_or_else(pressure)?;
            Ok(CodecMemory {
                workspace_bytes,
                encode_scratch_bytes,
                decode_scratch_bytes,
                total_bytes,
            })
        }
        /// Reject oversized context reservations before allocating. All codec
        /// allocations happen here; subsequent operations never grow workspace.
        pub fn new(chunk: usize, workspace_budget: usize) -> TelemetryResult<Self> {
            let (enc, dec) = Self::sizes(chunk)?;
            let workspace = enc.checked_add(dec).ok_or_else(pressure)?;
            if workspace > workspace_budget {
                return Err(TelemetryError::Io("codec workspace exceeds budget"));
            }
            let mut encoder = words(enc)?;
            let mut decoder = words(dec)?;
            // SAFETY: both buffers are 8-byte aligned, correctly sized, and owned
            // until codec drop. Static initialization never allocates internally.
            let c = unsafe { sys::ZSTD_initStaticCCtx(encoder.as_mut_ptr().cast(), enc) };
            let d = unsafe { sys::ZSTD_initStaticDCtx(decoder.as_mut_ptr().cast(), dec) };
            if c.is_null() || d.is_null() {
                return Err(pressure());
            }
            let params = unsafe { sys::ZSTD_getCParams(1, chunk as u64, 0) };
            zstd_result(unsafe { sys::ZSTD_CCtx_setCParams(c, params) })?;
            let codec = Self {
                encoder,
                decoder,
                chunk,
                decoded_chunks: 0,
            };
            if codec.workspace_bytes() > workspace_budget {
                return Err(pressure());
            }
            Ok(codec)
        }
        /// The fixed allocated capacity, rather than an estimate of live Zstd use.
        pub fn workspace_bytes(&self) -> usize {
            (self.encoder.capacity() + self.decoder.capacity()) * 8
        }
        pub fn decoded_chunks(&self) -> usize {
            self.decoded_chunks
        }
        pub fn primed_workspace_bytes(&self) -> usize {
            self.workspace_bytes()
        }
        pub fn chunk_bytes(&self) -> usize {
            self.chunk
        }
        fn encode_chunk(&mut self, raw: &[u8], scratch: &mut [u8]) -> TelemetryResult<usize> {
            if scratch.len() < raw.len() {
                return Err(TelemetryError::Io("encode scratch too small"));
            }
            // SAFETY: initialized static context; input/output are disjoint valid
            // slices. &mut self serializes context use and the buffers never move.
            let n = unsafe {
                sys::ZSTD_compress2(
                    self.encoder.as_mut_ptr().cast(),
                    scratch.as_mut_ptr().cast(),
                    scratch.len(),
                    raw.as_ptr().cast(),
                    raw.len(),
                )
            };
            if let Ok(n) = zstd_result(n)
                && n < raw.len()
            {
                return Ok(n);
            }
            scratch[..raw.len()].copy_from_slice(raw);
            Ok(raw.len())
        }
        /// Two-pass encoding uses only chunk-sized scratch. First measure the
        /// representation; then reserve exactly that many arena bytes and write
        /// directly into its private slot. No full encoded temporary is retained.
        /// Encoding errors or insufficient scratch fall back to plain storage.
        pub fn store(
            &mut self,
            store: &Arc<PacketStore>,
            data: &[u8],
            scratch: &mut [u8],
        ) -> TelemetryResult<StoredBytes> {
            if data.is_empty() || scratch.len() < self.chunk.min(data.len()) {
                return store.store(data);
            }
            let mut used = 0usize;
            for raw in data.chunks(self.chunk) {
                let n = self.encode_chunk(raw, scratch)?;
                used = match used.checked_add(8).and_then(|x| x.checked_add(n)) {
                    Some(n) if n < data.len() => n,
                    _ => return store.store(data),
                };
            }
            let slot = store.state.lock().reserve(used, true, 0)?;
            let handle = StoredBytes {
                store: store.clone(),
                slot: Some(slot),
                len: data.len(),
            };
            let result = (|| {
                let st = store.state.lock();
                let offset = st.slots[slot].offset;
                let mut cursor = 0usize;
                for raw in data.chunks(self.chunk) {
                    let n = self.encode_chunk(raw, scratch)?;
                    let end = cursor
                        .checked_add(8)
                        .and_then(|x| x.checked_add(n))
                        .filter(|&x| x <= used)
                        .ok_or(TelemetryError::Io("codec size changed"))?;
                    let raw_len = (raw.len() as u32).to_le_bytes();
                    let stored_len = (n as u32).to_le_bytes();
                    // SAFETY: this slot is private, unpinned and lock-protected;
                    // every write is checked within its exact reserved length.
                    unsafe {
                        let dst = st
                            .bytes
                            .as_ptr()
                            .cast::<u8>()
                            .add(offset + cursor)
                            .cast_mut();
                        ptr::copy_nonoverlapping(raw_len.as_ptr(), dst, 4);
                        ptr::copy_nonoverlapping(stored_len.as_ptr(), dst.add(4), 4);
                        ptr::copy_nonoverlapping(scratch.as_ptr(), dst.add(8), n);
                    }
                    cursor = end;
                }
                if cursor != used {
                    return Err(TelemetryError::Io("codec size changed"));
                }
                Ok(())
            })();
            // Drop/rollback happens only after the arena lock above is released.
            result?;
            Ok(handle)
        }
        /// Decode only chunks intersecting this field/range. Decoder scratch is
        /// at most chunk bytes; unrelated chunks are skipped without decoding.
        pub fn read_range(
            &mut self,
            handle: &StoredBytes,
            offset: usize,
            out: &mut [u8],
            scratch: &mut [u8],
        ) -> TelemetryResult<()> {
            let end = offset
                .checked_add(out.len())
                .filter(|&n| n <= handle.len())
                .ok_or(TelemetryError::Unpack("payload range out of bounds"))?;
            if !handle.is_compressed() {
                return handle.read_range(offset, out);
            }
            if out.is_empty() {
                return Ok(());
            }
            let view = handle.pin_encoded();
            let mut cursor = 0;
            let mut logical = 0;
            while logical < end {
                let header = view
                    .get(cursor..cursor + 8)
                    .ok_or(TelemetryError::Unpack("invalid stored chunk"))?;
                let raw = u32::from_le_bytes(header[..4].try_into().unwrap()) as usize;
                let n = u32::from_le_bytes(header[4..].try_into().unwrap()) as usize;
                cursor += 8;
                let bytes = view
                    .get(cursor..cursor + n)
                    .ok_or(TelemetryError::Unpack("invalid stored chunk"))?;
                if raw == 0 || raw > self.chunk || n > raw || n == 0 {
                    return Err(TelemetryError::Unpack("invalid stored chunk size"));
                }
                if logical + raw > offset {
                    let decoded = if n == raw {
                        bytes
                    } else {
                        if scratch.len() < raw {
                            return Err(TelemetryError::Io("field decode scratch too small"));
                        }
                        // SAFETY: fixed initialized decoder and valid disjoint slices.
                        let written = zstd_result(unsafe {
                            sys::ZSTD_decompressDCtx(
                                self.decoder.as_mut_ptr().cast(),
                                scratch.as_mut_ptr().cast(),
                                raw,
                                bytes.as_ptr().cast(),
                                bytes.len(),
                            )
                        })
                        .map_err(|_| TelemetryError::Unpack("stored chunk decode failed"))?;
                        if written != raw {
                            return Err(TelemetryError::Unpack("stored chunk size mismatch"));
                        }
                        self.decoded_chunks = self.decoded_chunks.wrapping_add(1);
                        &scratch[..raw]
                    };
                    let from = offset.max(logical);
                    let to = end.min(logical + raw);
                    out[from - offset..to - offset]
                        .copy_from_slice(&decoded[from - logical..to - logical]);
                }
                cursor += n;
                logical += raw;
            }
            Ok(())
        }
    }
    /// Owns both reusable scratch buffers and enforces one preflight RAM budget.
    /// The budget covers fixed buffers and object size, excluding allocator bookkeeping.
    pub struct BoundedPacketCodec {
        codec: PacketCodec,
        encode: Vec<u8>,
        decode: Vec<u8>,
    }
    impl BoundedPacketCodec {
        pub fn new(chunk: usize, memory_budget: usize) -> TelemetryResult<Self> {
            let required = PacketCodec::memory_requirements(chunk)?;
            if required.total_bytes > memory_budget {
                return Err(TelemetryError::Io("codec memory exceeds budget"));
            }
            let codec = PacketCodec::new(chunk, required.workspace_bytes)?;
            let encode = buffer(required.encode_scratch_bytes)?;
            let decode = buffer(required.decode_scratch_bytes)?;
            let result = Self {
                codec,
                encode,
                decode,
            };
            if result.reserved_bytes() > memory_budget {
                return Err(pressure());
            }
            Ok(result)
        }
        pub fn reserved_bytes(&self) -> usize {
            self.codec.workspace_bytes()
                + self.encode.capacity()
                + self.decode.capacity()
                + core::mem::size_of::<Self>()
        }
        pub fn decoded_chunks(&self) -> usize {
            self.codec.decoded_chunks()
        }
        pub fn store(
            &mut self,
            store: &Arc<PacketStore>,
            data: &[u8],
        ) -> TelemetryResult<StoredBytes> {
            self.codec.store(store, data, &mut self.encode)
        }
        pub fn read_range(
            &mut self,
            handle: &StoredBytes,
            offset: usize,
            out: &mut [u8],
        ) -> TelemetryResult<()> {
            self.codec.read_range(handle, offset, out, &mut self.decode)
        }
    }
}
#[cfg(feature = "compact-packet-compression")]
pub use compression::{BoundedPacketCodec, CodecMemory, PacketCodec};

/// Configure the default arena before worker startup. A zero capacity disables
/// parking for new items; existing handles retain their old arena safely.
/// Status matches SEDS_OK (0) / SEDS_IO (-14).
#[unsafe(no_mangle)]
pub extern "C" fn seds_packet_store_configure(
    capacity: usize,
    handles: usize,
    movable_limit: usize,
) -> i32 {
    if capacity == 0 {
        set_default_store(None);
        return 0;
    }
    match PacketStore::new(capacity, handles, movable_limit) {
        Ok(store) => {
            set_default_store(Some(store));
            0
        }
        Err(_) => crate::TelemetryErrorCode::Io as i32,
    }
}
#[unsafe(no_mangle)]
pub extern "C" fn seds_packet_store_compact() {
    if let Some(store) = default_store() {
        store.compact();
    }
}

/// Copy statistics for the current default arena. No allocation.
/// # Safety
/// out must be valid and aligned for one writable StoreStats, or null.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn seds_packet_store_stats(out: *mut StoreStats) -> i32 {
    if out.is_null() {
        return crate::TelemetryErrorCode::BadArg as i32;
    }
    let Some(store) = default_store() else {
        return crate::TelemetryErrorCode::Io as i32;
    };
    unsafe {
        out.write(store.stats());
    }
    0
}

#[cfg(all(test, feature = "std"))]
mod shared_address_tests {
    use super::*;
    #[test]
    fn arena_pressure_does_not_prevent_retained_work_from_draining() {
        let store = PacketStore::new(16, 1, 16).unwrap();
        let queued = store.store(&[7; 16]).unwrap();
        // Background admission must yield to dispatch, not strand the only
        // handle that can free space for subsequent packets.
        let background = store.store(&[8; 16]).map(|_| ());
        assert!(background.is_err());
        crate::memory_admission::allow_drain(background).unwrap();
        assert_eq!(&*queued.pin().unwrap(), &[7; 16]);
        drop(queued);
        assert!(store.store(&[8; 16]).is_ok());
    }
    #[test]
    fn reused_source_address_cannot_alias_different_content() {
        let store = PacketStore::new(256, 8, 256).unwrap();
        let first: Arc<[u8]> = Arc::from([1; 80]);
        let a = store.store_shared(&first).unwrap();
        drop(first);
        let second: Arc<[u8]> = Arc::from([2; 80]);
        // Model allocator address reuse deterministically, rather than rely on
        // the host allocator to choose the same address during this test.
        store.state.lock().slots[a.slot.unwrap()].source =
            Arc::as_ptr(&second) as *const u8 as usize;
        let b = store.store_shared(&second).unwrap();
        assert_eq!(&*a.pin().unwrap(), &[1; 80]);
        assert_eq!(&*b.pin().unwrap(), &[2; 80]);
        assert_eq!(store.stats().live_handles, 2);
    }
}