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

This commit is contained in:
LoGin 2024-04-30 18:45:01 +08:00 committed by GitHub
parent 7401bec5e3
commit 7db6e06354
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions

View File

@ -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
}
}

View File

@ -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));
}

View File

@ -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>,