Skip to content

Commit 24691eb

Browse files
authored
Merge pull request #489 from rust-osdev/feature/const-phys-frame
constify PhysFrame functions
2 parents 9a062df + c5bc9fc commit 24691eb

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/addr.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,15 @@ impl PhysAddr {
495495
where
496496
U: Into<u64>,
497497
{
498-
PhysAddr(align_down(self.0, align.into()))
498+
self.align_down_u64(align.into())
499+
}
500+
501+
/// Aligns the physical address downwards to the given alignment.
502+
///
503+
/// See the `align_down` function for more information.
504+
#[inline]
505+
pub(crate) const fn align_down_u64(self, align: u64) -> Self {
506+
PhysAddr(align_down(self.0, align))
499507
}
500508

501509
/// Checks whether the physical address has the demanded alignment.
@@ -504,7 +512,13 @@ impl PhysAddr {
504512
where
505513
U: Into<u64>,
506514
{
507-
self.align_down(align) == self
515+
self.is_aligned_u64(align.into())
516+
}
517+
518+
/// Checks whether the physical address has the demanded alignment.
519+
#[inline]
520+
pub(crate) const fn is_aligned_u64(self, align: u64) -> bool {
521+
self.align_down_u64(align).as_u64() == self.as_u64()
508522
}
509523
}
510524

src/structures/paging/frame.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ impl<S: PageSize> PhysFrame<S> {
2121
///
2222
/// Returns an error if the address is not correctly aligned (i.e. is not a valid frame start).
2323
#[inline]
24+
#[rustversion::attr(since(1.61), const)]
2425
pub fn from_start_address(address: PhysAddr) -> Result<Self, AddressNotAligned> {
25-
if !address.is_aligned(S::SIZE) {
26+
if !address.is_aligned_u64(S::SIZE) {
2627
return Err(AddressNotAligned);
2728
}
2829

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

4748
/// Returns the frame that contains the given physical address.
4849
#[inline]
50+
#[rustversion::attr(since(1.61), const)]
4951
pub fn containing_address(address: PhysAddr) -> Self {
5052
PhysFrame {
51-
start_address: address.align_down(S::SIZE),
53+
start_address: address.align_down_u64(S::SIZE),
5254
size: PhantomData,
5355
}
5456
}

0 commit comments

Comments
 (0)