Skip to main content

memlink_shm/
priority.rs

1//! Three-tier priority system for message classification and routing.
2//! Critical (20%), High (50%), Low (30%) slot distribution.
3
4#[repr(u8)]
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
6pub enum Priority {
7    Critical = 0,
8    High = 1,
9    #[default]
10    Low = 2,
11}
12
13impl Priority {
14    pub fn slot_range(&self, total_slots: usize) -> (usize, usize) {
15        match self {
16            Priority::Critical => {
17                let count = ((total_slots as f64 * 0.2).ceil() as usize).max(1);
18                (0, count)
19            }
20            Priority::High => {
21                let crit_count = ((total_slots as f64 * 0.2).ceil() as usize).max(1);
22                let high_end = ((total_slots as f64 * 0.7).floor() as usize).max(crit_count);
23                let count = (high_end - crit_count).max(1);
24                (crit_count, count)
25            }
26            Priority::Low => {
27                let crit_count = ((total_slots as f64 * 0.2).ceil() as usize).max(1);
28                let high_end = ((total_slots as f64 * 0.7).floor() as usize).max(crit_count);
29                let start = high_end;
30                let count = (total_slots - start).max(1);
31                (start, count)
32            }
33        }
34    }
35
36    pub fn all_in_order() -> &'static [Priority] {
37        &[Priority::Critical, Priority::High, Priority::Low]
38    }
39
40    pub fn from_u8(val: u8) -> Option<Self> {
41        match val {
42            0 => Some(Priority::Critical),
43            1 => Some(Priority::High),
44            2 => Some(Priority::Low),
45            _ => None,
46        }
47    }
48
49    pub fn as_u8(&self) -> u8 {
50        *self as u8
51    }
52}
53
54pub fn calculate_slot_distribution(total_slots: usize) -> (usize, usize, usize) {
55    let (_crit_start, crit_count) = Priority::Critical.slot_range(total_slots);
56    let (_high_start, high_count) = Priority::High.slot_range(total_slots);
57    let (_low_start, low_count) = Priority::Low.slot_range(total_slots);
58    (crit_count, high_count, low_count)
59}