chore: kernel/objects: extract as_object*

This commit is contained in:
Paul Pan 2024-09-03 19:35:17 +08:00
parent 163813fbde
commit db7fa2ba58
2 changed files with 18 additions and 14 deletions

View File

@ -14,11 +14,13 @@
*/ */
use crate::arch::layout::mmap_phys_to_virt;
use core::marker::PhantomData; use core::marker::PhantomData;
use uapi::{ use uapi::{
cap::ObjectType, cap::ObjectType,
error::{SysError, SysResult}, error::{SysError, SysResult},
}; };
use utils::addr::AddressOps;
pub mod cap; pub mod cap;
pub mod cnode; pub mod cnode;
@ -65,6 +67,22 @@ impl<'a, T: KernelObject + ?Sized> Cap<'a, T> {
} }
} }
impl<'a, T: KernelObject> Cap<'a, T> {
pub fn as_object(&self) -> &T {
unsafe {
let virt = mmap_phys_to_virt(self.cte.cap.ptr);
&*(virt.as_const_ptr())
}
}
pub fn as_object_mut(&mut self) -> &mut T {
unsafe {
let virt = mmap_phys_to_virt(self.cte.cap.ptr);
&mut *(virt.as_mut_ptr())
}
}
}
/// KernelObject is the base trait for all kernel objects /// KernelObject is the base trait for all kernel objects
pub trait KernelObject { pub trait KernelObject {
const OBJ_TYPE: ObjectType; const OBJ_TYPE: ObjectType;

View File

@ -155,20 +155,6 @@ impl<'a> TcbCap<'a> {
pub fn mint(ptr: PhysAddr) -> RawCap { pub fn mint(ptr: PhysAddr) -> RawCap {
RawCap::new(0, 0, ptr, ObjectType::TCB) RawCap::new(0, 0, ptr, ObjectType::TCB)
} }
pub fn as_object(&self) -> &TcbObject {
unsafe {
let virt = mmap_phys_to_virt(self.cte.cap.ptr);
&*(virt.as_const_ptr())
}
}
pub fn as_object_mut(&mut self) -> &mut TcbObject {
unsafe {
let virt = mmap_phys_to_virt(self.cte.cap.ptr);
&mut *(virt.as_mut_ptr())
}
}
} }
impl Debug for TcbCap<'_> { impl Debug for TcbCap<'_> {