Skip to content

Commit

Permalink
Make align_up and align_down const
Browse files Browse the repository at this point in the history
We can't make VirtAddr and PhysAddr methods const as they wrap an into
impl.
  • Loading branch information
josephlr committed Jul 7, 2021
1 parent 66b11eb commit 8afe59a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,12 @@ impl Sub<PhysAddr> for PhysAddr {
/// Returns the greatest x with alignment `align` so that x <= addr. The alignment must be
/// a power of 2.
#[inline]
pub fn align_down(addr: u64, align: u64) -> u64 {
pub const fn align_down(addr: u64, align: u64) -> u64 {
#[cfg(feature = "const_fn")]
assert!(align.is_power_of_two(), "`align` must be a power of two");
#[cfg(not(feature = "const_fn"))]
[(); 1][!align.is_power_of_two() as usize];

addr & !(align - 1)
}

Expand All @@ -544,8 +548,12 @@ pub fn align_down(addr: u64, align: u64) -> u64 {
/// Returns the smallest x with alignment `align` so that x >= addr. The alignment must be
/// a power of 2.
#[inline]
pub fn align_up(addr: u64, align: u64) -> u64 {
pub const fn align_up(addr: u64, align: u64) -> u64 {
#[cfg(feature = "const_fn")]
assert!(align.is_power_of_two(), "`align` must be a power of two");
#[cfg(not(feature = "const_fn"))]
[(); 1][!align.is_power_of_two() as usize];

let align_mask = align - 1;
if addr & align_mask == 0 {
addr // already aligned
Expand Down

0 comments on commit 8afe59a

Please sign in to comment.