-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(AddressRange): create and use a proper type #144
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ edition = "2021" | |
|
||
[dependencies] | ||
bitflags = "2.3" | ||
log = "0.4" |
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should just provide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use it for anything else than pages, I've checked |
||
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 | ||
} | ||
Comment on lines
+49
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dito |
||
|
||
pub fn contains(&self, val: usize) -> bool { | ||
self.start <= val && val < self.end | ||
} | ||
|
||
pub fn size(&self) -> usize { | ||
self.end - self.start | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be cool if
mm
was able to find the page_size by itself