-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(AddressRange): create and use a proper type
Previously AddressRange was just a type alias to a usize pair. This caused confusion as sometimes it was (addr, size) or (start, end) and lead to a few confusing debugs. The new type hopefully clear up confusions and make it easy to iterate over the pages in an addressrange.
- Loading branch information
Showing
8 changed files
with
187 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ edition = "2021" | |
|
||
[dependencies] | ||
bitflags = "2.3" | ||
log = "0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,60 @@ | ||
#![no_std] | ||
|
||
use core::convert::Into; | ||
use core::ops::Range; | ||
|
||
pub mod mm; | ||
|
||
#[derive(Debug)] | ||
pub enum Error {} | ||
|
||
pub type TimerCallbackFn = fn(); | ||
|
||
pub type Range = (usize, usize); | ||
// /// A range similar to core::ops::Range but that is copyable. | ||
// /// The range is half-open, inclusive below, exclusive above, ie. [start; end[ | ||
// #[derive(Debug, Copy, Clone, PartialEq)] | ||
// pub struct Range<T: Copy> { | ||
// pub start: T, | ||
// pub end: T, | ||
// } | ||
// | ||
// impl<T: Copy + core::cmp::PartialOrd + core::cmp::PartialEq + core::ops::Sub<Output = T>> | ||
// Range<T> | ||
// { | ||
// pub fn new(start: T, end: T) -> Self { | ||
// Self { start, end } | ||
// } | ||
// | ||
// pub fn contains(&self, val: T) -> bool { | ||
// self.start <= val && val < self.end | ||
// } | ||
// | ||
// pub fn size(&self) -> T { | ||
// self.end - self.start | ||
// } | ||
// } | ||
/// A range similar to core::ops::Range but that is copyable. | ||
/// The range is half-open, inclusive below, exclusive above, ie. [start; end[ | ||
#[derive(Debug, Copy, Clone, PartialEq)] | ||
pub struct AddressRange { | ||
pub start: usize, | ||
pub end: usize, | ||
} | ||
|
||
impl AddressRange { | ||
pub fn new<T: Into<usize>>(range: Range<T>) -> Self { | ||
let (start, end) = (range.start.into(), range.end.into()); | ||
// assert!(range.start % page_size == 0); | ||
// assert_eq!(range.end, mm::align_up(range.end, page_size)); | ||
|
||
assert!(start < end); | ||
|
||
Self { start, end } | ||
} | ||
|
||
pub fn with_size(start: usize, size: usize) -> Self { | ||
Self::new(start..start + size) | ||
} | ||
|
||
pub fn round_up_to_page(self, page_size: usize) -> Self { | ||
Self { | ||
start: self.start, | ||
end: mm::align_up(self.end, page_size), | ||
} | ||
} | ||
|
||
pub fn iter_pages(self, page_size: usize) -> impl Iterator<Item = usize> { | ||
assert_eq!(self.end, mm::align_up(self.end, page_size)); | ||
|
||
(self.start..=self.end).step_by(page_size) | ||
} | ||
|
||
pub fn count_pages(&self, page_size: usize) -> usize { | ||
mm::align_up(self.size(), page_size) / page_size | ||
} | ||
|
||
pub fn contains(&self, val: usize) -> bool { | ||
self.start <= val && val < self.end | ||
} | ||
|
||
pub fn size(&self) -> usize { | ||
self.end - self.start | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.