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

paging: page ranges can be generated from Range<u64> #224

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 29 additions & 1 deletion src/structures/paging/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::structures::paging::PageTableIndex;
use crate::VirtAddr;
use core::fmt;
use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::ops::{Add, AddAssign, Range, Sub, SubAssign};

/// Trait for abstracting over the three possible page sizes on x86_64, 4KiB, 2MiB, 1GiB.
pub trait PageSize: Copy + Eq + PartialOrd + Ord {
Expand Down Expand Up @@ -268,6 +268,11 @@ impl<S: PageSize> Sub<Self> for Page<S> {
}

/// A range of pages with exclusive upper bound.
///
/// ```rust
/// # use x86_64::structures::paging::{Size4KiB, page::PageRange};
/// let _: PageRange<Size4KiB> = (0x1000..0x4000u64).into();
/// ```
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub struct PageRange<S: PageSize = Size4KiB> {
Expand All @@ -277,6 +282,15 @@ pub struct PageRange<S: PageSize = Size4KiB> {
pub end: Page<S>,
}

impl<S: PageSize> From<Range<u64>> for PageRange<S> {
fn from(range: Range<u64>) -> Self {
let start = Page::containing_address(VirtAddr::new(range.start));
let end = Page::containing_address(VirtAddr::new(range.end));

Self { start, end }
}
}

impl<S: PageSize> PageRange<S> {
/// Returns wether this range contains no pages.
#[inline]
Expand Down Expand Up @@ -321,6 +335,11 @@ impl<S: PageSize> fmt::Debug for PageRange<S> {
}

/// A range of pages with inclusive upper bound.
///
/// ```rust
/// # use x86_64::structures::paging::{Size4KiB, page::PageRangeInclusive};
/// let _: PageRangeInclusive<Size4KiB> = (0x1000..0x4000u64).into();
/// ```
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub struct PageRangeInclusive<S: PageSize = Size4KiB> {
Expand All @@ -330,6 +349,15 @@ pub struct PageRangeInclusive<S: PageSize = Size4KiB> {
pub end: Page<S>,
}

impl<S: PageSize> From<Range<u64>> for PageRangeInclusive<S> {
fn from(range: Range<u64>) -> Self {
let start = Page::containing_address(VirtAddr::new(range.start));
let end = Page::containing_address(VirtAddr::new(range.end));

Self { start, end }
}
}

impl<S: PageSize> PageRangeInclusive<S> {
/// Returns wether this range contains no pages.
#[inline]
Expand Down