59 lines
1.8 KiB
Rust
59 lines
1.8 KiB
Rust
use std::fs::read_to_string;
|
|
|
|
pub unsafe trait VirtualMemoryApi: Sized + Drop {
|
|
type Reservation<'s>: Reservation + 's
|
|
where
|
|
Self: 's;
|
|
|
|
fn getPageSize(&self) -> usize;
|
|
|
|
fn reserveMemory<'s>(&'s self, pagesCount: usize) -> Option<Self::Reservation<'s>>;
|
|
|
|
unsafe fn extendReservation<'s>(
|
|
&'s self,
|
|
base: &Self::Reservation<'s>,
|
|
pagesCount: usize,
|
|
) -> ExtendResult<Self::Reservation<'s>>;
|
|
}
|
|
|
|
pub unsafe trait Reservation: Sized + Ord + Drop {
|
|
fn getPageSize(&self) -> usize;
|
|
fn pagesCount(&self) -> usize;
|
|
unsafe fn commitPages(&mut self, indexOfFirst: usize, count: usize) -> *mut [u8];
|
|
unsafe fn decommitPages(&mut self, indexOfFirst: usize, count: usize);
|
|
unsafe fn release(self);
|
|
|
|
fn isFollowedBy(&self, next: &Self) -> bool;
|
|
fn isPrecededBy(&self, next: &Self) -> bool {
|
|
return next.isFollowedBy(self);
|
|
}
|
|
}
|
|
|
|
pub enum ExtendResult<T> {
|
|
OutOfMemory,
|
|
ContinuationIsBusy,
|
|
Success(T),
|
|
}
|
|
|
|
impl<T> ExtendResult<T> {
|
|
pub fn map<R>(&self, caster: impl FnOnce(&T) -> R) -> ExtendResult<R> {
|
|
match self {
|
|
ExtendResult::Success(v) => {
|
|
return ExtendResult::Success(caster(&v));
|
|
}
|
|
ExtendResult::ContinuationIsBusy => return ExtendResult::ContinuationIsBusy,
|
|
ExtendResult::OutOfMemory => return ExtendResult::OutOfMemory,
|
|
}
|
|
}
|
|
|
|
pub fn map_into<R>(self, caster: impl FnOnce(T) -> R) -> ExtendResult<R> {
|
|
match self {
|
|
ExtendResult::Success(v) => {
|
|
return ExtendResult::Success(caster(v));
|
|
}
|
|
ExtendResult::ContinuationIsBusy => return ExtendResult::ContinuationIsBusy,
|
|
ExtendResult::OutOfMemory => return ExtendResult::OutOfMemory,
|
|
}
|
|
}
|
|
}
|