Skip to content

Commit

Permalink
CurrentProc's inner -> &'p Proc
Browse files Browse the repository at this point in the history
  • Loading branch information
anemoneflower committed Feb 4, 2021
1 parent 82aed51 commit a07cd75
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions kernel-rs/src/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,23 +356,23 @@ pub struct Proc {
/// `inner` is current Cpu's proc, this means it's state is `RUNNING`.
/// `Proc` lives during lifetime `'p`.
pub struct CurrentProc<'p> {
inner: *const Proc,
inner: &'p Proc,
_marker: PhantomData<&'p Proc>,
}

impl CurrentProc<'_> {
impl<'p> CurrentProc<'p> {
/// # Safety
///
/// `ptr` must not be null pointer, and should be current Cpu's proc.
unsafe fn from_raw(ptr: *const Proc) -> Self {
unsafe fn from_raw(ptr: &'p Proc) -> Self {
CurrentProc {
inner: ptr,
_marker: PhantomData,
}
}

fn raw(&self) -> *const Proc {
self.inner
self.inner as *const Proc
}

/// Give up the CPU for one scheduling round.
Expand All @@ -387,7 +387,7 @@ impl Deref for CurrentProc<'_> {
type Target = Proc;
fn deref(&self) -> &Self::Target {
// Safe since `inner` always refers to `Proc`.
unsafe { &*self.inner }
self.inner
}
}

Expand Down Expand Up @@ -1019,7 +1019,7 @@ impl Kernel {
/// Returns `Some<CurrentProc<'_>>` if current proc exists.
/// If current proc is null, return `None`.
/// If `(*c).proc` is non-null, returned `CurrentProc`'s `inner` lives during `&self`'s lifetime
pub fn current_proc(&self) -> Option<CurrentProc<'_>> {
pub fn current_proc<'p>(&'p self) -> Option<CurrentProc<'p>> {
unsafe { push_off() };
let cpu = self.mycpu();
let proc = unsafe { (*cpu).proc };
Expand All @@ -1028,6 +1028,6 @@ impl Kernel {
return None;
}
// This is safe because p is non-null and current Cpu's proc.
Some(unsafe { CurrentProc::from_raw(proc) })
Some(unsafe { CurrentProc::from_raw(&(*proc)) })
}
}

0 comments on commit a07cd75

Please sign in to comment.