Skip to content

Commit bc4c8b3

Browse files
bitmap: make use of trailing_{zeros,ones}
Signed-off-by: Anhad Singh <[email protected]>
1 parent 97a6260 commit bc4c8b3

File tree

2 files changed

+74
-44
lines changed

2 files changed

+74
-44
lines changed

aero.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@ def main():
644644

645645
if not kernel_bin or args.check:
646646
return
647+
648+
if not user_bins:
649+
user_bins = []
647650

648651
kernel_bin = kernel_bin[0]
649652
iso_path = prepare_iso(args, kernel_bin, user_bins)

src/aero_kernel/src/utils/bitmap.rs

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,9 @@ impl<A: Allocator> Bitmap<A> {
9898
/// ```
9999
pub fn find_first_unset(&self) -> Option<usize> {
100100
for (i, block) in self.bitmap.iter().enumerate() {
101-
let mut block_value = *block;
102-
103-
if block_value != usize::MAX {
104-
let mut bit = 0;
105-
106-
// Loop through the bits in the block and find
107-
// the first unset bit.
108-
while block_value.get_bit(0) {
109-
block_value >>= 1;
110-
bit += 1;
111-
}
112-
113-
return Some((i * BLOCK_BITS) + bit);
101+
let trailing_ones = block.trailing_ones();
102+
if trailing_ones < BLOCK_BITS as u32 {
103+
return Some(i * BLOCK_BITS + trailing_ones as usize);
114104
}
115105
}
116106

@@ -130,19 +120,9 @@ impl<A: Allocator> Bitmap<A> {
130120
/// ```
131121
pub fn find_first_set(&self) -> Option<usize> {
132122
for (i, block) in self.bitmap.iter().enumerate() {
133-
let mut block_value = *block;
134-
135-
if block_value != 0 {
136-
let mut bit = 0;
137-
138-
// Loop through the bits in the block and find
139-
// the first set bit.
140-
while !block_value.get_bit(0) {
141-
block_value >>= 1;
142-
bit += 1;
143-
}
144-
145-
return Some((i * BLOCK_BITS) + bit);
123+
let trailing_zeros = block.trailing_zeros();
124+
if trailing_zeros < BLOCK_BITS as u32 {
125+
return Some(i * BLOCK_BITS + trailing_zeros as usize);
146126
}
147127
}
148128

@@ -155,33 +135,80 @@ mod test {
155135
use super::*;
156136
use alloc::alloc::Global;
157137

138+
const TEST_BITMAP_SIZE: usize = 4096;
139+
158140
#[test]
159-
fn bitmap_first_unset_idx() {
160-
let mut bitmap = Bitmap::new_in(Global, 4096);
141+
fn find_first_unset() {
142+
let mut map = Bitmap::new_in(Global, TEST_BITMAP_SIZE);
161143

162-
bitmap.set(69, true);
163-
assert_eq!(bitmap.find_first_unset(), Some(0));
144+
// Set all of the bits to true.
145+
for i in 0..TEST_BITMAP_SIZE {
146+
assert_eq!(map.find_first_unset(), Some(i));
147+
map.set(i, true);
148+
}
164149

165-
bitmap.set(0, true);
166-
assert_eq!(bitmap.find_first_unset(), Some(1));
167-
}
150+
assert_eq!(map.find_first_unset(), None);
168151

169-
#[test]
170-
fn bitmap_first_set_idx() {
171-
let mut bitmap = Bitmap::new_in(Global, 4096);
152+
map.set(0, false);
153+
assert_eq!(map.find_first_unset(), Some(0));
154+
155+
map.set(0, true);
156+
map.set(1, false);
157+
assert_eq!(map.find_first_unset(), Some(1));
158+
159+
map.set(56, false);
160+
assert_eq!(map.find_first_unset(), Some(1));
172161

173-
bitmap.set(69, true);
174-
assert_eq!(bitmap.find_first_set(), Some(69));
162+
map.set(1, true);
163+
assert_eq!(map.find_first_unset(), Some(56));
164+
165+
map.set(80, false);
166+
assert_eq!(map.find_first_unset(), Some(56));
167+
168+
map.set(56, true);
169+
assert_eq!(map.find_first_unset(), Some(80));
170+
171+
map.set(82, false);
172+
assert_eq!(map.find_first_unset(), Some(80));
173+
174+
map.set(80, true);
175+
assert_eq!(map.find_first_unset(), Some(82));
176+
177+
map.set(5, false);
178+
assert_eq!(map.find_first_unset(), Some(5));
175179
}
176180

177181
#[test]
178-
fn bitmap_set_and_test() {
179-
let mut bitmap = Bitmap::new_in(Global, 4096);
182+
fn find_first_set() {
183+
let mut map = Bitmap::new_in(Global, TEST_BITMAP_SIZE);
184+
assert_eq!(map.find_first_set(), None);
185+
186+
map.set(0, true);
187+
assert_eq!(map.find_first_set(), Some(0));
188+
189+
map.set(0, false);
190+
map.set(1, true);
191+
assert_eq!(map.find_first_set(), Some(1));
192+
193+
map.set(56, true);
194+
assert_eq!(map.find_first_set(), Some(1));
195+
196+
map.set(1, false);
197+
assert_eq!(map.find_first_set(), Some(56));
198+
199+
map.set(80, true);
200+
assert_eq!(map.find_first_set(), Some(56));
201+
202+
map.set(56, false);
203+
assert_eq!(map.find_first_set(), Some(80));
204+
205+
map.set(82, true);
206+
assert_eq!(map.find_first_set(), Some(80));
180207

181-
bitmap.set(69, true);
182-
assert!(bitmap.is_set(69));
208+
map.set(80, false);
209+
assert_eq!(map.find_first_set(), Some(82));
183210

184-
bitmap.set(69, false);
185-
assert!(!bitmap.is_set(69));
211+
map.set(5, true);
212+
assert_eq!(map.find_first_set(), Some(5));
186213
}
187214
}

0 commit comments

Comments
 (0)