sel4_bounce_buffer_allocator/
lib.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

#![no_std]

extern crate alloc;

use core::alloc::Layout;
use core::fmt;
use core::ops::Range;

type Offset = usize;
type Size = usize;
type Align = usize;

mod basic;
mod bump;

pub use basic::Basic;
pub use bump::Bump;

const MIN_ALLOCATION_SIZE: Size = 1;

pub trait AbstractBounceBufferAllocator {
    type Error: fmt::Debug;

    fn allocate(&mut self, layout: Layout) -> Result<Offset, Self::Error>;

    fn deallocate(&mut self, offset: Offset, size: Size);
}

pub struct BounceBufferAllocator<T> {
    abstract_allocator: T,
    max_alignment: Align,
}

impl<T> BounceBufferAllocator<T> {
    pub fn new(abstract_allocator: T, max_alignment: Align) -> Self {
        assert!(max_alignment.is_power_of_two());
        Self {
            abstract_allocator,
            max_alignment,
        }
    }

    pub fn max_alignment(&self) -> Align {
        self.max_alignment
    }

    pub fn check_alignment(&self, region: *mut u8) {
        assert_eq!(region.cast::<()>().align_offset(self.max_alignment()), 0); // sanity check
    }
}

impl<T: AbstractBounceBufferAllocator> BounceBufferAllocator<T> {
    pub fn allocate(&mut self, layout: Layout) -> Result<Range<Offset>, T::Error> {
        assert!(layout.align() <= self.max_alignment);
        assert!(layout.size() >= MIN_ALLOCATION_SIZE);
        let start = self.abstract_allocator.allocate(layout)?;
        let end = start + layout.size();
        Ok(start..end)
    }

    pub fn deallocate(&mut self, buffer: Range<Offset>) {
        self.abstract_allocator
            .deallocate(buffer.start, buffer.len())
    }
}