From: Joshua Simmons Date: Sat, 29 Oct 2022 14:09:14 +0000 (+0200) Subject: Add `is_aligned_to` and `is_aligned` functions X-Git-Url: https://git.nega.tv//gitweb.cgi?a=commitdiff_plain;h=e0c723f62ecb2d0e562be76a2a94ebafe42baab3;p=josh%2Fnarcissus Add `is_aligned_to` and `is_aligned` functions --- diff --git a/narcissus-core/src/lib.rs b/narcissus-core/src/lib.rs index 7a4b304..a503bc5 100644 --- a/narcissus-core/src/lib.rs +++ b/narcissus-core/src/lib.rs @@ -271,6 +271,20 @@ pub fn align_offset(x: usize, align: usize) -> usize { (x + align - 1) & !(align - 1) } +#[must_use] +pub fn is_aligned_to(ptr: *const T, align: usize) -> bool { + if align == 0 || !align.is_power_of_two() { + panic!("is_aligned_to: align is not a power-of-two"); + } + + (ptr as usize) & (align - 1) == 0 +} + +#[must_use] +pub fn is_aligned(ptr: *const T) -> bool { + is_aligned_to(ptr, std::mem::align_of::()) +} + pub fn page_size() -> usize { 4096 }