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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
//! This crate provides functions for aligning addresses.
//!
//! [`Align`] is implemented for all unsigned integers and provides methods for:
//! * [`align_down`]
//! * [`align_up`]
//! * [`is_aligned`]
//!
//! [`align_down`]: Align::align_down
//! [`align_up`]: Align::align_up
//! [`is_aligned`]: Align::is_aligned
//!
//! This crate is based on work from the [`x86_64`] crate, but is available for all architectures and all unsigned integer types.
//!
//! [`x86_64`]: https://docs.rs/x86_64
//!
//! # Examples
//!
//! ```
//! use align_address::Align;
//!
//! assert_eq!(123_u64.align_up(2), 124);
//! ```
#![no_std]
#![forbid(unsafe_code)]
/// An adress that can be aligned.
pub trait Align<A = Self>: Copy + PartialEq {
/// Align address downwards.
///
/// Returns the greatest `x` with alignment `align` so that `x <= addr`.
///
/// Panics if the alignment is not a power of two.
fn align_down(self, align: A) -> Self;
/// Align address upwards.
///
/// Returns the smallest `x` with alignment `align` so that `x >= addr`.
///
/// Panics if the alignment is not a power of two or if an overflow occurs.
fn align_up(self, align: A) -> Self;
/// Checks whether the address has the demanded alignment.
#[allow(clippy::wrong_self_convention)]
#[inline]
fn is_aligned(self, align: A) -> bool {
self.align_down(align) == self
}
}
macro_rules! align_impl {
($u:ty, $align_down:ident, $align_up:ident) => {
/// Align address downwards.
///
/// Returns the greatest `x` with alignment `align` so that `x <= addr`.
///
/// Panics if the alignment is not a power of two.
// Adapted from `x86_64`
#[inline]
const fn $align_down(addr: $u, align: $u) -> $u {
assert!(align.is_power_of_two(), "`align` must be a power of two");
addr & !(align - 1)
}
/// Align address upwards.
///
/// Returns the smallest `x` with alignment `align` so that `x >= addr`.
///
/// Panics if the alignment is not a power of two or if an overflow occurs.
// Adapted from `x86_64`
#[inline]
const fn $align_up(addr: $u, align: $u) -> $u {
assert!(align.is_power_of_two(), "`align` must be a power of two");
let align_mask = align - 1;
if addr & align_mask == 0 {
addr // already aligned
} else {
// FIXME: Replace with .expect, once `Option::expect` is const.
if let Some(aligned) = (addr | align_mask).checked_add(1) {
aligned
} else {
panic!("attempt to add with overflow")
}
}
}
impl Align for $u {
#[inline]
fn align_down(self, align: Self) -> Self {
$align_down(self, align.into())
}
#[inline]
fn align_up(self, align: Self) -> Self {
$align_up(self, align.into())
}
}
};
}
align_impl!(u8, align_down_u8, align_up_u8);
align_impl!(u16, align_down_u16, align_up_u16);
align_impl!(u32, align_down_u32, align_up_u32);
align_impl!(u64, align_down_u64, align_up_u64);
align_impl!(u128, align_down_u128, align_up_u128);
align_impl!(usize, align_down_usize, align_up_usize);
// Adapted from `x86_64`
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_align_up_impl {
($u:ty, $align_up:ident, $test_align_up:ident) => {
#[test]
fn $test_align_up() {
// align 1
assert_eq!($align_up(0, 1), 0);
assert_eq!($align_up(123, 1), 123);
assert_eq!($align_up(<$u>::MAX, 1), <$u>::MAX);
// align 2
assert_eq!($align_up(0, 2), 0);
assert_eq!($align_up(123, 2), 124);
assert_eq!($align_up(<$u>::MAX - 1, 2), <$u>::MAX - 1);
// address 0
assert_eq!($align_up(0, 128), 0);
assert_eq!($align_up(0, 1), 0);
assert_eq!($align_up(0, 2), 0);
assert_eq!($align_up(0, <$u>::MAX & 1 << (<$u>::BITS - 1)), 0);
}
};
}
test_align_up_impl!(u8, align_up_u8, test_align_up_u8);
test_align_up_impl!(u16, align_up_u16, test_align_up_u16);
test_align_up_impl!(u32, align_up_u32, test_align_up_u32);
test_align_up_impl!(u64, align_up_u64, test_align_up_u64);
test_align_up_impl!(u128, align_up_u128, test_align_up_u128);
test_align_up_impl!(usize, align_up_usize, test_align_up_usize);
macro_rules! test_align_up_overflow_impl {
($u:ty, $test_align_up_overflow:ident, $two:expr) => {
#[test]
#[should_panic]
fn $test_align_up_overflow() {
<$u>::MAX.align_up($two);
}
};
}
test_align_up_overflow_impl!(u8, test_align_up_overflow_u8, 2);
test_align_up_overflow_impl!(u16, test_align_up_overflow_u16, 2);
test_align_up_overflow_impl!(u32, test_align_up_overflow_u32, 2);
test_align_up_overflow_impl!(u64, test_align_up_overflow_u64, 2);
test_align_up_overflow_impl!(u128, test_align_up_overflow_u128, 2);
test_align_up_overflow_impl!(usize, test_align_up_overflow_usize, 2);
}