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