Implement a new set of physical page APIs

This commit is contained in:
Zhang Junyang
2024-12-24 18:20:55 +08:00
committed by Tate, Hongliang Tian
parent 6e1c36965a
commit cdac59beda
56 changed files with 882 additions and 995 deletions

View File

@ -54,7 +54,7 @@ use ostd::{
///
/// The generic parameter `M` of `SafePtr<_, M, _>` must implement the `VmIo`
/// trait. The most important `VmIo` types are `Vmar`, `Vmo`, `IoMem`, and
/// `UntypedFrame`. The blanket implementations of `VmIo` also include pointer-like
/// `DynUFrame`. The blanket implementations of `VmIo` also include pointer-like
/// types that refer to a `VmIo` type. Some examples are `&Vmo`, `Box<Vmar>`,
/// and `Arc<IoMem>`.
///

View File

@ -2,41 +2,41 @@
// SPDX-License-Identifier: MPL-2.0
//! Provides [`SegmentSlice`] for quick duplication and slicing over [`UntypedSegment`].
//! Provides [`SegmentSlice`] for quick duplication and slicing over [`DynUSegment`].
use alloc::sync::Arc;
use core::ops::Range;
use ostd::{
mm::{
FallibleVmRead, FallibleVmWrite, Infallible, Paddr, UntypedFrame, UntypedSegment, VmIo,
VmReader, VmWriter, PAGE_SIZE,
DynUFrame, DynUSegment, FallibleVmRead, FallibleVmWrite, Infallible, Paddr, UntypedMem,
VmIo, VmReader, VmWriter, PAGE_SIZE,
},
Error, Result,
};
/// A reference to a slice of a [`UntypedSegment`].
/// A reference to a slice of a [`DynUSegment`].
///
/// Cloning a [`SegmentSlice`] is cheap, as it only increments one reference
/// count. While cloning a [`UntypedSegment`] will increment the reference count of
/// count. While cloning a [`DynUSegment`] will increment the reference count of
/// many underlying pages.
///
/// The downside is that the [`SegmentSlice`] requires heap allocation. Also,
/// if any [`SegmentSlice`] of the original [`UntypedSegment`] is alive, all pages in
/// the original [`UntypedSegment`], including the pages that are not referenced, will
/// if any [`SegmentSlice`] of the original [`DynUSegment`] is alive, all pages in
/// the original [`DynUSegment`], including the pages that are not referenced, will
/// not be freed.
#[derive(Debug, Clone)]
pub struct SegmentSlice {
inner: Arc<UntypedSegment>,
inner: Arc<DynUSegment>,
range: Range<usize>,
}
impl SegmentSlice {
/// Returns a part of the `UntypedSegment`.
/// Returns a part of the `DynUSegment`.
///
/// # Panics
///
/// If `range` is not within the range of this `UntypedSegment`,
/// If `range` is not within the range of this `DynUSegment`,
/// then the method panics.
pub fn range(&self, range: Range<usize>) -> Self {
let orig_range = &self.range;
@ -124,9 +124,9 @@ impl VmIo for SegmentSlice {
}
}
impl From<UntypedSegment> for SegmentSlice {
fn from(segment: UntypedSegment) -> Self {
let range = 0..segment.nbytes() / PAGE_SIZE;
impl From<DynUSegment> for SegmentSlice {
fn from(segment: DynUSegment) -> Self {
let range = 0..segment.size() / PAGE_SIZE;
Self {
inner: Arc::new(segment),
range,
@ -134,7 +134,7 @@ impl From<UntypedSegment> for SegmentSlice {
}
}
impl From<SegmentSlice> for UntypedSegment {
impl From<SegmentSlice> for DynUSegment {
fn from(slice: SegmentSlice) -> Self {
let start = slice.range.start * PAGE_SIZE;
let end = slice.range.end * PAGE_SIZE;
@ -142,8 +142,8 @@ impl From<SegmentSlice> for UntypedSegment {
}
}
impl From<UntypedFrame> for SegmentSlice {
fn from(frame: UntypedFrame) -> Self {
SegmentSlice::from(UntypedSegment::from(frame))
impl From<DynUFrame> for SegmentSlice {
fn from(frame: DynUFrame) -> Self {
SegmentSlice::from(DynUSegment::from(frame))
}
}