Skip to content

Commit

Permalink
feat(bitmap): Add bit and for AllocBitMap (DragonOS-Community#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
fslongjin authored Apr 30, 2024
1 parent 7401bec commit 7db6e06
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kernel/crates/bitmap/src/alloc_bitmap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::ops::BitAnd;

use alloc::vec::Vec;

use crate::{bitmap_core::BitMapCore, traits::BitMapOps};
Expand Down Expand Up @@ -108,3 +110,15 @@ impl BitMapOps<usize> for AllocBitmap {
self.core.set_all(self.elements, &mut self.data, value);
}
}

impl BitAnd for AllocBitmap {
type Output = Self;

fn bitand(self, rhs: Self) -> Self::Output {
let mut result = AllocBitmap::new(self.elements);
for i in 0..rhs.data.len() {
result.data[i] = self.data[i] & rhs.data[i];
}
result
}
}
20 changes: 20 additions & 0 deletions kernel/crates/bitmap/tests/alloc-bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,23 @@ fn test_alloc_bitmap_full_128() {
assert_eq!(bitmap.is_full(), false);
assert_eq!(bitmap.is_empty(), true);
}

#[test]
fn test_alloc_bitmap_bitand_128() {
let mut bitmap = AllocBitmap::new(128);
bitmap.set_all(true);

let mut bitmap2 = AllocBitmap::new(128);

bitmap2.set(0, true);
bitmap2.set(1, true);
bitmap2.set(67, true);

let bitmap3 = bitmap & bitmap2;

assert_eq!(bitmap3.len(), 128);
assert_eq!(bitmap3.size(), 16);
assert_eq!(bitmap3.first_index(), Some(0));
assert_eq!(bitmap3.first_false_index(), Some(2));
assert_eq!(bitmap3.last_index(), Some(67));
}
11 changes: 11 additions & 0 deletions kernel/src/libs/cpumask.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::ops::BitAnd;

use bitmap::{traits::BitMapOps, AllocBitmap};

use crate::{mm::percpu::PerCpu, smp::cpu::ProcessorId};
Expand Down Expand Up @@ -86,6 +88,15 @@ impl CpuMask {
}
}

impl BitAnd for CpuMask {
type Output = Self;

fn bitand(self, rhs: Self) -> Self::Output {
let bmp = self.bmp & rhs.bmp;
Self { bmp }
}
}

pub struct CpuMaskIter<'a> {
mask: &'a CpuMask,
index: Option<ProcessorId>,
Expand Down

0 comments on commit 7db6e06

Please sign in to comment.