Frame Allocator OK.

This commit is contained in:
Yifan Wu 2020-12-03 10:40:30 +08:00
parent 528d99258a
commit e37e5708e4
9 changed files with 205 additions and 158 deletions

70
os/src/mm/address.rs Normal file
View file

@ -0,0 +1,70 @@
/// T: {PhysAddr, VirtAddr, PhysPageNum, VirtPageNum}
/// T -> usize: T.0
/// usize -> T: usize.into()
use crate::config::{PAGE_SIZE, PAGE_SIZE_BITS};
/// Definitions
#[derive(Copy, Clone, Debug)]
pub struct PhysAddr(pub usize);
#[derive(Copy, Clone, Debug)]
pub struct VirtAddr(pub usize);
#[derive(Copy, Clone, Debug)]
pub struct PhysPageNum(pub usize);
#[derive(Copy, Clone, Debug)]
pub struct VirtPageNum(pub usize);
impl From<usize> for PhysAddr {
fn from(v: usize) -> Self { Self(v) }
}
impl From<usize> for PhysPageNum {
fn from(v: usize) -> Self { Self(v) }
}
impl From<PhysAddr> for usize {
fn from(v: PhysAddr) -> Self { v.0 }
}
impl From<PhysPageNum> for usize {
fn from(v: PhysPageNum) -> Self { v.0 }
}
impl From<usize> for VirtAddr {
fn from(v: usize) -> Self { Self(v) }
}
impl From<usize> for VirtPageNum {
fn from(v: usize) -> Self { Self(v) }
}
impl From<VirtAddr> for usize {
fn from(v: VirtAddr) -> Self { v.0 }
}
impl From<VirtPageNum> for usize {
fn from(v: VirtPageNum) -> Self { v.0 }
}
impl VirtAddr {
pub fn floor(&self) -> VirtPageNum { VirtPageNum(self.0 / PAGE_SIZE) }
pub fn ceil(&self) -> VirtPageNum { VirtPageNum((self.0 + PAGE_SIZE - 1) / PAGE_SIZE) }
pub fn page_offset(&self) -> usize { self.0 & (PAGE_SIZE - 1) }
}
impl From<VirtAddr> for VirtPageNum {
fn from(v: VirtAddr) -> Self {
assert_eq!(v.page_offset(), 0);
v.floor()
}
}
impl From<VirtPageNum> for VirtAddr {
fn from(v: VirtPageNum) -> Self { Self(v.0 << PAGE_SIZE_BITS) }
}
impl PhysAddr {
pub fn floor(&self) -> PhysPageNum { PhysPageNum(self.0 / PAGE_SIZE) }
pub fn ceil(&self) -> PhysPageNum { PhysPageNum((self.0 + PAGE_SIZE - 1) / PAGE_SIZE) }
pub fn page_offset(&self) -> usize { self.0 & (PAGE_SIZE - 1) }
}
impl From<PhysAddr> for PhysPageNum {
fn from(v: PhysAddr) -> Self {
assert_eq!(v.page_offset(), 0);
v.floor()
}
}
impl From<PhysPageNum> for PhysAddr {
fn from(v: PhysPageNum) -> Self { Self(v.0 << PAGE_SIZE_BITS) }
}