Skip to content

Commit

Permalink
Tidy code
Browse files Browse the repository at this point in the history
Signed-off-by: Graham MacDonald <[email protected]>
  • Loading branch information
gmacd committed Feb 4, 2024
1 parent 5758a10 commit 59ff943
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions port/src/bitmapalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ impl<const SIZE_BYTES: usize> Bitmap<SIZE_BYTES> {
Self { bytes: [init_value; SIZE_BYTES] }
}

/// Is bit `i` within the bitmap set?
pub fn is_set(&self, i: usize) -> bool {
let byteidx = i / 8;
let bitidx = i % 8;
let byte = self.bytes[byteidx];
byte & (1 << bitidx) > 0
}

/// Set bit `i` within the bitmap
pub fn set(&mut self, i: usize, b: bool) {
let byteidx = i / 8;
let bitidx = i % 8;
Expand Down Expand Up @@ -149,17 +151,13 @@ impl<const NUM_BITMAPS: usize, const BITMAP_SIZE_BYTES: usize>
return Err(BitmapPageAllocError::OutOfBounds);
}

let bytes_per_bitmap = self.bytes_per_bitmap();
let bitmap_idx = pa.addr() as usize / bytes_per_bitmap;

let offset_into_bitmap = pa.addr() as usize % bytes_per_bitmap;
let bitmap_byte_idx = offset_into_bitmap / self.alloc_page_size;
let (bitmap_idx, bit_idx) = self.physaddr_as_indices(pa);

let bitmap = &mut self.bitmaps[bitmap_idx];
if !bitmap.is_set(bitmap_byte_idx) {
if !bitmap.is_set(bit_idx) {
return Err(BitmapPageAllocError::NotAllocated);
}
bitmap.set(bitmap_byte_idx, false);
bitmap.set(bit_idx, false);

Ok(())
}
Expand All @@ -176,6 +174,20 @@ impl<const NUM_BITMAPS: usize, const BITMAP_SIZE_BYTES: usize>
(total - free_bytes, total)
}

/// For the given physaddr, returns a tuple of
/// (the bitmap containing pa, and the index of the bit within that bitmap).
fn physaddr_as_indices(&self, pa: PhysAddr) -> (usize, usize) {
// Get the index of the bitmap containing the pa
let bytes_per_bitmap = self.bytes_per_bitmap();
let bitmap_idx = pa.addr() as usize / bytes_per_bitmap;

// Get the bit within the bitmap representing the pa
let pa_offset_into_bitmap = pa.addr() as usize % bytes_per_bitmap;
let bit_idx = pa_offset_into_bitmap / self.alloc_page_size;

(bitmap_idx, bit_idx)
}

fn mark_range(
&mut self,
range: &PhysRange,
Expand All @@ -186,18 +198,14 @@ impl<const NUM_BITMAPS: usize, const BITMAP_SIZE_BYTES: usize>
return Err(BitmapPageAllocError::OutOfBounds);
}

let bytes_per_bitmap = self.bytes_per_bitmap();
for pa in range.step_by_rounded(self.alloc_page_size) {
let bitmap_idx = pa.addr() as usize / bytes_per_bitmap;
let (bitmap_idx, bit_idx) = self.physaddr_as_indices(pa);
if bitmap_idx >= self.bitmaps.len() {
return Err(BitmapPageAllocError::OutOfBounds);
}

let offset_into_bitmap = pa.addr() as usize % bytes_per_bitmap;
let bitmap_byte_idx = offset_into_bitmap / self.alloc_page_size;

let bitmap = &mut self.bitmaps[bitmap_idx];
bitmap.set(bitmap_byte_idx, mark_allocated);
bitmap.set(bit_idx, mark_allocated);
}
Ok(())
}
Expand Down

0 comments on commit 59ff943

Please sign in to comment.