Skip to content

Commit a8d61fa

Browse files
committed
rust: kernel: iosys_map: Wrap iosys_map_memset()
This is strange that in-so-far that it works on byte level and doesn't use IoSysMapRef<T>. It will be used to initialize mappings in the asahi driver either to zero or for debugging purposes to special byte patterns. Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 203c375 commit a8d61fa

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

rust/helpers/iosys_map.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ void rust_helper_iosys_map_memcpy_from(void *dst, const struct iosys_map *src,
1313
{
1414
iosys_map_memcpy_from(dst, src, src_offset, len);
1515
}
16+
17+
void rust_helper_iosys_map_memset(struct iosys_map *dst, size_t offset,
18+
int value, size_t len)
19+
{
20+
iosys_map_memset(dst, offset, value, len);
21+
}

rust/kernel/iosys_map.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,39 @@ impl<'a, T: AsBytes + FromBytes> IoSysMapRef<'a, T> {
186186
Ok(())
187187
}
188188

189+
/// Memset the region starting from `offset`.
190+
///
191+
/// `offset` and `len` are in units of `T`, not the number of bytes.
192+
///
193+
/// This function can return the following errors:
194+
///
195+
/// * [`EOVERFLOW`] if calculating the length of the slice results in an overflow.
196+
/// * [`EINVAL`] if the slice would go out of bounds of the memory region.
197+
///
198+
/// # Examples
199+
///
200+
/// ```
201+
/// use kernel::iosys_map::*;
202+
///
203+
/// # fn test() -> Result {
204+
/// # let mut map = tests::VecIoSysMap::new(&[0u8; 3])?;
205+
/// # {
206+
/// # let mut map = map.get();
207+
/// map.memset(7)?; // (now [7, 7, 7])
208+
/// # }
209+
/// #
210+
/// # map.assert_eq(&[7, 7, 7]);
211+
/// #
212+
/// # Ok::<(), Error>(()) }
213+
/// # assert!(test().is_ok());
214+
/// ```
215+
pub fn memset(&mut self, value: i32) {
216+
// SAFETY:
217+
// - The address pointed to by this iosys_map is guaranteed to be valid via IoSysMapRef's
218+
// type invariants.
219+
unsafe { bindings::iosys_map_memset(self.as_raw_mut(), 0, value, self.size()) };
220+
}
221+
189222
/// Attempt to compute the offset of an item within the iosys map using its index.
190223
///
191224
/// Returns an error if an overflow occurs.

0 commit comments

Comments
 (0)