Skip to content

Commit

Permalink
Add size and len for PageRange, PhysFrameRange etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Wasabi375 committed Jul 14, 2024
1 parent 2d3bd56 commit 024007f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/structures/paging/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ impl<S: PageSize> PhysFrameRange<S> {
pub fn is_empty(&self) -> bool {
self.start >= self.end
}

/// Returns the number of frames in the range.
#[inline]
pub fn len(&self) -> u64 {
(self.end.start_address() - self.start.start_address()) / S::SIZE
}

/// Returns the size in bytes of all frames within the range.
#[inline]
pub fn size(&self) -> u64 {
self.start.size() * self.len()
}
}

impl<S: PageSize> Iterator for PhysFrameRange<S> {
Expand Down Expand Up @@ -190,6 +202,18 @@ impl<S: PageSize> PhysFrameRangeInclusive<S> {
pub fn is_empty(&self) -> bool {
self.start > self.end
}

/// Returns the number of frames in the range.
#[inline]
pub fn len(&self) -> u64 {
((self.end.start_address() - self.start.start_address()) / S::SIZE) + 1
}

/// Returns the size in bytes of all frames within the range.
#[inline]
pub fn size(&self) -> u64 {
self.start.size() * self.len()
}
}

impl<S: PageSize> Iterator for PhysFrameRangeInclusive<S> {
Expand All @@ -215,3 +239,20 @@ impl<S: PageSize> fmt::Debug for PhysFrameRangeInclusive<S> {
.finish()
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_frame_range_len() {
let start_addr = PhysAddr::new(0xdead_beaf);
let start = PhysFrame::<Size4KiB>::containing_address(start_addr);
let end = start + 50;

let range = PhysFrameRange { start, end };
assert_eq!(range.len(), 50);

let range_inclusive = PhysFrameRangeInclusive { start, end };
assert_eq!(range_inclusive.len(), 51);
}
}
37 changes: 37 additions & 0 deletions src/structures/paging/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ impl<S: PageSize> PageRange<S> {
pub fn is_empty(&self) -> bool {
self.start >= self.end
}

/// Returns the number of pages in the range.
#[inline]
pub fn len(&self) -> u64 {
(self.end.start_address() - self.start.start_address()) / S::SIZE
}

/// Returns the size in bytes of all pages within the range.
#[inline]
pub fn size(&self) -> u64 {
self.start.size() * self.len()
}
}

impl<S: PageSize> Iterator for PageRange<S> {
Expand Down Expand Up @@ -380,6 +392,18 @@ impl<S: PageSize> PageRangeInclusive<S> {
pub fn is_empty(&self) -> bool {
self.start > self.end
}

/// Returns the number of frames in the range.
#[inline]
pub fn len(&self) -> u64 {
((self.end.start_address() - self.start.start_address()) / S::SIZE) + 1
}

/// Returns the size in bytes of all frames within the range.
#[inline]
pub fn size(&self) -> u64 {
self.start.size() * self.len()
}
}

impl<S: PageSize> Iterator for PageRangeInclusive<S> {
Expand Down Expand Up @@ -484,4 +508,17 @@ mod tests {
}
assert_eq!(range_inclusive.next(), None);
}

#[test]
pub fn test_page_range_len() {
let start_addr = VirtAddr::new(0xdead_beaf);
let start = Page::<Size4KiB>::containing_address(start_addr);
let end = start + 50;

let range = PageRange { start, end };
assert_eq!(range.len(), 50);

let range_inclusive = PageRangeInclusive { start, end };
assert_eq!(range_inclusive.len(), 51);
}
}

0 comments on commit 024007f

Please sign in to comment.