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 5e93271
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

## New Features

- [add `size` and `len` for `PageRange`, `PhysFrameRange`, `PageRangeInclusive` and `PhysFrameRangeInclusive`](https://github.com/rust-osdev/x86_64/pull/491)

# 0.15.1 – 2024-03-19

## New Features
Expand Down
49 changes: 49 additions & 0 deletions src/structures/paging/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ 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 {
if !self.is_empty() {
self.end - self.start
} else {
0
}
}

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

impl<S: PageSize> Iterator for PhysFrameRange<S> {
Expand Down Expand Up @@ -190,6 +206,22 @@ 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 {
if !self.is_empty() {
self.end - self.start + 1
} else {
0
}
}

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

impl<S: PageSize> Iterator for PhysFrameRangeInclusive<S> {
Expand All @@ -215,3 +247,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);
}
}
45 changes: 45 additions & 0 deletions src/structures/paging/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,22 @@ 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 {
if !self.is_empty() {
self.end - self.start
} else {
0
}
}

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

impl<S: PageSize> Iterator for PageRange<S> {
Expand Down Expand Up @@ -380,6 +396,22 @@ 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 {
if !self.is_empty() {
self.end - self.start + 1
} else {
0
}
}

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

impl<S: PageSize> Iterator for PageRangeInclusive<S> {
Expand Down Expand Up @@ -484,4 +516,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 5e93271

Please sign in to comment.