Skip to content

Commit

Permalink
copy wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Mar 7, 2022
1 parent 7f6cb34 commit a3fdb80
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
13 changes: 7 additions & 6 deletions capsules/src/app_flash_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ impl<'a> AppFlash<'a> {
self.buffer
.take()
.map_or(Err(ErrorCode::RESERVE), |buffer| {
let _ = app_buffer.copy_to_slice(buffer);
let length = cmp::min(buffer.len(), app_buffer.len());
let d = &app_buffer.get(0..length).unwrap();
for (i, c) in
buffer.as_mut()[0..length].iter_mut().enumerate()
{
*c = d.get(i).unwrap().get();
}
// let d = &app_buffer.get(0..length).unwrap();
// for (i, c) in
// buffer.as_mut()[0..length].iter_mut().enumerate()
// {
// *c = d.get(i).unwrap().get();
// }

self.driver.write(buffer, flash_address, length)
})
Expand Down
35 changes: 35 additions & 0 deletions kernel/src/processbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,45 @@ impl<'a> ReadableProcessSlice<'a> {
/// `ErrorCode::SIZE` is returned. Subslicing can be used to obtain a slice
/// of matching length.
pub fn copy_to_slice(&self, dest: &mut [u8]) -> Result<(), ErrorCode> {
// Method implementation adopted from the
// core::slice::copy_from_slice method implementation:
// https://doc.rust-lang.org/src/core/slice/mod.rs.html#3034-3036

if self.len() != dest.len() {
Err(ErrorCode::SIZE)
} else {
// _If_ this turns out to not be efficiently optimized, it
// should be possible to use a ptr::copy_nonoverlapping here
// given we have exclusive mutable access to the destination
// slice which will never be in process memory, and the layout
// of &[ReadableProcessByte] is guaranteed to be compatible to
// &[u8].
for (i, b) in self.iter().enumerate() {
dest[i] = b.get();
}
Ok(())
}
}

/// Copy as much of the contents of a [`ReadableProcessSlice`] into a
/// mutable slice reference as possible.
///
/// This will copy up to `self.len()` or `dest.len()` bytes, whichever is
/// smaller.
///
/// This returns the number of bytes copied.
pub fn copy_to_slice_minimum(&self, dest: &mut [u8]) -> usize {
// Method implemetation adopted from the
// core::slice::copy_from_slice method implementation:
// https://doc.rust-lang.org/src/core/slice/mod.rs.html#3034-3036

let copy_length = cmp::min(self.len(), dest.len());


let dest_short = self.get_unchecked_mut(0..copy_length);

let _ = self.copy_to_slice(dest_short)

if self.len() != dest.len() {
Err(ErrorCode::SIZE)
} else {
Expand Down

0 comments on commit a3fdb80

Please sign in to comment.