diff --git a/framework/aster-frame/src/lib.rs b/framework/aster-frame/src/lib.rs index df5bf86f..1c1f8ee8 100644 --- a/framework/aster-frame/src/lib.rs +++ b/framework/aster-frame/src/lib.rs @@ -14,6 +14,7 @@ #![feature(panic_info_message)] #![feature(ptr_sub_ptr)] #![feature(strict_provenance)] +#![feature(pointer_is_aligned)] #![allow(dead_code)] #![allow(unused_variables)] #![no_std] diff --git a/framework/aster-frame/src/vm/frame.rs b/framework/aster-frame/src/vm/frame.rs index 983e148f..8ceb113e 100644 --- a/framework/aster-frame/src/vm/frame.rs +++ b/framework/aster-frame/src/vm/frame.rs @@ -723,17 +723,32 @@ impl<'a> VmWriter<'a> { /// Fills the available space by repeating `value`. /// + /// Returns the number of values written. + /// /// # Panic /// /// The size of the available space must be a multiple of the size of `value`. /// Otherwise, the method would panic. - pub fn fill(&mut self, value: T) { - assert!(self.avail() / value.as_bytes().len() > 0); - assert!(self.avail() % value.as_bytes().len() == 0); + pub fn fill(&mut self, value: T) -> usize { + let avail = self.avail(); - while self.avail() > 0 { - self.write(&mut value.as_bytes().into()); + assert!((self.cursor as *mut T).is_aligned()); + assert!(avail % core::mem::size_of::() == 0); + + let written_num = avail / core::mem::size_of::(); + + 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 } }