1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//! Alignment utility functions. /// Align an address downwards, returning the greatest X with alignment `align` /// so that x <= addr. The alignment must be a power of two. /// /// # Panics /// /// Will panic if `align` is not a power of two. pub fn align_down(addr: usize, align: usize) -> usize { if align.is_power_of_two() { addr & !(align - 1) } else if align == 0 { addr } else { panic!("non power-of-two alignment"); } } /// Align an address upwards, returning the smallest X with alignment `align` /// so that x >= addr. The alignment must be a power of two. /// /// # Panics /// /// Will panic if `align` is not a power of two. pub fn align_up(addr: usize, align: usize) -> usize { align_down(addr + align - 1, align) }