limen-core 0.1.0-alpha.1

Limen core contracts and primitives.
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
//! Fixed-capacity, `no_std`, `no_alloc` memory manager.
//!
//! Feature `checked-memory-manager-refs` enables per-slot runtime borrow-state
//! checking and guard types that increment/decrement the borrow counter.
//!
//! Default (no feature): plain references are returned: zero-cost for MCU.

use crate::errors::MemoryError;
use crate::memory::header_store::HeaderStore;
use crate::memory::manager::MemoryManager;
use crate::memory::MemoryClass;
use crate::message::payload::Payload;
use crate::message::{Message, MessageHeader};
use crate::types::MessageToken;

#[cfg(feature = "checked-memory-manager-refs")]
use core::cell::Cell;

#[cfg(feature = "checked-memory-manager-refs")]
use core::ops::{Deref, DerefMut};

/// A fixed-capacity memory manager backed by `[Option<Message<P>>; DEPTH]`.
///
/// Behavior depends on the `checked-memory-manager-refs` feature:
///
/// - **Feature disabled (default)**: guard associated types are plain references:
///   `&MessageHeader`, `&Message<P>`, `&mut Message<P>` — zero-cost, suitable
///   for MCU/no-alloc builds.
/// - **Feature enabled**: per-slot borrow counters are kept (u16). `peek_header`,
///   `read`, `read_mut` return small guard types that update the per-slot
///   borrow counter and restore it on `Drop`. Conflicts return explicit
///   `MemoryError` values.
pub struct StaticMemoryManager<P: Payload, const DEPTH: usize> {
    slots: [Option<Message<P>>; DEPTH],
    #[cfg(feature = "checked-memory-manager-refs")]
    borrow_states: [Cell<u16>; DEPTH], // u16 is small and OK for typical DEPTH <= 65535
    mem_class: MemoryClass,
}

impl<P: Payload, const DEPTH: usize> StaticMemoryManager<P, DEPTH> {
    /// Create a new manager with all slots empty.
    ///
    /// `memory_class` defaults to [`MemoryClass::Host`].
    pub fn new() -> Self {
        Self {
            slots: core::array::from_fn(|_| None),
            #[cfg(feature = "checked-memory-manager-refs")]
            borrow_states: core::array::from_fn(|_| Cell::new(0)),
            mem_class: MemoryClass::Host,
        }
    }

    /// Create a new manager with a specific memory class.
    pub fn with_memory_class(mem_class: MemoryClass) -> Self {
        Self {
            slots: core::array::from_fn(|_| None),
            #[cfg(feature = "checked-memory-manager-refs")]
            borrow_states: core::array::from_fn(|_| Cell::new(0)),
            mem_class,
        }
    }
}

impl<P: Payload, const DEPTH: usize> Default for StaticMemoryManager<P, DEPTH> {
    fn default() -> Self {
        Self::new()
    }
}

//
// ===== Implementation when checked refs are disabled (zero-cost path) =====
//

#[cfg(not(feature = "checked-memory-manager-refs"))]
mod unchecked {
    use super::*;

    impl<P: Payload, const DEPTH: usize> HeaderStore for StaticMemoryManager<P, DEPTH> {
        type HeaderGuard<'a>
            = &'a MessageHeader
        where
            Self: 'a;

        fn peek_header(&self, token: MessageToken) -> Result<Self::HeaderGuard<'_>, MemoryError> {
            let idx = token.index();
            if idx >= DEPTH {
                return Err(MemoryError::BadToken);
            }
            // Per tests/contract: treat unallocated slot as BadToken for read/peek.
            self.slots[idx]
                .as_ref()
                .map(|msg| msg.header())
                .ok_or(MemoryError::BadToken)
        }
    }

    impl<P: Payload, const DEPTH: usize> MemoryManager<P> for StaticMemoryManager<P, DEPTH> {
        type ReadGuard<'a>
            = &'a Message<P>
        where
            Self: 'a;

        type WriteGuard<'a>
            = &'a mut Message<P>
        where
            Self: 'a;

        fn store(&mut self, value: Message<P>) -> Result<MessageToken, MemoryError> {
            for (i, slot) in self.slots.iter_mut().enumerate() {
                if slot.is_none() {
                    *slot = Some(value);
                    return Ok(MessageToken::new(i as u32));
                }
            }
            Err(MemoryError::NoFreeSlots)
        }

        fn read(&self, token: MessageToken) -> Result<Self::ReadGuard<'_>, MemoryError> {
            let idx = token.index();
            if idx >= DEPTH {
                return Err(MemoryError::BadToken);
            }
            // Per tests/contract: unallocated slot -> BadToken
            self.slots[idx].as_ref().ok_or(MemoryError::BadToken)
        }

        fn read_mut(&mut self, token: MessageToken) -> Result<Self::WriteGuard<'_>, MemoryError> {
            let idx = token.index();
            if idx >= DEPTH {
                return Err(MemoryError::BadToken);
            }
            // unallocated slot -> BadToken (consistent with read)
            self.slots[idx].as_mut().ok_or(MemoryError::BadToken)
        }

        fn free(&mut self, token: MessageToken) -> Result<(), MemoryError> {
            let idx = token.index();
            if idx >= DEPTH {
                return Err(MemoryError::BadToken);
            }
            if self.slots[idx].is_none() {
                // Freeing an already-empty slot is NotAllocated per tests.
                return Err(MemoryError::NotAllocated);
            }
            self.slots[idx] = None;
            Ok(())
        }

        fn available(&self) -> usize {
            self.slots.iter().filter(|s| s.is_none()).count()
        }

        fn capacity(&self) -> usize {
            DEPTH
        }

        fn memory_class(&self) -> MemoryClass {
            self.mem_class
        }
    }
}

//
// ===== Implementation when checked refs are enabled =====
//

#[cfg(feature = "checked-memory-manager-refs")]
mod checked {
    use super::*;

    // Borrow-state representation
    //
    // Convention:
    // - 0 => unborrowed
    // - 1..=u16::MAX - 1 => read borrow count
    // - u16::MAX => write borrowed (exclusive)
    const WRITE_BORROW_MARK: u16 = u16::MAX;

    fn try_increment_read(cell: &Cell<u16>) -> Result<(), MemoryError> {
        let value = cell.get();
        if value == WRITE_BORROW_MARK {
            return Err(MemoryError::AlreadyBorrowed);
        }
        if value == WRITE_BORROW_MARK - 1 {
            return Err(MemoryError::AlreadyBorrowed);
        }
        cell.set(value + 1);
        Ok(())
    }

    fn decrement_read(cell: &Cell<u16>) {
        let v = cell.get();
        if v == 0 || v == WRITE_BORROW_MARK {
            // Recover to zero defensively
            cell.set(0);
        } else {
            cell.set(v - 1);
        }
    }

    fn try_set_write(cell: &Cell<u16>) -> Result<(), MemoryError> {
        if cell.get() != 0 {
            return Err(MemoryError::AlreadyBorrowed);
        }
        cell.set(WRITE_BORROW_MARK);
        Ok(())
    }

    fn clear_write(cell: &Cell<u16>) {
        cell.set(0);
    }

    // Header guard
    pub struct StaticHeaderGuard<'a> {
        header: &'a MessageHeader,
        borrow_state: &'a Cell<u16>,
    }

    impl<'a> Deref for StaticHeaderGuard<'a> {
        type Target = MessageHeader;
        fn deref(&self) -> &Self::Target {
            self.header
        }
    }

    impl<'a> Drop for StaticHeaderGuard<'a> {
        fn drop(&mut self) {
            decrement_read(self.borrow_state);
        }
    }

    // Read guard
    pub struct StaticReadGuard<'a, P: Payload> {
        msg: &'a Message<P>,
        borrow_state: &'a Cell<u16>,
    }

    impl<'a, P: Payload> Deref for StaticReadGuard<'a, P> {
        type Target = Message<P>;
        fn deref(&self) -> &Self::Target {
            self.msg
        }
    }

    impl<'a, P: Payload> Drop for StaticReadGuard<'a, P> {
        fn drop(&mut self) {
            decrement_read(self.borrow_state);
        }
    }

    // Write guard
    pub struct StaticWriteGuard<'a, P: Payload> {
        msg: &'a mut Message<P>,
        borrow_state: &'a Cell<u16>,
    }

    impl<'a, P: Payload> Deref for StaticWriteGuard<'a, P> {
        type Target = Message<P>;
        fn deref(&self) -> &Self::Target {
            self.msg
        }
    }

    impl<'a, P: Payload> DerefMut for StaticWriteGuard<'a, P> {
        fn deref_mut(&mut self) -> &mut Self::Target {
            self.msg
        }
    }

    impl<'a, P: Payload> Drop for StaticWriteGuard<'a, P> {
        fn drop(&mut self) {
            clear_write(self.borrow_state);
        }
    }

    impl<P: Payload, const DEPTH: usize> HeaderStore for StaticMemoryManager<P, DEPTH> {
        type HeaderGuard<'a>
            = StaticHeaderGuard<'a>
        where
            Self: 'a;

        fn peek_header(&self, token: MessageToken) -> Result<Self::HeaderGuard<'_>, MemoryError> {
            let idx = token.index();
            if idx >= DEPTH {
                return Err(MemoryError::BadToken);
            }

            match self.slots[idx].as_ref() {
                Some(msg) => {
                    try_increment_read(&self.borrow_states[idx])?;
                    Ok(StaticHeaderGuard {
                        header: msg.header(),
                        borrow_state: &self.borrow_states[idx],
                    })
                }
                None => Err(MemoryError::BadToken),
            }
        }
    }

    impl<P: Payload, const DEPTH: usize> MemoryManager<P> for StaticMemoryManager<P, DEPTH> {
        type ReadGuard<'a>
            = StaticReadGuard<'a, P>
        where
            Self: 'a;

        type WriteGuard<'a>
            = StaticWriteGuard<'a, P>
        where
            Self: 'a;

        fn store(&mut self, value: Message<P>) -> Result<MessageToken, MemoryError> {
            for (i, slot) in self.slots.iter_mut().enumerate() {
                if slot.is_none() {
                    slot.replace(value);
                    // ensure borrow state's cleared
                    self.borrow_states[i].set(0);
                    return Ok(MessageToken::new(i as u32));
                }
            }
            Err(MemoryError::NoFreeSlots)
        }

        fn read(&self, token: MessageToken) -> Result<Self::ReadGuard<'_>, MemoryError> {
            let idx = token.index();
            if idx >= DEPTH {
                return Err(MemoryError::BadToken);
            }

            match self.slots[idx].as_ref() {
                Some(msg) => {
                    try_increment_read(&self.borrow_states[idx])?;
                    Ok(StaticReadGuard {
                        msg,
                        borrow_state: &self.borrow_states[idx],
                    })
                }
                None => Err(MemoryError::BadToken),
            }
        }

        fn read_mut(&mut self, token: MessageToken) -> Result<Self::WriteGuard<'_>, MemoryError> {
            let idx = token.index();
            if idx >= DEPTH {
                return Err(MemoryError::BadToken);
            }

            match self.slots[idx].as_mut() {
                Some(msg) => {
                    try_set_write(&self.borrow_states[idx])?;
                    Ok(StaticWriteGuard {
                        msg,
                        borrow_state: &self.borrow_states[idx],
                    })
                }
                None => Err(MemoryError::BadToken),
            }
        }

        fn free(&mut self, token: MessageToken) -> Result<(), MemoryError> {
            let idx = token.index();
            if idx >= DEPTH {
                return Err(MemoryError::BadToken);
            }

            if self.slots[idx].is_none() {
                return Err(MemoryError::NotAllocated);
            }

            let state = self.borrow_states[idx].get();
            if state != 0 {
                return Err(MemoryError::BorrowActive);
            }

            self.slots[idx] = None;
            self.borrow_states[idx].set(0);
            Ok(())
        }

        fn available(&self) -> usize {
            self.slots.iter().filter(|s| s.is_none()).count()
        }

        fn capacity(&self) -> usize {
            DEPTH
        }

        fn memory_class(&self) -> MemoryClass {
            self.mem_class
        }
    }
}

#[cfg(test)]
mod tests {
    // ---------------------------------------------------------------------------
    // Planned memory manager tests — NOT YET IMPLEMENTED
    //
    // C1 / tensor types (planned/C1.md):
    //   - run_store_retrieve_tensor_payload: store a TensorPayload (GPU buffer
    //     descriptor + shape), verify peek_header returns the correct byte count
    //     and memory_class == MemoryClass::Device(0).
    //   - run_free_device_tensor_does_not_double_free: allocate two tensor tokens,
    //     free the first, verify the second is still valid.
    //
    // P1 (PlatformBackend — planned/P1.md):
    //   If the manager gains a device-side allocator backend:
    //   - run_store_on_device_backend_respects_memory_class: store with
    //     MemoryClass::Device(0) and verify the returned token's header reports
    //     the correct class.
    //
    // RS1 (runtime lifecycle — planned/RS1.md):
    //   - run_reset_frees_all_slots: after a runtime reset, all manager slots
    //     must be free and used_slots() must return 0.
    // ---------------------------------------------------------------------------

    use super::*;
    use crate::{
        message::MessageHeader,
        prelude::{create_test_tensor_filled_with, TestTensor, TEST_TENSOR_BYTE_COUNT},
    };

    // Helper: build a simple Message<TestTensor>.
    fn make_msg(val: u32) -> Message<TestTensor> {
        Message::new(MessageHeader::empty(), create_test_tensor_filled_with(val))
    }

    #[test]
    fn store_read_free_cycle() {
        let mut mgr: StaticMemoryManager<TestTensor, 4> = StaticMemoryManager::new();
        assert_eq!(mgr.available(), 4);
        assert_eq!(mgr.capacity(), 4);

        let token = mgr.store(make_msg(42)).unwrap();
        assert_eq!(mgr.available(), 3);

        {
            let msg = mgr.read(token).unwrap();
            assert_eq!(*msg.payload(), create_test_tensor_filled_with(42));
        }

        mgr.free(token).unwrap();
        assert_eq!(mgr.available(), 4);
    }

    #[test]
    fn read_mut_works() {
        let mut mgr: StaticMemoryManager<TestTensor, 4> = StaticMemoryManager::new();
        let token = mgr.store(make_msg(10)).unwrap();

        {
            let mut write_guard = mgr.read_mut(token).unwrap();
            let msg = core::ops::DerefMut::deref_mut(&mut write_guard);
            *msg.payload_mut() = create_test_tensor_filled_with(99);
        }

        {
            let msg = mgr.read(token).unwrap();
            assert_eq!(*msg.payload(), create_test_tensor_filled_with(99));
        }

        mgr.free(token).unwrap();
    }

    #[test]
    fn peek_header_works() {
        let mut mgr: StaticMemoryManager<TestTensor, 4> = StaticMemoryManager::new();
        let token = mgr.store(make_msg(7)).unwrap();

        {
            let header = mgr.peek_header(token).unwrap();
            assert_eq!(*header.payload_size_bytes(), TEST_TENSOR_BYTE_COUNT);
        }

        mgr.free(token).unwrap();
    }

    #[test]
    fn capacity_exhaustion() {
        let mut mgr: StaticMemoryManager<TestTensor, 2> = StaticMemoryManager::new();
        let _t0 = mgr.store(make_msg(1)).unwrap();
        let _t1 = mgr.store(make_msg(2)).unwrap();
        assert_eq!(mgr.available(), 0);

        let err = mgr.store(make_msg(3));
        assert_eq!(err, Err(MemoryError::NoFreeSlots));
    }

    #[test]
    fn double_free_detected() {
        let mut mgr: StaticMemoryManager<TestTensor, 4> = StaticMemoryManager::new();
        let token = mgr.store(make_msg(1)).unwrap();
        mgr.free(token).unwrap();

        let err = mgr.free(token);
        assert_eq!(err, Err(MemoryError::NotAllocated));
    }

    #[test]
    fn bad_token_detected() {
        let mgr: StaticMemoryManager<TestTensor, 4> = StaticMemoryManager::new();
        let bad = MessageToken::new(99);

        assert!(matches!(mgr.read(bad), Err(MemoryError::BadToken)));
        assert!(matches!(mgr.peek_header(bad), Err(MemoryError::BadToken)));
    }

    #[test]
    fn read_freed_slot_is_bad_token() {
        let mut mgr: StaticMemoryManager<TestTensor, 4> = StaticMemoryManager::new();
        let token = mgr.store(make_msg(1)).unwrap();
        mgr.free(token).unwrap();

        assert!(matches!(mgr.read(token), Err(MemoryError::BadToken)));
        assert!(matches!(mgr.peek_header(token), Err(MemoryError::BadToken)));
    }

    #[test]
    fn slot_reuse_after_free() {
        let mut mgr: StaticMemoryManager<TestTensor, 1> = StaticMemoryManager::new();
        let t0 = mgr.store(make_msg(10)).unwrap();
        mgr.free(t0).unwrap();

        // Slot 0 should be reused.
        let t1 = mgr.store(make_msg(20)).unwrap();
        assert_eq!(t1.index(), 0);
        assert_eq!(
            *mgr.read(t1).unwrap().payload(),
            create_test_tensor_filled_with(20)
        );
    }

    #[test]
    fn memory_class_configurable() {
        let mgr: StaticMemoryManager<TestTensor, 4> =
            StaticMemoryManager::with_memory_class(MemoryClass::Device(0));
        assert_eq!(mgr.memory_class(), MemoryClass::Device(0));
    }

    #[test]
    fn default_memory_class_is_host() {
        let mgr: StaticMemoryManager<TestTensor, 4> = StaticMemoryManager::new();
        assert_eq!(mgr.memory_class(), MemoryClass::Host);
    }
}