Skip to content

Commit 45084e0

Browse files
committed
refactor: remove unsafe in reg
Signed-off-by: Woshiluo Luo <woshiluo.luo@outlook.com>
1 parent 7a05488 commit 45084e0

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/de_mut/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use core::marker::PhantomData;
44
use serde::de::MapAccess;
55
use serde::{de, Deserialize};
66

7+
// TODO: Spec 2.3.5 said that we should not inherited from ancestors and the size-cell &
8+
// address-cells should only used for current node's children.
79
#[allow(unused)]
810
#[derive(Clone)]
911
pub struct Node<'de> {

src/de_mut/reg.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub struct RegRegion(pub Range<usize>);
2424
#[derive(Clone, Copy, Debug)]
2525
#[repr(C)]
2626
pub(super) struct RegConfig {
27-
pub address_cells: u32,
28-
pub size_cells: u32,
27+
pub address_cells: usize,
28+
pub size_cells: usize,
2929
}
3030

3131
impl RegConfig {
@@ -87,21 +87,28 @@ impl Iterator for RegIter<'_> {
8787
fn next(&mut self) -> Option<Self::Item> {
8888
let len = BLOCK_LEN * (self.config.address_cells + self.config.size_cells) as usize;
8989
if self.data.len() >= len {
90-
let mut block = self.data.as_ptr() as *const StructureBlock;
91-
self.data = &self.data[len..];
90+
let (current_block, data) = self.data.split_at(len);
91+
self.data = data;
9292
let mut base = 0;
9393
let mut len = 0;
94+
let mut block_id = 0;
9495
for _ in 0..self.config.address_cells {
95-
unsafe {
96-
base = (base << 32) | (*block).as_usize();
97-
block = block.offset(1);
98-
}
96+
base = (base << 32)
97+
| u32::from_be_bytes(
98+
current_block[block_id * 4..(block_id + 1) * 4]
99+
.try_into()
100+
.unwrap(),
101+
) as usize;
102+
block_id += 1;
99103
}
100104
for _ in 0..self.config.size_cells {
101-
unsafe {
102-
len = (len << 32) | (*block).as_usize();
103-
block = block.offset(1);
104-
}
105+
len = (len << 32)
106+
| u32::from_be_bytes(
107+
current_block[block_id * 4..(block_id + 1) * 4]
108+
.try_into()
109+
.unwrap(),
110+
) as usize;
111+
block_id += 1;
105112
}
106113
Some(RegRegion(base..base + len))
107114
} else {

src/de_mut/struct_access.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ impl<'de> de::MapAccess<'de> for StructAccess<'de, '_> {
8484
self.de.cursor = ValueCursor::Body(next);
8585
match name {
8686
"#address-cells" => {
87-
self.de.reg.address_cells = c.map_u32_on(self.de.dtb)?;
87+
self.de.reg.address_cells = c.map_u32_on(self.de.dtb)? as usize;
8888
}
8989
"#size-cells" => {
90-
self.de.reg.size_cells = c.map_u32_on(self.de.dtb)?;
90+
self.de.reg.size_cells = c.map_u32_on(self.de.dtb)? as usize;
9191
}
9292
_ => {}
9393
}

0 commit comments

Comments
 (0)