Browse Source

feat(bitmap): Add bit and for AllocBitMap (#793)

LoGin 10 months ago
parent
commit
7db6e06354

+ 14 - 0
kernel/crates/bitmap/src/alloc_bitmap.rs

@@ -1,3 +1,5 @@
+use core::ops::BitAnd;
+
 use alloc::vec::Vec;
 
 use crate::{bitmap_core::BitMapCore, traits::BitMapOps};
@@ -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 - 0
kernel/crates/bitmap/tests/alloc-bitmap.rs

@@ -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 - 0
kernel/src/libs/cpumask.rs

@@ -1,3 +1,5 @@
+use core::ops::BitAnd;
+
 use bitmap::{traits::BitMapOps, AllocBitmap};
 
 use crate::{mm::percpu::PerCpu, smp::cpu::ProcessorId};
@@ -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>,