Skip to main content

DBox

Struct DBox 

Source
pub struct DBox<T> { /* private fields */ }
Expand description

DMA 可访问的单值容器。

DBox<T> 提供单个值的 DMA 可访问存储,支持自动缓存同步。 每次访问值(read/write/modify)时都会根据 DMA 方向自动处理缓存操作。

§类型参数

  • T: 存储的值类型

§缓存同步

缓存同步在每次访问时自动执行:

  • read(): 读取前使 CPU 缓存失效(FromDevice/Bidirectional)
  • write(value): 写入后刷新 CPU 缓存(ToDevice/Bidirectional)
  • modify(f): 先失效缓存,执行闭包,再刷新缓存

§示例

use dma_api::{DeviceDma, DmaDirection};

#[derive(Default)]
struct Descriptor {
    addr: u64,
    length: u32,
}

let device = DeviceDma::new(0xFFFFFFFF, &my_dma_impl);

// 分配描述符
let mut dma_desc = device
    .box_zero_with_align::<Descriptor>(64, DmaDirection::ToDevice)
    .expect("Failed to allocate");

// 配置描述符(自动刷新缓存)
dma_desc.modify(|d| d.length = 4096);

// 获取 DMA 地址给硬件
let desc_addr = dma_desc.dma_addr();

§生命周期

DBox 拥有其分配的 DMA 内存,在离开作用域时自动释放。

Implementations§

Source§

impl<T> DBox<T>

Source

pub fn dma_addr(&self) -> DmaAddr

获取 DMA 地址。

返回设备用于访问此 DMA 缓冲区的物理/DMA 地址。 将此地址传递给硬件设备以配置 DMA 操作。

§返回

DMA 地址

Source

pub fn read(&self) -> T

读取存储的值。

根据 DMA 方向自动处理缓存同步:

  • FromDevice/Bidirectional: 读取前使 CPU 缓存失效
  • ToDevice: 无缓存操作
§返回

存储的值

Source

pub fn write(&mut self, value: T)

写入新值。

根据 DMA 方向自动处理缓存同步:

  • ToDevice/Bidirectional: 写入后刷新 CPU 缓存
  • FromDevice: 无缓存操作
§参数
  • value: 要写入的值
Source

pub fn modify(&mut self, f: impl FnOnce(&mut T))

修改值(read-modify-write 模式)。

此方法等价于先调用 read(),然后对值执行闭包,最后调用 write()。 缓存同步操作:读取前失效缓存,写入后刷新缓存。

§参数
  • f: 修改值的闭包
§示例
dma_box.modify(|v| v.field += 10);
Source

pub fn as_ptr(&self) -> NonNull<T>

获取指向存储值的指针。

§返回

指向存储值的非空指针

Source

pub unsafe fn as_buff_mut(&mut self) -> &mut [u8]

获取底层缓冲区的可变切片。

§Safety
  • 调用者必须确保在使用该切片期间,设备不会访问此内存区域
  • 调用者必须手动处理缓存同步(flush/invalidate)

Trait Implementations§

Source§

impl<T> Send for DBox<T>
where T: Send,

Auto Trait Implementations§

§

impl<T> Freeze for DBox<T>

§

impl<T> !RefUnwindSafe for DBox<T>

§

impl<T> !Sync for DBox<T>

§

impl<T> Unpin for DBox<T>
where T: Unpin,

§

impl<T> !UnwindSafe for DBox<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.