Skip to main content

kmem_region/
allocator.rs

1use crate::{IntAlign, PhysAddr};
2
3pub struct LineAllocator {
4    pub start: PhysAddr,
5    iter: PhysAddr,
6    pub end: PhysAddr,
7}
8
9impl LineAllocator {
10    #[inline(always)]
11    pub fn new(start: PhysAddr, size: usize) -> Self {
12        Self {
13            start,
14            iter: start,
15            end: start + size,
16        }
17    }
18
19    #[inline(always)]
20    pub fn alloc(&mut self, layout: core::alloc::Layout) -> Option<PhysAddr> {
21        let start = self.iter.align_up(layout.align());
22        if start + layout.size() > self.end {
23            return None;
24        }
25        self.iter = start + layout.size();
26
27        Some(start)
28    }
29
30    #[inline(always)]
31    pub fn highest_address(&self) -> PhysAddr {
32        self.iter
33    }
34
35    #[inline(always)]
36    pub fn used(&self) -> usize {
37        self.iter - self.start
38    }
39}