Skip to content

Commit 4dde6bd

Browse files
committed
rust: add IoMem::offset_ok_of_val
This performs the same function as `IoMem::offset_ok` but when the size is known only at runtime, for example, with slices (that carry their own length as a runtime value). Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent e2c5979 commit 4dde6bd

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

rust/kernel/io_mem.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ impl<const SIZE: usize> IoMem<SIZE> {
173173
}
174174
}
175175

176+
fn offset_ok_of_val<T: ?Sized>(offset: usize, value: &T) -> bool {
177+
let value_size = core::mem::size_of_val(value);
178+
let value_alignment = core::mem::align_of_val(value);
179+
if let Some(end) = offset.checked_add(value_size) {
180+
end <= SIZE && offset % value_alignment == 0
181+
} else {
182+
false
183+
}
184+
}
185+
176186
const fn check_offset<T>(offset: usize) {
177187
crate::build_assert!(Self::offset_ok::<T>(offset), "IoMem offset overflow");
178188
}

0 commit comments

Comments
 (0)