mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-29 04:13:24 +00:00
Introduce XArray and refactor the COW mechanism of Vmo
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
233e1fac98
commit
33c8727a13
4
framework/aster-frame/src/collections/mod.rs
Normal file
4
framework/aster-frame/src/collections/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! This module provides some advanced collections.
|
||||
pub mod xarray;
|
51
framework/aster-frame/src/collections/xarray.rs
Normal file
51
framework/aster-frame/src/collections/xarray.rs
Normal file
@ -0,0 +1,51 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! This module introduces the xarray crate and provides relevant support and interfaces for `XArray`.
|
||||
extern crate xarray as xarray_crate;
|
||||
|
||||
use alloc::sync::Arc;
|
||||
use core::{marker::PhantomData, mem::ManuallyDrop, ops::Deref};
|
||||
|
||||
use xarray_crate::ItemEntry;
|
||||
pub use xarray_crate::{Cursor, CursorMut, XArray, XMark};
|
||||
|
||||
use crate::vm::VmFrame;
|
||||
|
||||
/// `VmFrameRef` is a struct that can work as `&'a VmFrame`.
|
||||
pub struct VmFrameRef<'a> {
|
||||
inner: ManuallyDrop<VmFrame>,
|
||||
_marker: PhantomData<&'a VmFrame>,
|
||||
}
|
||||
|
||||
impl<'a> Deref for VmFrameRef<'a> {
|
||||
type Target = VmFrame;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
// SAFETY: `VmFrame` is essentially an `Arc` smart pointer that points to a location which is aligned to 4,
|
||||
// meeting the requirements of the `ItemEntry` for `XArray`.
|
||||
unsafe impl ItemEntry for VmFrame {
|
||||
type Ref<'a> = VmFrameRef<'a> where Self: 'a;
|
||||
|
||||
fn into_raw(self) -> *const () {
|
||||
let ptr = Arc::as_ptr(&self.frame_index);
|
||||
let _ = ManuallyDrop::new(self);
|
||||
ptr.cast()
|
||||
}
|
||||
|
||||
unsafe fn from_raw(raw: *const ()) -> Self {
|
||||
Self {
|
||||
frame_index: Arc::from_raw(raw.cast()),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn raw_as_ref<'a>(raw: *const ()) -> Self::Ref<'a> {
|
||||
VmFrameRef {
|
||||
inner: ManuallyDrop::new(VmFrame::from_raw(raw.cast())),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
@ -28,6 +28,7 @@ extern crate static_assertions;
|
||||
pub mod arch;
|
||||
pub mod boot;
|
||||
pub mod bus;
|
||||
pub mod collections;
|
||||
pub mod console;
|
||||
pub mod cpu;
|
||||
mod error;
|
||||
|
Reference in New Issue
Block a user