feat: switch to new linked_list

This commit is contained in:
Paul Pan 2024-05-04 23:29:42 +08:00
parent c24264f469
commit 0c3ece6fdc
2 changed files with 15 additions and 34 deletions

View File

@ -1,6 +1,10 @@
use crate::objects::null::NullCap;
use api::cap::ObjectType;
use core::{cell::Cell, ptr::NonNull};
use core::cell::Cell;
use utils::{
linked_list::{Link, LinkHelper},
LinkHelperImpl,
};
use vspace::addr::PhysAddr;
/// RawCap is the specific implementation of capability which stores in CNode
@ -31,47 +35,27 @@ impl RawCap {
}
}
#[derive(Copy, Clone, Default)]
pub struct Link {
pub prev: Option<NonNull<CapEntry>>,
pub next: Option<NonNull<CapEntry>>,
}
#[derive(Clone)]
pub struct CapEntry {
pub cap: Cell<RawCap>,
pub link: Cell<Link>,
pub link: Link<Self>,
}
LinkHelperImpl!(CapEntry: link);
const_assert_eq!(core::mem::size_of::<CapEntry>(), ObjectType::CNode.size(0));
impl CapEntry {
pub fn new(cap: RawCap) -> Self {
Self {
cap: Cell::new(cap),
link: Cell::new(Link::default()),
link: Link::default(),
}
}
pub fn init(&mut self) {
self.cap = Cell::new(NullCap::mint());
self.link = Cell::new(Link::default());
}
pub fn prev(&self) -> Option<NonNull<CapEntry>> {
self.link.get().prev
}
pub fn next(&self) -> Option<NonNull<CapEntry>> {
self.link.get().next
}
pub fn set_prev(&mut self, prev: Option<NonNull<CapEntry>>) {
self.link.get_mut().prev = prev;
}
pub fn set_next(&mut self, next: Option<NonNull<CapEntry>>) {
self.link.get_mut().next = next;
self.link = Link::default();
}
}

View File

@ -51,25 +51,22 @@ impl<'a, T: KernelObject + ?Sized> TryFrom<&'a CapEntry> for Cap<'a, T> {
impl<'a, T: KernelObject + ?Sized> Cap<'a, T> {
pub fn append(&mut self, new: &mut CapEntry) {
let next = self.cte.next();
let next = self.cte.link.next();
// update new cap's link
new.set_prev(Some(NonNull::from(self.cte)));
new.set_next(next);
new.link.set_prev(Some(NonNull::from(self.cte)));
new.link.set_next(next);
// record new cap's addr
let new_addr = Some(NonNull::from(new));
// update next cap's link.prev
if let Some(mut next) = next {
unsafe { next.as_mut().set_prev(new_addr) };
unsafe { next.as_mut().link.set_prev(new_addr) };
}
// update self's link.next
self.cte.link.update(|mut link| {
link.next = new_addr;
link
});
self.cte.link.next.set(new_addr);
}
}