Switch to usize for alignment checks

This commit is contained in:
Berkus Decker 2021-02-28 00:45:52 +02:00
parent ff12867b02
commit bf291b917f
7 changed files with 32 additions and 25 deletions

View File

@ -78,7 +78,7 @@ impl PhysAddr {
/// See the `align_up` function for more information. /// See the `align_up` function for more information.
pub fn aligned_up<U>(self, align: U) -> Self pub fn aligned_up<U>(self, align: U) -> Self
where where
U: Into<u64>, U: Into<usize>,
{ {
PhysAddr(align_up(self.0, align.into())) PhysAddr(align_up(self.0, align.into()))
} }
@ -88,7 +88,7 @@ impl PhysAddr {
/// See the `align_down` function for more information. /// See the `align_down` function for more information.
pub fn aligned_down<U>(self, align: U) -> Self pub fn aligned_down<U>(self, align: U) -> Self
where where
U: Into<u64>, U: Into<usize>,
{ {
PhysAddr(align_down(self.0, align.into())) PhysAddr(align_down(self.0, align.into()))
} }
@ -96,7 +96,7 @@ impl PhysAddr {
/// Checks whether the physical address has the demanded alignment. /// Checks whether the physical address has the demanded alignment.
pub fn is_aligned<U>(self, align: U) -> bool pub fn is_aligned<U>(self, align: U) -> bool
where where
U: Into<u64>, U: Into<usize>,
{ {
self.aligned_down(align) == self self.aligned_down(align) == self
} }

View File

@ -113,7 +113,7 @@ impl VirtAddr {
/// See the `align_up` free function for more information. /// See the `align_up` free function for more information.
pub fn aligned_up<U>(self, align: U) -> Self pub fn aligned_up<U>(self, align: U) -> Self
where where
U: Into<u64>, U: Into<usize>,
{ {
VirtAddr(align_up(self.0, align.into())) VirtAddr(align_up(self.0, align.into()))
} }
@ -123,7 +123,7 @@ impl VirtAddr {
/// See the `align_down` free function for more information. /// See the `align_down` free function for more information.
pub fn aligned_down<U>(self, align: U) -> Self pub fn aligned_down<U>(self, align: U) -> Self
where where
U: Into<u64>, U: Into<usize>,
{ {
VirtAddr(align_down(self.0, align.into())) VirtAddr(align_down(self.0, align.into()))
} }
@ -131,7 +131,7 @@ impl VirtAddr {
/// Checks whether the virtual address has the demanded alignment. /// Checks whether the virtual address has the demanded alignment.
pub fn is_aligned<U>(self, align: U) -> bool pub fn is_aligned<U>(self, align: U) -> bool
where where
U: Into<u64>, U: Into<usize>,
{ {
self.aligned_down(align) == self self.aligned_down(align) == self
} }

View File

@ -48,7 +48,7 @@ impl<S: PageSize> PhysFrame<S> {
/// Returns the frame that contains the given physical address. /// Returns the frame that contains the given physical address.
pub fn containing_address(address: PhysAddr) -> Self { pub fn containing_address(address: PhysAddr) -> Self {
PhysFrame { PhysFrame {
start_address: address.align_down(S::SIZE), start_address: address.aligned_down(S::SIZE),
size: PhantomData, size: PhantomData,
} }
} }
@ -86,8 +86,9 @@ impl<S: PageSize> fmt::Debug for PhysFrame<S> {
impl<S: PageSize> Add<u64> for PhysFrame<S> { impl<S: PageSize> Add<u64> for PhysFrame<S> {
type Output = Self; type Output = Self;
/// Adds `rhs` same-sized frames to the current address.
fn add(self, rhs: u64) -> Self::Output { fn add(self, rhs: u64) -> Self::Output {
PhysFrame::containing_address(self.start_address() + rhs * u64::from(S::SIZE)) PhysFrame::containing_address(self.start_address() + rhs * S::SIZE as u64)
} }
} }
@ -99,8 +100,10 @@ impl<S: PageSize> AddAssign<u64> for PhysFrame<S> {
impl<S: PageSize> Sub<u64> for PhysFrame<S> { impl<S: PageSize> Sub<u64> for PhysFrame<S> {
type Output = Self; type Output = Self;
/// Subtracts `rhs` same-sized frames from the current address.
// @todo should I sub pages or just bytes here?
fn sub(self, rhs: u64) -> Self::Output { fn sub(self, rhs: u64) -> Self::Output {
PhysFrame::containing_address(self.start_address() - rhs * u64::from(S::SIZE)) PhysFrame::containing_address(self.start_address() - rhs * S::SIZE as u64)
} }
} }
@ -111,13 +114,14 @@ impl<S: PageSize> SubAssign<u64> for PhysFrame<S> {
} }
impl<S: PageSize> Sub<PhysFrame<S>> for PhysFrame<S> { impl<S: PageSize> Sub<PhysFrame<S>> for PhysFrame<S> {
type Output = u64; type Output = usize;
/// Return number of frames between start and end addresses.
fn sub(self, rhs: PhysFrame<S>) -> Self::Output { fn sub(self, rhs: PhysFrame<S>) -> Self::Output {
(self.start_address - rhs.start_address) / S::SIZE (self.start_address - rhs.start_address) as usize / S::SIZE
} }
} }
/// An range of physical memory frames, exclusive the upper bound. /// A range of physical memory frames, exclusive the upper bound.
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
#[repr(C)] #[repr(C)]
pub struct PhysFrameRange<S: PageSize = Size4KiB> { pub struct PhysFrameRange<S: PageSize = Size4KiB> {

View File

@ -46,7 +46,7 @@ impl<S: PageSize> Page<S> {
/// Returns the page that contains the given virtual address. /// Returns the page that contains the given virtual address.
pub fn containing_address(address: VirtAddr) -> Self { pub fn containing_address(address: VirtAddr) -> Self {
Page { Page {
start_address: address.align_down(S::SIZE), start_address: address.aligned_down(S::SIZE),
size: PhantomData, size: PhantomData,
} }
} }
@ -147,7 +147,7 @@ impl<S: PageSize> Add<u64> for Page<S> {
type Output = Self; type Output = Self;
// @todo should I add pages or just bytes here? // @todo should I add pages or just bytes here?
fn add(self, rhs: u64) -> Self::Output { fn add(self, rhs: u64) -> Self::Output {
Page::containing_address(self.start_address() + rhs * u64::from(S::SIZE)) Page::containing_address(self.start_address() + rhs * S::SIZE as u64)
} }
} }
@ -159,9 +159,10 @@ impl<S: PageSize> AddAssign<u64> for Page<S> {
impl<S: PageSize> Sub<u64> for Page<S> { impl<S: PageSize> Sub<u64> for Page<S> {
type Output = Self; type Output = Self;
/// Subtracts `rhs` same-sized pages from the current address.
// @todo should I sub pages or just bytes here? // @todo should I sub pages or just bytes here?
fn sub(self, rhs: u64) -> Self::Output { fn sub(self, rhs: u64) -> Self::Output {
Page::containing_address(self.start_address() - rhs * u64::from(S::SIZE)) Page::containing_address(self.start_address() - rhs * S::SIZE as u64)
} }
} }
@ -172,9 +173,9 @@ impl<S: PageSize> SubAssign<u64> for Page<S> {
} }
impl<S: PageSize> Sub<Self> for Page<S> { impl<S: PageSize> Sub<Self> for Page<S> {
type Output = u64; type Output = usize;
fn sub(self, rhs: Self) -> Self::Output { fn sub(self, rhs: Self) -> Self::Output {
(self.start_address - rhs.start_address) / S::SIZE (self.start_address - rhs.start_address) as usize / S::SIZE
} }
} }

View File

@ -73,13 +73,13 @@ impl BootInfo {
let mut new_reg: BootInfoMemRegion = BootInfoMemRegion::new(); let mut new_reg: BootInfoMemRegion = BootInfoMemRegion::new();
/* Determine whether placing the region at the start or the end will create a bigger left over region */ /* Determine whether placing the region at the start or the end will create a bigger left over region */
if reg_iter.start.aligned_up(1u64 << size_bits) - reg_iter.start if reg_iter.start.aligned_up(1usize << size_bits) - reg_iter.start
< reg_iter.end - reg_iter.end.aligned_down(1u64 << size_bits) < reg_iter.end - reg_iter.end.aligned_down(1usize << size_bits)
{ {
new_reg.start = reg_iter.start.aligned_up(1u64 << size_bits); new_reg.start = reg_iter.start.aligned_up(1usize << size_bits);
new_reg.end = new_reg.start + (1u64 << size_bits); new_reg.end = new_reg.start + (1u64 << size_bits);
} else { } else {
new_reg.end = reg_iter.end.aligned_down(1u64 << size_bits); new_reg.end = reg_iter.end.aligned_down(1usize << size_bits);
new_reg.start = new_reg.end - (1u64 << size_bits); new_reg.start = new_reg.end - (1u64 << size_bits);
} }
if new_reg.end > new_reg.start if new_reg.end > new_reg.start

View File

@ -12,6 +12,7 @@
#![feature(asm)] #![feature(asm)]
#![feature(global_asm)] #![feature(global_asm)]
#![feature(decl_macro)] #![feature(decl_macro)]
#![feature(const_fn)]
#![feature(allocator_api)] #![feature(allocator_api)]
#![feature(ptr_internals)] #![feature(ptr_internals)]
#![feature(format_args_nl)] #![feature(format_args_nl)]

View File

@ -10,18 +10,18 @@ pub use bump_allocator::BumpAllocator;
/// ///
/// Returns the greatest x with alignment `align` so that x <= addr. /// Returns the greatest x with alignment `align` so that x <= addr.
/// The alignment must be a power of 2. /// The alignment must be a power of 2.
pub fn align_down(addr: u64, align: u64) -> u64 { pub fn align_down(addr: u64, align: usize) -> u64 {
assert!(align.is_power_of_two(), "`align` must be a power of two"); assert!(align.is_power_of_two(), "`align` must be a power of two");
addr & !(align - 1) addr & !(align as u64 - 1)
} }
/// Align address upwards. /// Align address upwards.
/// ///
/// Returns the smallest x with alignment `align` so that x >= addr. /// Returns the smallest x with alignment `align` so that x >= addr.
/// The alignment must be a power of 2. /// The alignment must be a power of 2.
pub fn align_up(addr: u64, align: u64) -> u64 { pub fn align_up(addr: u64, align: usize) -> u64 {
assert!(align.is_power_of_two(), "`align` must be a power of two"); assert!(align.is_power_of_two(), "`align` must be a power of two");
let align_mask = align - 1; let align_mask = align as u64 - 1;
if addr & align_mask == 0 { if addr & align_mask == 0 {
addr // already aligned addr // already aligned
} else { } else {
@ -31,6 +31,7 @@ pub fn align_up(addr: u64, align: u64) -> u64 {
/// Calculate the next possible aligned address without sanity checking the /// Calculate the next possible aligned address without sanity checking the
/// input parameters. /// input parameters.
// u64 for return and addr?
#[inline] #[inline]
fn aligned_addr_unchecked(addr: usize, alignment: usize) -> usize { fn aligned_addr_unchecked(addr: usize, alignment: usize) -> usize {
(addr + (alignment - 1)) & !(alignment - 1) (addr + (alignment - 1)) & !(alignment - 1)