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