aboutsummaryrefslogtreecommitdiff
path: root/rendy-memory/src/heaps/heap.rs
blob: 6595cbc83a56fb9232afc07ebfeeaf76330681bd (plain)
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
use crate::utilization::*;

#[derive(Debug)]
pub(super) struct MemoryHeap {
    size: u64,
    used: u64,
    effective: u64,
}

impl MemoryHeap {
    pub(super) fn new(size: u64) -> Self {
        MemoryHeap {
            size,
            used: 0,
            effective: 0,
        }
    }

    pub(super) fn available(&self) -> u64 {
        if self.used > self.size {
            log::warn!("Heap size exceeded");
            0
        } else {
            self.size - self.used
        }
    }

    pub(super) fn allocated(&mut self, used: u64, effective: u64) {
        self.used += used;
        self.effective += effective;
        debug_assert!(self.used >= self.effective);
    }

    pub(super) fn freed(&mut self, used: u64, effective: u64) {
        self.used -= used;
        self.effective -= effective;
        debug_assert!(self.used >= self.effective);
    }

    pub(super) fn utilization(&self) -> MemoryHeapUtilization {
        MemoryHeapUtilization {
            utilization: MemoryUtilization {
                used: self.used,
                effective: self.effective,
            },
            size: self.size,
        }
    }
}