Skip to content

Commit 7db6e06

Browse files
authored
feat(bitmap): Add bit and for AllocBitMap (DragonOS-Community#793)
1 parent 7401bec commit 7db6e06

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

kernel/crates/bitmap/src/alloc_bitmap.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::ops::BitAnd;
2+
13
use alloc::vec::Vec;
24

35
use crate::{bitmap_core::BitMapCore, traits::BitMapOps};
@@ -108,3 +110,15 @@ impl BitMapOps<usize> for AllocBitmap {
108110
self.core.set_all(self.elements, &mut self.data, value);
109111
}
110112
}
113+
114+
impl BitAnd for AllocBitmap {
115+
type Output = Self;
116+
117+
fn bitand(self, rhs: Self) -> Self::Output {
118+
let mut result = AllocBitmap::new(self.elements);
119+
for i in 0..rhs.data.len() {
120+
result.data[i] = self.data[i] & rhs.data[i];
121+
}
122+
result
123+
}
124+
}

kernel/crates/bitmap/tests/alloc-bitmap.rs

+20
Original file line numberDiff line numberDiff line change
@@ -643,3 +643,23 @@ fn test_alloc_bitmap_full_128() {
643643
assert_eq!(bitmap.is_full(), false);
644644
assert_eq!(bitmap.is_empty(), true);
645645
}
646+
647+
#[test]
648+
fn test_alloc_bitmap_bitand_128() {
649+
let mut bitmap = AllocBitmap::new(128);
650+
bitmap.set_all(true);
651+
652+
let mut bitmap2 = AllocBitmap::new(128);
653+
654+
bitmap2.set(0, true);
655+
bitmap2.set(1, true);
656+
bitmap2.set(67, true);
657+
658+
let bitmap3 = bitmap & bitmap2;
659+
660+
assert_eq!(bitmap3.len(), 128);
661+
assert_eq!(bitmap3.size(), 16);
662+
assert_eq!(bitmap3.first_index(), Some(0));
663+
assert_eq!(bitmap3.first_false_index(), Some(2));
664+
assert_eq!(bitmap3.last_index(), Some(67));
665+
}

kernel/src/libs/cpumask.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::ops::BitAnd;
2+
13
use bitmap::{traits::BitMapOps, AllocBitmap};
24

35
use crate::{mm::percpu::PerCpu, smp::cpu::ProcessorId};
@@ -86,6 +88,15 @@ impl CpuMask {
8688
}
8789
}
8890

91+
impl BitAnd for CpuMask {
92+
type Output = Self;
93+
94+
fn bitand(self, rhs: Self) -> Self::Output {
95+
let bmp = self.bmp & rhs.bmp;
96+
Self { bmp }
97+
}
98+
}
99+
89100
pub struct CpuMaskIter<'a> {
90101
mask: &'a CpuMask,
91102
index: Option<ProcessorId>,

0 commit comments

Comments
 (0)