nic_buffers/lib.rs
1#![no_std]
2
3/// Error type for DMA allocation failures.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct DmaAllocError;
6
7#[derive(Clone, Copy)]
8pub struct DmaRegion {
9 pub phys: u64,
10 pub virt: *mut u8,
11 pub size: usize,
12}
13
14// SAFETY: DmaRegion contains only a physical address, a raw pointer, and a size.
15// It is safe to send across threads because the underlying DMA memory is
16// accessed only through the allocator's synchronization guarantees.
17unsafe impl Send for DmaRegion {}
18// SAFETY: DmaRegion is safe to share between threads for the same reasons
19// as Send above; concurrent access is controlled by the allocator.
20unsafe impl Sync for DmaRegion {}
21
22impl DmaRegion {
23 pub const ZERO: Self = Self {
24 phys: 0,
25 virt: core::ptr::null_mut(),
26 size: 0,
27 };
28
29 /// Returns whether null.
30 pub fn is_null(&self) -> bool {
31 self.virt.is_null()
32 }
33}
34
35pub trait DmaAllocator {
36 /// Allocates dma.
37 fn alloc_dma(&self, size: usize) -> Result<DmaRegion, DmaAllocError>;
38 /// # Safety
39 ///
40 /// The caller must ensure that `region` was previously allocated by this
41 /// allocator and has not already been freed.
42 unsafe fn free_dma(&self, region: DmaRegion);
43}