xref: /DragonOS/kernel/crates/bitmap/src/alloc_bitmap.rs (revision b2ca6800f9d943e5d3656d9b50a099da768775a7)
1*b2ca6800SLoGin use alloc::vec::Vec;
2*b2ca6800SLoGin 
3*b2ca6800SLoGin use crate::{bitmap_core::BitMapCore, traits::BitMapOps};
4*b2ca6800SLoGin 
5*b2ca6800SLoGin pub struct AllocBitmap {
6*b2ca6800SLoGin     elements: usize,
7*b2ca6800SLoGin     data: Vec<usize>,
8*b2ca6800SLoGin     core: BitMapCore<usize>,
9*b2ca6800SLoGin }
10*b2ca6800SLoGin 
11*b2ca6800SLoGin impl AllocBitmap {
12*b2ca6800SLoGin     pub fn new(elements: usize) -> Self {
13*b2ca6800SLoGin         let data = vec![0usize; (elements + usize::BITS as usize - 1) / (usize::BITS as usize)];
14*b2ca6800SLoGin         Self {
15*b2ca6800SLoGin             elements,
16*b2ca6800SLoGin             data,
17*b2ca6800SLoGin             core: BitMapCore::new(),
18*b2ca6800SLoGin         }
19*b2ca6800SLoGin     }
20*b2ca6800SLoGin }
21*b2ca6800SLoGin 
22*b2ca6800SLoGin impl BitMapOps<usize> for AllocBitmap {
23*b2ca6800SLoGin     #[inline]
24*b2ca6800SLoGin     fn get(&self, index: usize) -> Option<bool> {
25*b2ca6800SLoGin         return self.core.get(self.elements, &self.data, index);
26*b2ca6800SLoGin     }
27*b2ca6800SLoGin 
28*b2ca6800SLoGin     #[inline]
29*b2ca6800SLoGin     fn set(&mut self, index: usize, value: bool) -> Option<bool> {
30*b2ca6800SLoGin         return self.core.set(self.elements, &mut self.data, index, value);
31*b2ca6800SLoGin     }
32*b2ca6800SLoGin 
33*b2ca6800SLoGin     #[inline]
34*b2ca6800SLoGin     fn len(&self) -> usize {
35*b2ca6800SLoGin         self.elements
36*b2ca6800SLoGin     }
37*b2ca6800SLoGin 
38*b2ca6800SLoGin     #[inline]
39*b2ca6800SLoGin     fn size(&self) -> usize {
40*b2ca6800SLoGin         self.data.len() * core::mem::size_of::<usize>()
41*b2ca6800SLoGin     }
42*b2ca6800SLoGin 
43*b2ca6800SLoGin     #[inline]
44*b2ca6800SLoGin     fn first_index(&self) -> Option<usize> {
45*b2ca6800SLoGin         self.core.first_index(&self.data)
46*b2ca6800SLoGin     }
47*b2ca6800SLoGin 
48*b2ca6800SLoGin     #[inline]
49*b2ca6800SLoGin     fn first_false_index(&self) -> Option<usize> {
50*b2ca6800SLoGin         self.core.first_false_index(self.elements, &self.data)
51*b2ca6800SLoGin     }
52*b2ca6800SLoGin 
53*b2ca6800SLoGin     #[inline]
54*b2ca6800SLoGin     fn last_index(&self) -> Option<usize> {
55*b2ca6800SLoGin         self.core.last_index(self.elements, &self.data)
56*b2ca6800SLoGin     }
57*b2ca6800SLoGin 
58*b2ca6800SLoGin     #[inline]
59*b2ca6800SLoGin     fn last_false_index(&self) -> Option<usize> {
60*b2ca6800SLoGin         self.core.last_false_index(self.elements, &self.data)
61*b2ca6800SLoGin     }
62*b2ca6800SLoGin 
63*b2ca6800SLoGin     #[inline]
64*b2ca6800SLoGin     fn next_index(&self, index: usize) -> Option<usize> {
65*b2ca6800SLoGin         self.core.next_index(self.elements, &self.data, index)
66*b2ca6800SLoGin     }
67*b2ca6800SLoGin 
68*b2ca6800SLoGin     #[inline]
69*b2ca6800SLoGin     fn next_false_index(&self, index: usize) -> Option<usize> {
70*b2ca6800SLoGin         self.core.next_false_index(self.elements, &self.data, index)
71*b2ca6800SLoGin     }
72*b2ca6800SLoGin 
73*b2ca6800SLoGin     #[inline]
74*b2ca6800SLoGin     fn prev_index(&self, index: usize) -> Option<usize> {
75*b2ca6800SLoGin         self.core.prev_index(self.elements, &self.data, index)
76*b2ca6800SLoGin     }
77*b2ca6800SLoGin 
78*b2ca6800SLoGin     #[inline]
79*b2ca6800SLoGin     fn prev_false_index(&self, index: usize) -> Option<usize> {
80*b2ca6800SLoGin         self.core.prev_false_index(self.elements, &self.data, index)
81*b2ca6800SLoGin     }
82*b2ca6800SLoGin 
83*b2ca6800SLoGin     #[inline]
84*b2ca6800SLoGin     fn invert(&mut self) {
85*b2ca6800SLoGin         self.core.invert(self.elements, &mut self.data);
86*b2ca6800SLoGin     }
87*b2ca6800SLoGin 
88*b2ca6800SLoGin     #[inline]
89*b2ca6800SLoGin     fn is_full(&self) -> bool {
90*b2ca6800SLoGin         self.core.is_full(self.elements, &self.data)
91*b2ca6800SLoGin     }
92*b2ca6800SLoGin 
93*b2ca6800SLoGin     #[inline]
94*b2ca6800SLoGin     fn is_empty(&self) -> bool {
95*b2ca6800SLoGin         self.core.is_empty(&self.data)
96*b2ca6800SLoGin     }
97*b2ca6800SLoGin 
98*b2ca6800SLoGin     #[inline]
99*b2ca6800SLoGin     unsafe fn as_bytes(&self) -> &[u8] {
100*b2ca6800SLoGin         core::slice::from_raw_parts(
101*b2ca6800SLoGin             self.data.as_ptr() as *const u8,
102*b2ca6800SLoGin             core::mem::size_of::<Self>(),
103*b2ca6800SLoGin         )
104*b2ca6800SLoGin     }
105*b2ca6800SLoGin 
106*b2ca6800SLoGin     fn set_all(&mut self, value: bool) {
107*b2ca6800SLoGin         self.core.set_all(self.elements, &mut self.data, value);
108*b2ca6800SLoGin     }
109*b2ca6800SLoGin }
110