Skip to content
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

Add size and len for PageRange, PhysFrameRange, PageRangeInclusive and PhysFrameRangeInclusive #491

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Wasabi375 marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}