Struct sparse_mmap::unix::SparseMapping

source ·
pub struct SparseMapping { /* private fields */ }
Expand description

A reserved virtual address range that may be partially populated with memory mappings.

Implementations§

source§

impl SparseMapping

source

pub fn new(len: usize) -> Result<Self, Error>

Reserves a sparse mapping range with the given size.

The range will be aligned to the largest system page size that’s smaller or equal to len.

source

pub fn is_local(&self) -> bool

Returns true if the mapping is local to the current process.

source

pub fn as_ptr(&self) -> *mut c_void

Returns the pointer to the beginning of the sparse mapping.

source

pub fn len(&self) -> usize

Returns the length of the mapping, in bytes.

source

pub fn alloc(&self, offset: usize, len: usize) -> Result<(), Error>

Allocates private, writable memory at the given offset within the mapping.

source

pub fn map_zero(&self, offset: usize, len: usize) -> Result<(), Error>

Maps read-only zero pages at the given offset within the mapping.

source

pub fn map_file( &self, offset: usize, len: usize, file_mapping: impl AsFd, file_offset: u64, writable: bool, ) -> Result<(), Error>

Maps a portion of a file mapping at offset.

source

pub unsafe fn mmap( &self, offset: usize, len: usize, prot: i32, map_flags: i32, fd: impl AsFd, file_offset: i64, ) -> Result<(), Error>

Maps memory into the mapping, passing parameters through to the mmap syscall.

§Safety

This routine is safe to use as long as the caller ensures map_flags excludes any flags that render the memory region non-unmappable (e.g., MAP_LOCKED). Misuse may lead to system resource issues, such as falsely perceived out-of-memory conditions.

source

pub unsafe fn mmap_anonymous( &self, offset: usize, len: usize, prot: i32, map_flags: i32, ) -> Result<()>

Maps anonymous memory into the mapping, with parameters for the mmap syscall.

§Safety

This routine is safe to use as long as the caller ensures map_flags excludes any flags that render the memory region non-unmappable (e.g., MAP_LOCKED). Misuse may lead to system resource issues, such as falsely perceived out-of-memory conditions.

source

pub fn unmap(&self, offset: usize, len: usize) -> Result<()>

Unmaps memory from the mapping.

source§

impl SparseMapping

source

pub fn page_size() -> usize

Gets the supported page size for sparse mappings.

source

pub fn write_at( &self, offset: usize, data: &[u8], ) -> Result<(), SparseMappingError>

Tries to write into the sparse mapping.

source

pub fn read_at( &self, offset: usize, data: &mut [u8], ) -> Result<(), SparseMappingError>

Tries to read from the sparse mapping.

source

pub fn read_plain<T: FromBytes>( &self, offset: usize, ) -> Result<T, SparseMappingError>

Tries to read a type T from offset.

source

pub fn fill_at( &self, offset: usize, val: u8, len: usize, ) -> Result<(), SparseMappingError>

Tries to fill a region of the sparse mapping with val.

source

pub fn atomic_slice(&self, start: usize, len: usize) -> &[AtomicU8]

Gets a slice for accessing the mapped data directly.

This is safe from a Rust memory model perspective, since the underlying VA is either mapped and is owned in a shared state by this object (in which case &AtomicU8 access from multiple threads is fine), or the VA is not mapped but is reserved and so will not be mapped by another Rust object.

In the latter case, actually accessing the data may cause a fault, which will likely lead to a process crash, so care must nonetheless be taken when using this method.

Trait Implementations§

source§

impl Debug for SparseMapping

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for SparseMapping

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for SparseMapping

source§

impl Sync for SparseMapping

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more