From d434c9b834ee0e7f672e0387e21990ca41e318c9 Mon Sep 17 00:00:00 2001 From: hky1999 <976929993@qq.com> Date: Wed, 11 Dec 2024 17:25:01 +0800 Subject: [PATCH] [refactor] separate is_aligned --- src/bitmap.rs | 17 ++++++----------- src/lib.rs | 8 ++++++++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/bitmap.rs b/src/bitmap.rs index 6a5082a..0b3e034 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -101,7 +101,7 @@ impl PageAllocator for BitmapPageAllocator { let align_log2 = align_pow2.trailing_zeros() as usize; // Check if the base address is aligned to the given PAGE_SIZE. - if !Self::is_aligned(base) { + if !crate::is_aligned(base, Self::PAGE_SIZE) { return Err(AllocError::InvalidParam); } @@ -115,7 +115,10 @@ impl PageAllocator for BitmapPageAllocator { } fn dealloc_pages(&mut self, pos: usize, num_pages: usize) { - assert!(Self::is_aligned(pos), "pos must be aligned to PAGE_SIZE"); + assert!( + crate::is_aligned(pos, Self::PAGE_SIZE), + "pos must be aligned to PAGE_SIZE" + ); if self .inner @@ -138,12 +141,4 @@ impl PageAllocator for BitmapPageAllocator { } } -impl BitmapPageAllocator { - /// Checks whether the address has the demanded alignment. - /// - /// Equivalent to `addr % align == 0`, but the alignment must be a power of two. - #[inline] - const fn is_aligned(base_addr: usize) -> bool { - base_addr & (PAGE_SIZE - 1) == 0 - } -} +impl BitmapPageAllocator {} diff --git a/src/lib.rs b/src/lib.rs index 7853c28..015a781 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,6 +140,14 @@ const fn align_up(pos: usize, align: usize) -> usize { (pos + align - 1) & !(align - 1) } +/// Checks whether the address has the demanded alignment. +/// +/// Equivalent to `addr % align == 0`, but the alignment must be a power of two. +#[inline] +const fn is_aligned(base_addr: usize, align: usize) -> bool { + base_addr & (align - 1) == 0 +} + #[cfg(feature = "allocator_api")] mod allocator_api { extern crate alloc;