Skip to content

Commit f0c99cc

Browse files
committed
Moved the bitmap data structure into a structures module
Signed-off-by: SlyMarbo <[email protected]>
1 parent fc65019 commit f0c99cc

File tree

4 files changed

+215
-208
lines changed

4 files changed

+215
-208
lines changed

kernel/src/lib.rs

Lines changed: 1 addition & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
extern crate alloc;
3131

32-
use alloc::vec;
33-
use alloc::vec::Vec;
3432
use bootloader::BootInfo;
3533
use core::panic::PanicInfo;
3634
use lazy_static::lazy_static;
@@ -43,6 +41,7 @@ pub mod interrupts;
4341
pub mod memory;
4442
pub mod pci;
4543
pub mod serial;
44+
pub mod structures;
4645
pub mod task;
4746
pub mod time;
4847

@@ -99,146 +98,6 @@ impl<A> Locked<A> {
9998
}
10099
}
101100

102-
/// Bitmap is a simple bitmap implementation.
103-
///
104-
#[derive(Debug, PartialEq)]
105-
pub struct Bitmap {
106-
num: usize,
107-
bits: Vec<u64>,
108-
}
109-
110-
impl Bitmap {
111-
/// new_set returns a new bitmap with all
112-
/// bits set to true.
113-
///
114-
pub fn new_set(num: usize) -> Self {
115-
// Make sure we only set the bits we
116-
// have.
117-
let mut bitmap = Bitmap::new_unset(num);
118-
for i in 0..num {
119-
bitmap.set(i);
120-
}
121-
122-
bitmap
123-
}
124-
125-
/// new_unset returns a new bitmap with all
126-
/// bits set to false.
127-
///
128-
pub fn new_unset(num: usize) -> Self {
129-
Bitmap {
130-
num,
131-
bits: vec![0u64; (num + 63) / 64],
132-
}
133-
}
134-
135-
// get returns whether bit n is set.
136-
//
137-
// get will panic if n exceeds the bitmap's
138-
// size in bits.
139-
//
140-
pub fn get(&self, n: usize) -> bool {
141-
if n >= self.num {
142-
panic!("cannot call get({}) on Bitmap of size {}", n, self.num);
143-
}
144-
145-
let i = n / 64;
146-
let j = n % 64;
147-
let mask = 1u64 << (j as u64);
148-
149-
self.bits[i] & mask == mask
150-
}
151-
152-
// set sets bit n to true.
153-
//
154-
// set will panic if n exceeds the bitmap's
155-
// size in bits.
156-
//
157-
pub fn set(&mut self, n: usize) {
158-
if n >= self.num {
159-
panic!("cannot call set({}) on Bitmap of size {}", n, self.num);
160-
}
161-
162-
let i = n / 64;
163-
let j = n % 64;
164-
let mask = 1u64 << (j as u64);
165-
166-
self.bits[i] |= mask;
167-
}
168-
169-
// unset sets bit n to false.
170-
//
171-
// unset will panic if n exceeds the bitmap's
172-
// size in bits.
173-
//
174-
pub fn unset(&mut self, n: usize) {
175-
if n >= self.num {
176-
panic!("cannot call unset({}) on Bitmap of size {}", n, self.num);
177-
}
178-
179-
let i = n / 64;
180-
let j = n % 64;
181-
let mask = 1u64 << (j as u64);
182-
183-
self.bits[i] &= !mask;
184-
}
185-
186-
// next_set returns the smallest n, such that
187-
// bit n is set (true), or None if all bits
188-
// are false.
189-
//
190-
pub fn next_set(&self) -> Option<usize> {
191-
for (i, values) in self.bits.iter().enumerate() {
192-
for j in 0..64 {
193-
let mask = 1u64 << (j as u64);
194-
if values & mask == mask {
195-
return Some(i * 64 + j);
196-
}
197-
}
198-
}
199-
200-
None
201-
}
202-
203-
// next_n_set returns the smallest i, such that
204-
// bits i to i+n-1 are set (true), or None if
205-
// no such i can be found..
206-
//
207-
pub fn next_n_set(&self, n: usize) -> Option<usize> {
208-
let mut mask = 0u64;
209-
for i in 0..n {
210-
mask |= 1 << i;
211-
}
212-
213-
for (i, values) in self.bits.iter().enumerate() {
214-
for j in 0..64 - n {
215-
if values & mask << j == mask << j {
216-
return Some(i * 64 + j);
217-
}
218-
}
219-
}
220-
221-
None
222-
}
223-
224-
// next_unset returns the smallest n, such that
225-
// bit n is unset (false), or None if all bits
226-
// are true.
227-
//
228-
pub fn next_unset(&self) -> Option<usize> {
229-
for (i, values) in self.bits.iter().enumerate() {
230-
for j in 0..64 {
231-
let mask = 1u64 << (j as u64);
232-
if values & mask == 0 {
233-
return Some(i * 64 + j);
234-
}
235-
}
236-
}
237-
238-
None
239-
}
240-
}
241-
242101
// Test helpers.
243102

244103
/// Testable represents a test function.
@@ -337,68 +196,3 @@ fn test_kernel_main(boot_info: &'static BootInfo) -> ! {
337196
fn panic(info: &PanicInfo) -> ! {
338197
test_panic_handler(info)
339198
}
340-
341-
#[test_case]
342-
fn bitmap() {
343-
let mut bitmap = Bitmap::new_unset(7);
344-
for i in 0..7 {
345-
// Check it's false by default.
346-
assert_eq!(bitmap.get(i), false);
347-
assert_eq!(bitmap.next_set(), None);
348-
349-
// Check set.
350-
bitmap.set(i);
351-
assert_eq!(bitmap.get(i), true);
352-
assert_eq!(bitmap.next_set(), Some(i));
353-
354-
// Check unset.
355-
bitmap.unset(i);
356-
assert_eq!(bitmap.get(i), false);
357-
}
358-
359-
bitmap = Bitmap::new_unset(67);
360-
for i in 0..67 {
361-
// Check it's false by default.
362-
assert_eq!(bitmap.get(i), false);
363-
assert_eq!(bitmap.next_set(), None);
364-
365-
// Check set.
366-
bitmap.set(i);
367-
assert_eq!(bitmap.get(i), true);
368-
assert_eq!(bitmap.next_set(), Some(i));
369-
370-
// Check unset.
371-
bitmap.unset(i);
372-
assert_eq!(bitmap.get(i), false);
373-
}
374-
375-
bitmap = Bitmap::new_set(7);
376-
for i in 0..7 {
377-
// Check it's true by default.
378-
assert_eq!(bitmap.get(i), true);
379-
380-
// Check unset.
381-
bitmap.unset(i);
382-
assert_eq!(bitmap.get(i), false);
383-
assert_eq!(bitmap.next_unset(), Some(i));
384-
385-
// Check set.
386-
bitmap.set(i);
387-
assert_eq!(bitmap.get(i), true);
388-
}
389-
390-
bitmap = Bitmap::new_set(67);
391-
for i in 0..67 {
392-
// Check it's true by default.
393-
assert_eq!(bitmap.get(i), true);
394-
395-
// Check unset.
396-
bitmap.unset(i);
397-
assert_eq!(bitmap.get(i), false);
398-
assert_eq!(bitmap.next_unset(), Some(i));
399-
400-
// Check set.
401-
bitmap.set(i);
402-
assert_eq!(bitmap.get(i), true);
403-
}
404-
}

kernel/src/memory/pmm/bitmap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//! to allocate and deallocate physical memory frames.
33
44
use crate::memory::pmm::boot_info::BootInfoFrameAllocator;
5-
use crate::{println, Bitmap};
5+
use crate::println;
6+
use crate::structures::bitmap::Bitmap;
67
use alloc::vec::Vec;
78
use bootloader::bootinfo::{MemoryRegion, MemoryRegionType};
89
use core::slice::Iter;

0 commit comments

Comments
 (0)