Skip to main content

cranpose_ui_graphics/
memory_headroom.rs

1//! How much memory a device has left, as its host reports it.
2
3/// How much memory the host reports it has left.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum MemoryHeadroom {
6    /// Enough for what caches and lazy layouts keep ready. A host that
7    /// reports nothing has it.
8    #[default]
9    Ample,
10    /// Little: a lazy layout keeps only the items it shows, and its reuse
11    /// pool one more; caches shrink to their least.
12    Tight,
13}
14
15impl MemoryHeadroom {
16    /// The headroom of a heap with `free` of its `total` bytes left, coming
17    /// from `self`: tight below a quarter free, and ample again only above
18    /// three eighths, so the items a tight list lets go do not make it ample
19    /// the next frame.
20    pub fn of_heap(self, free: usize, total: usize) -> Self {
21        match self {
22            Self::Ample if free.saturating_mul(4) < total => Self::Tight,
23            Self::Tight if free.saturating_mul(8) > total.saturating_mul(3) => Self::Ample,
24            same => same,
25        }
26    }
27}