Skip to content

Commit

Permalink
refactor: remove unsafe in reg
Browse files Browse the repository at this point in the history
Signed-off-by: Woshiluo Luo <[email protected]>
  • Loading branch information
woshiluo committed Nov 19, 2024
1 parent 7a05488 commit 45084e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/de_mut/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use core::marker::PhantomData;
use serde::de::MapAccess;
use serde::{de, Deserialize};

// TODO: Spec 2.3.5 said that we should not inherited from ancestors and the size-cell &
// address-cells should only used for current node's children.
#[allow(unused)]
#[derive(Clone)]
pub struct Node<'de> {
Expand Down
31 changes: 19 additions & 12 deletions src/de_mut/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub struct RegRegion(pub Range<usize>);
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub(super) struct RegConfig {
pub address_cells: u32,
pub size_cells: u32,
pub address_cells: usize,
pub size_cells: usize,
}

impl RegConfig {
Expand Down Expand Up @@ -87,21 +87,28 @@ impl Iterator for RegIter<'_> {
fn next(&mut self) -> Option<Self::Item> {
let len = BLOCK_LEN * (self.config.address_cells + self.config.size_cells) as usize;
if self.data.len() >= len {
let mut block = self.data.as_ptr() as *const StructureBlock;
self.data = &self.data[len..];
let (current_block, data) = self.data.split_at(len);
self.data = data;
let mut base = 0;
let mut len = 0;
let mut block_id = 0;
for _ in 0..self.config.address_cells {
unsafe {
base = (base << 32) | (*block).as_usize();
block = block.offset(1);
}
base = (base << 32)
| u32::from_be_bytes(
current_block[block_id * 4..(block_id + 1) * 4]
.try_into()
.unwrap(),
) as usize;
block_id += 1;
}
for _ in 0..self.config.size_cells {
unsafe {
len = (len << 32) | (*block).as_usize();
block = block.offset(1);
}
len = (len << 32)
| u32::from_be_bytes(
current_block[block_id * 4..(block_id + 1) * 4]
.try_into()
.unwrap(),
) as usize;
block_id += 1;
}
Some(RegRegion(base..base + len))
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/de_mut/struct_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ impl<'de> de::MapAccess<'de> for StructAccess<'de, '_> {
self.de.cursor = ValueCursor::Body(next);
match name {
"#address-cells" => {
self.de.reg.address_cells = c.map_u32_on(self.de.dtb)?;
self.de.reg.address_cells = c.map_u32_on(self.de.dtb)? as usize;
}
"#size-cells" => {
self.de.reg.size_cells = c.map_u32_on(self.de.dtb)?;
self.de.reg.size_cells = c.map_u32_on(self.de.dtb)? as usize;
}
_ => {}
}
Expand Down

0 comments on commit 45084e0

Please sign in to comment.