From e0c723f62ecb2d0e562be76a2a94ebafe42baab3 Mon Sep 17 00:00:00 2001 From: Joshua Simmons Date: Sat, 29 Oct 2022 16:09:14 +0200 Subject: [PATCH] Add `is_aligned_to` and `is_aligned` functions --- narcissus-core/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 } -- 2.49.0