diff --git a/kernel/libs/aster-util/src/safe_ptr.rs b/kernel/libs/aster-util/src/safe_ptr.rs index 4bcb640f3..05716befc 100644 --- a/kernel/libs/aster-util/src/safe_ptr.rs +++ b/kernel/libs/aster-util/src/safe_ptr.rs @@ -5,12 +5,10 @@ use core::{fmt::Debug, marker::PhantomData}; use aster_rights::{Dup, Exec, Full, Read, Signal, TRightSet, TRights, Write}; use aster_rights_proc::require; use inherit_methods_macro::inherit_methods; -pub use ostd::Pod; use ostd::{ mm::{Daddr, DmaStream, HasDaddr, HasPaddr, Paddr, PodOnce, VmIo, VmIoOnce}, - Result, + Pod, Result, }; -pub use typeflags_util::SetContain; /// Safe pointers. /// @@ -157,7 +155,7 @@ pub struct SafePtr { phantom: PhantomData, } -impl SafePtr { +impl SafePtr { /// Create a new instance. /// /// # Access rights @@ -174,15 +172,14 @@ impl SafePtr { } } -impl SafePtr { +impl SafePtr { pub fn paddr(&self) -> Paddr { self.vm_obj.paddr() + self.offset } } +// =============== Read and write methods ============== impl SafePtr> { - // =============== Read and write methods ============== - /// Read the value from the pointer. /// /// # Access rights @@ -222,9 +219,33 @@ impl SafePtr> { pub fn write_slice(&self, slice: &[T]) -> Result<()> { self.vm_obj.write_slice(self.offset, slice) } +} - // =============== Address-related methods ============== +// =============== Read and write methods ============== +impl SafePtr> { + /// Reads the value from the pointer using one non-tearing instruction. + /// + /// # Access rights + /// + /// This method requires the `Read` right. + #[require(R > Read)] + pub fn read_once(&self) -> Result { + self.vm_obj.read_once(self.offset) + } + /// Overwrites the value at the pointer using one non-tearing instruction. + /// + /// # Access rights + /// + /// This method requires the `Write` right. + #[require(R > Write)] + pub fn write_once(&self, val: &T) -> Result<()> { + self.vm_obj.write_once(self.offset, val) + } +} + +// =============== Address-related methods ============== +impl SafePtr { pub const fn is_aligned(&self) -> bool { self.offset % core::mem::align_of::() == 0 } @@ -258,9 +279,10 @@ impl SafePtr> { self.offset -= (-bytes) as usize; } } +} - // =============== VM object-related methods ============== - +// =============== VM object-related methods ============== +impl SafePtr { pub const fn vm(&self) -> &M { &self.vm_obj } @@ -268,9 +290,12 @@ impl SafePtr> { pub fn set_vm(&mut self, vm_obj: M) { self.vm_obj = vm_obj; } +} +// =============== VM object-related methods ============== +impl SafePtr { /// Construct a new SafePtr which will point to the same address - pub fn borrow_vm(&self) -> SafePtr> { + pub fn borrow_vm(&self) -> SafePtr { let SafePtr { offset: addr, vm_obj, @@ -280,15 +305,16 @@ impl SafePtr> { SafePtr { offset: *addr, vm_obj, - rights: *rights, + rights: rights.clone(), phantom: PhantomData, } } +} - // =============== Type conversion methods ============== - +// =============== Type conversion methods ============== +impl SafePtr { /// Cast the accessed structure into a new one, which is usually used when accessing a field in a structure. - pub fn cast(self) -> SafePtr> { + pub fn cast(self) -> SafePtr { let SafePtr { offset: addr, vm_obj, @@ -302,7 +328,10 @@ impl SafePtr> { phantom: PhantomData, } } +} +// =============== Type conversion methods ============== +impl SafePtr> { /// Construct a new SafePtr and restrict the rights of it. /// /// # Access rights @@ -324,28 +353,6 @@ impl SafePtr> { } } -impl SafePtr> { - /// Reads the value from the pointer using one non-tearing instruction. - /// - /// # Access rights - /// - /// This method requires the `Read` right. - #[require(R > Read)] - pub fn read_once(&self) -> Result { - self.vm_obj.read_once(self.offset) - } - - /// Overwrites the value at the pointer using one non-tearing instruction. - /// - /// # Access rights - /// - /// This method requires the `Write` right. - #[require(R > Write)] - pub fn write_once(&self, val: &T) -> Result<()> { - self.vm_obj.write_once(self.offset, val) - } -} - impl HasDaddr for SafePtr { fn daddr(&self) -> Daddr { self.offset + self.vm_obj.daddr() @@ -404,24 +411,13 @@ impl Debug for SafePtr { macro_rules! field_ptr { ($ptr:expr, $type:ty, $($field:tt)+) => {{ use ostd::offset_of; - use ostd::mm::VmIo; - use aster_rights::Dup; - use aster_rights::TRightSet; - use aster_rights::TRights; - use aster_util::safe_ptr::Pod; - use aster_util::safe_ptr::SetContain; use aster_util::safe_ptr::SafePtr; #[inline] - fn new_field_ptr( - container_ptr: &SafePtr>, + fn new_field_ptr( + container_ptr: &SafePtr, field_offset: *const U - ) -> SafePtr> - where - T: Pod, - M: VmIo, - R: TRights, - U: Pod, + ) -> SafePtr { let mut ptr = container_ptr.borrow_vm(); ptr.byte_add(field_offset as usize); @@ -429,6 +425,6 @@ macro_rules! field_ptr { } let field_offset = offset_of!($type, $($field)*); - new_field_ptr($ptr,field_offset) + new_field_ptr($ptr, field_offset) }} }