Skip to content

Commit a59bf9f

Browse files
authored
Merge pull request #491 from Wasabi375/ranges
Add size and len for PageRange, PhysFrameRange, PageRangeInclusive and PhysFrameRangeInclusive
2 parents 1d8a69c + ae65fb6 commit a59bf9f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
## New Features
4+
5+
- [add `size` and `len` for `PageRange`, `PhysFrameRange`, `PageRangeInclusive` and `PhysFrameRangeInclusive`](https://github.com/rust-osdev/x86_64/pull/491)
6+
37
# 0.15.1 – 2024-03-19
48

59
## New Features

src/structures/paging/frame.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ impl<S: PageSize> PhysFrameRange<S> {
148148
pub fn is_empty(&self) -> bool {
149149
self.start >= self.end
150150
}
151+
152+
/// Returns the number of frames in the range.
153+
#[inline]
154+
pub fn len(&self) -> u64 {
155+
if !self.is_empty() {
156+
self.end - self.start
157+
} else {
158+
0
159+
}
160+
}
161+
162+
/// Returns the size in bytes of all frames within the range.
163+
#[inline]
164+
pub fn size(&self) -> u64 {
165+
S::SIZE * self.len()
166+
}
151167
}
152168

153169
impl<S: PageSize> Iterator for PhysFrameRange<S> {
@@ -190,6 +206,22 @@ impl<S: PageSize> PhysFrameRangeInclusive<S> {
190206
pub fn is_empty(&self) -> bool {
191207
self.start > self.end
192208
}
209+
210+
/// Returns the number of frames in the range.
211+
#[inline]
212+
pub fn len(&self) -> u64 {
213+
if !self.is_empty() {
214+
self.end - self.start + 1
215+
} else {
216+
0
217+
}
218+
}
219+
220+
/// Returns the size in bytes of all frames within the range.
221+
#[inline]
222+
pub fn size(&self) -> u64 {
223+
S::SIZE * self.len()
224+
}
193225
}
194226

195227
impl<S: PageSize> Iterator for PhysFrameRangeInclusive<S> {
@@ -215,3 +247,20 @@ impl<S: PageSize> fmt::Debug for PhysFrameRangeInclusive<S> {
215247
.finish()
216248
}
217249
}
250+
251+
#[cfg(test)]
252+
mod tests {
253+
use super::*;
254+
#[test]
255+
pub fn test_frame_range_len() {
256+
let start_addr = PhysAddr::new(0xdead_beaf);
257+
let start = PhysFrame::<Size4KiB>::containing_address(start_addr);
258+
let end = start + 50;
259+
260+
let range = PhysFrameRange { start, end };
261+
assert_eq!(range.len(), 50);
262+
263+
let range_inclusive = PhysFrameRangeInclusive { start, end };
264+
assert_eq!(range_inclusive.len(), 51);
265+
}
266+
}

src/structures/paging/page.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,22 @@ impl<S: PageSize> PageRange<S> {
327327
pub fn is_empty(&self) -> bool {
328328
self.start >= self.end
329329
}
330+
331+
/// Returns the number of pages in the range.
332+
#[inline]
333+
pub fn len(&self) -> u64 {
334+
if !self.is_empty() {
335+
self.end - self.start
336+
} else {
337+
0
338+
}
339+
}
340+
341+
/// Returns the size in bytes of all pages within the range.
342+
#[inline]
343+
pub fn size(&self) -> u64 {
344+
S::SIZE * self.len()
345+
}
330346
}
331347

332348
impl<S: PageSize> Iterator for PageRange<S> {
@@ -380,6 +396,22 @@ impl<S: PageSize> PageRangeInclusive<S> {
380396
pub fn is_empty(&self) -> bool {
381397
self.start > self.end
382398
}
399+
400+
/// Returns the number of frames in the range.
401+
#[inline]
402+
pub fn len(&self) -> u64 {
403+
if !self.is_empty() {
404+
self.end - self.start + 1
405+
} else {
406+
0
407+
}
408+
}
409+
410+
/// Returns the size in bytes of all frames within the range.
411+
#[inline]
412+
pub fn size(&self) -> u64 {
413+
S::SIZE * self.len()
414+
}
383415
}
384416

385417
impl<S: PageSize> Iterator for PageRangeInclusive<S> {
@@ -484,4 +516,17 @@ mod tests {
484516
}
485517
assert_eq!(range_inclusive.next(), None);
486518
}
519+
520+
#[test]
521+
pub fn test_page_range_len() {
522+
let start_addr = VirtAddr::new(0xdead_beaf);
523+
let start = Page::<Size4KiB>::containing_address(start_addr);
524+
let end = start + 50;
525+
526+
let range = PageRange { start, end };
527+
assert_eq!(range.len(), 50);
528+
529+
let range_inclusive = PageRangeInclusive { start, end };
530+
assert_eq!(range_inclusive.len(), 51);
531+
}
487532
}

0 commit comments

Comments
 (0)