Skip to content

constify PhysFrame functions #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/addr.rs
Original file line number Diff line number Diff line change
@@ -495,7 +495,15 @@ impl PhysAddr {
where
U: Into<u64>,
{
PhysAddr(align_down(self.0, align.into()))
self.align_down_u64(align.into())
}

/// Aligns the physical address downwards to the given alignment.
///
/// See the `align_down` function for more information.
#[inline]
pub(crate) const fn align_down_u64(self, align: u64) -> Self {
PhysAddr(align_down(self.0, align))
}

/// Checks whether the physical address has the demanded alignment.
@@ -504,7 +512,13 @@ impl PhysAddr {
where
U: Into<u64>,
{
self.align_down(align) == self
self.is_aligned_u64(align.into())
}

/// Checks whether the physical address has the demanded alignment.
#[inline]
pub(crate) const fn is_aligned_u64(self, align: u64) -> bool {
self.align_down_u64(align).as_u64() == self.as_u64()
}
}

6 changes: 4 additions & 2 deletions src/structures/paging/frame.rs
Original file line number Diff line number Diff line change
@@ -21,8 +21,9 @@ impl<S: PageSize> PhysFrame<S> {
///
/// Returns an error if the address is not correctly aligned (i.e. is not a valid frame start).
#[inline]
#[rustversion::attr(since(1.61), const)]
pub fn from_start_address(address: PhysAddr) -> Result<Self, AddressNotAligned> {
if !address.is_aligned(S::SIZE) {
if !address.is_aligned_u64(S::SIZE) {
return Err(AddressNotAligned);
}

@@ -46,9 +47,10 @@ impl<S: PageSize> PhysFrame<S> {

/// Returns the frame that contains the given physical address.
#[inline]
#[rustversion::attr(since(1.61), const)]
pub fn containing_address(address: PhysAddr) -> Self {
PhysFrame {
start_address: address.align_down(S::SIZE),
start_address: address.align_down_u64(S::SIZE),
size: PhantomData,
}
}