mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-13 15:26:48 +00:00
Improve the efficiency of fill method in VmWriter
This commit is contained in:
parent
a6055bb092
commit
c9cfb98746
@ -14,6 +14,7 @@
|
|||||||
#![feature(panic_info_message)]
|
#![feature(panic_info_message)]
|
||||||
#![feature(ptr_sub_ptr)]
|
#![feature(ptr_sub_ptr)]
|
||||||
#![feature(strict_provenance)]
|
#![feature(strict_provenance)]
|
||||||
|
#![feature(pointer_is_aligned)]
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
#![allow(unused_variables)]
|
#![allow(unused_variables)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
@ -723,17 +723,32 @@ impl<'a> VmWriter<'a> {
|
|||||||
|
|
||||||
/// Fills the available space by repeating `value`.
|
/// Fills the available space by repeating `value`.
|
||||||
///
|
///
|
||||||
|
/// Returns the number of values written.
|
||||||
|
///
|
||||||
/// # Panic
|
/// # Panic
|
||||||
///
|
///
|
||||||
/// The size of the available space must be a multiple of the size of `value`.
|
/// The size of the available space must be a multiple of the size of `value`.
|
||||||
/// Otherwise, the method would panic.
|
/// Otherwise, the method would panic.
|
||||||
pub fn fill<T: Pod>(&mut self, value: T) {
|
pub fn fill<T: Pod>(&mut self, value: T) -> usize {
|
||||||
assert!(self.avail() / value.as_bytes().len() > 0);
|
let avail = self.avail();
|
||||||
assert!(self.avail() % value.as_bytes().len() == 0);
|
|
||||||
|
|
||||||
while self.avail() > 0 {
|
assert!((self.cursor as *mut T).is_aligned());
|
||||||
self.write(&mut value.as_bytes().into());
|
assert!(avail % core::mem::size_of::<T>() == 0);
|
||||||
|
|
||||||
|
let written_num = avail / core::mem::size_of::<T>();
|
||||||
|
|
||||||
|
for i in 0..written_num {
|
||||||
|
// Safety: `written_num` is calculated by the avail size and the size of the type `T`,
|
||||||
|
// hence the `add` operation and `write` operation are valid and will only manipulate
|
||||||
|
// the memory managed by this writer.
|
||||||
|
unsafe {
|
||||||
|
(self.cursor as *mut T).add(i).write(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The available space has been filled so this cursor can be moved to the end.
|
||||||
|
self.cursor = self.end;
|
||||||
|
written_num
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user