Skip to main content

DArray

Struct DArray 

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

DMA 可访问的数组容器。

DArray<T> 提供固定大小的 DMA 可访问数组,支持自动缓存同步。 每次访问元素(read/set)时都会根据 DMA 方向自动处理缓存操作。

§类型参数

  • T: 数组元素类型

§缓存同步

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

  • read(index): 读取前使 CPU 缓存失效(FromDevice/Bidirectional)
  • set(index, value): 写入后刷新 CPU 缓存(ToDevice/Bidirectional)
  • copy_from_slice(slice): 写入后刷新整个范围

§示例

use dma_api::{DeviceDma, DmaDirection};

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

// 创建 100 个 u32 的 DMA 数组
let mut dma_array = device
    .array_zero_with_align::<u32>(100, 64, DmaDirection::FromDevice)
    .expect("Failed to allocate");

dma_array.set(0, 0x12345678);  // 写入(自动刷新缓存)
let value = dma_array.read(0);  // 读取(自动失效缓存)

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

§生命周期

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

Implementations§

Source§

impl<T> DArray<T>

Source

pub fn dma_addr(&self) -> DmaAddr

获取 DMA 地址。

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

§返回

DMA 地址

Source

pub fn len(&self) -> usize

获取数组长度(元素个数)。

§返回

数组中的元素个数

Source

pub fn is_empty(&self) -> bool

检查数组是否为空。

§返回

如果数组长度为 0 返回 true,否则返回 false

Source

pub fn bytes_len(&self) -> usize

获取数组的字节长度。

§返回

数组占用的总字节数

Source

pub fn read(&self, index: usize) -> Option<T>

读取指定索引的元素。

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

  • FromDevice/Bidirectional: 读取前使 CPU 缓存失效
  • ToDevice: 无缓存操作
§参数
  • index: 元素索引
§返回

如果索引有效返回 Some(T),否则返回 None

Source

pub fn set(&mut self, index: usize, value: T)

设置指定索引的元素值。

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

  • ToDevice/Bidirectional: 写入后刷新 CPU 缓存
  • FromDevice: 无缓存操作
§参数
  • index: 元素索引,必须在范围内
  • value: 要写入的值
§Panics

如果 index >= self.len() 则 panic

Source

pub fn iter(&self) -> DArrayIter<'_, T>

创建迭代器。

返回一个迭代器,按顺序读取数组元素。 每次读取都会自动处理缓存同步。

§返回

DArrayIter 迭代器

Source

pub fn copy_from_slice(&mut self, src: &[T])

从 slice 复制数据到数组。

复制完成后刷新整个数组的 CPU 缓存(ToDevice/Bidirectional)。

§参数
  • src: 源 slice,长度必须 <= self.len()
§Panics

如果 src.len() > self.len() 则 panic

Source

pub unsafe fn as_mut_slice(&mut self) -> &mut [T]

§Safety

slice will not auto do cache sync operations.

Source

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

Trait Implementations§

Source§

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

Auto Trait Implementations§

§

impl<T> Freeze for DArray<T>

§

impl<T> !RefUnwindSafe for DArray<T>

§

impl<T> !Sync for DArray<T>

§

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

§

impl<T> !UnwindSafe for DArray<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.