Skip to content

Commit

Permalink
[refactor] separate is_aligned
Browse files Browse the repository at this point in the history
  • Loading branch information
hky1999 committed Dec 11, 2024
1 parent 28f358c commit d434c9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<const PAGE_SIZE: usize> PageAllocator for BitmapPageAllocator<PAGE_SIZE> {
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);
}

Expand All @@ -115,7 +115,10 @@ impl<const PAGE_SIZE: usize> PageAllocator for BitmapPageAllocator<PAGE_SIZE> {
}

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
Expand All @@ -138,12 +141,4 @@ impl<const PAGE_SIZE: usize> PageAllocator for BitmapPageAllocator<PAGE_SIZE> {
}
}

impl<const PAGE_SIZE: usize> BitmapPageAllocator<PAGE_SIZE> {
/// 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<const PAGE_SIZE: usize> BitmapPageAllocator<PAGE_SIZE> {}
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d434c9b

Please sign in to comment.