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
impl SparseMapping
sourcepub fn new(len: usize) -> Result<Self, Error>
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
.
sourcepub fn as_ptr(&self) -> *mut c_void
pub fn as_ptr(&self) -> *mut c_void
Returns the pointer to the beginning of the sparse mapping.
sourcepub fn alloc(&self, offset: usize, len: usize) -> Result<(), Error>
pub fn alloc(&self, offset: usize, len: usize) -> Result<(), Error>
Allocates private, writable memory at the given offset within the mapping.
sourcepub fn map_zero(&self, offset: usize, len: usize) -> Result<(), Error>
pub fn map_zero(&self, offset: usize, len: usize) -> Result<(), Error>
Maps read-only zero pages at the given offset within the mapping.
sourcepub fn map_file(
&self,
offset: usize,
len: usize,
file_mapping: impl AsFd,
file_offset: u64,
writable: bool,
) -> Result<(), Error>
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
.
sourcepub unsafe fn mmap(
&self,
offset: usize,
len: usize,
prot: i32,
map_flags: i32,
fd: impl AsFd,
file_offset: i64,
) -> Result<(), Error>
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.
sourcepub unsafe fn mmap_anonymous(
&self,
offset: usize,
len: usize,
prot: i32,
map_flags: i32,
) -> Result<()>
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§impl SparseMapping
impl SparseMapping
sourcepub fn write_at(
&self,
offset: usize,
data: &[u8],
) -> Result<(), SparseMappingError>
pub fn write_at( &self, offset: usize, data: &[u8], ) -> Result<(), SparseMappingError>
Tries to write into the sparse mapping.
sourcepub fn read_at(
&self,
offset: usize,
data: &mut [u8],
) -> Result<(), SparseMappingError>
pub fn read_at( &self, offset: usize, data: &mut [u8], ) -> Result<(), SparseMappingError>
Tries to read from the sparse mapping.
sourcepub fn read_plain<T: FromBytes>(
&self,
offset: usize,
) -> Result<T, SparseMappingError>
pub fn read_plain<T: FromBytes>( &self, offset: usize, ) -> Result<T, SparseMappingError>
Tries to read a type T
from offset
.
sourcepub fn fill_at(
&self,
offset: usize,
val: u8,
len: usize,
) -> Result<(), SparseMappingError>
pub fn fill_at( &self, offset: usize, val: u8, len: usize, ) -> Result<(), SparseMappingError>
Tries to fill a region of the sparse mapping with val
.
sourcepub fn atomic_slice(&self, start: usize, len: usize) -> &[AtomicU8]
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.