sel4_abstract_allocator/
bump.rs
1use core::alloc::Layout;
8use core::ops::Range;
9
10use crate::{AbstractAllocator, AbstractAllocatorAllocation};
11
12pub struct BumpAllocator {
13 watermark: usize,
14 size: usize,
15}
16
17impl BumpAllocator {
18 pub fn new(size: usize) -> Self {
19 Self { watermark: 0, size }
20 }
21}
22
23impl AbstractAllocator for BumpAllocator {
24 type AllocationError = InsufficientResources;
25
26 type Allocation = Allocation;
27
28 fn allocate(&mut self, layout: Layout) -> Result<Self::Allocation, Self::AllocationError> {
29 let start = self.watermark.next_multiple_of(layout.align());
30 let end = start + layout.size();
31 if end > self.size {
32 return Err(InsufficientResources::new());
33 }
34 self.watermark = end;
35 Ok(Allocation::new(start..end))
36 }
37
38 fn deallocate(&mut self, _allocation: Self::Allocation) {}
39}
40
41pub struct Allocation(Range<usize>);
42
43impl Allocation {
44 fn new(range: Range<usize>) -> Self {
45 Self(range)
46 }
47}
48
49impl AbstractAllocatorAllocation for Allocation {
50 fn range(&self) -> Range<usize> {
51 self.0.clone()
52 }
53}
54
55#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
56pub struct InsufficientResources(());
57
58impl InsufficientResources {
59 fn new() -> Self {
60 Self(())
61 }
62}