Struct base::sys::linux::mmap::MemoryMapping

source ·
pub struct MemoryMapping {
    addr: *mut u8,
    size: usize,
}
Expand description

Wraps an anonymous shared memory mapping in the current process. Provides RAII semantics including munmap when no longer needed.

Fields§

§addr: *mut u8§size: usize

Implementations§

source§

impl MemoryMapping

source

pub fn new(size: usize) -> Result<MemoryMapping>

Creates an anonymous shared, read/write mapping of size bytes.

§Arguments
  • size - Size of memory region in bytes.
source

pub fn new_protection( size: usize, align: Option<u64>, prot: Protection ) -> Result<MemoryMapping>

Creates an anonymous shared mapping of size bytes with prot protection.

§Arguments
  • size - Size of memory region in bytes.
  • align - Optional alignment for MemoryMapping::addr.
  • prot - Protection (e.g. readable/writable) of the memory region.
source

pub fn from_fd(fd: &dyn AsRawDescriptor, size: usize) -> Result<MemoryMapping>

Maps the first size bytes of the given fd as read/write.

§Arguments
  • fd - File descriptor to mmap from.
  • size - Size of memory region in bytes.
source

pub fn from_fd_offset( fd: &dyn AsRawDescriptor, size: usize, offset: u64 ) -> Result<MemoryMapping>

source

pub fn from_fd_offset_protection( fd: &dyn AsRawDescriptor, size: usize, offset: u64, prot: Protection ) -> Result<MemoryMapping>

Maps the size bytes starting at offset bytes of the given fd as read/write.

§Arguments
  • fd - File descriptor to mmap from.
  • size - Size of memory region in bytes.
  • offset - Offset in bytes from the beginning of fd to start the mmap.
  • prot - Protection (e.g. readable/writable) of the memory region.
source

pub fn from_fd_offset_protection_populate( fd: &dyn AsRawDescriptor, size: usize, offset: u64, align: u64, prot: Protection, populate: bool ) -> Result<MemoryMapping>

Maps size bytes starting at offset from the given fd as read/write, and requests that the pages are pre-populated.

§Arguments
  • fd - File descriptor to mmap from.
  • size - Size of memory region in bytes.
  • offset - Offset in bytes from the beginning of fd to start the mmap.
  • align - Alignment for MemoryMapping::addr.
  • prot - Protection (e.g. readable/writable) of the memory region.
  • populate - Populate (prefault) page tables for a mapping.
source

pub unsafe fn new_protection_fixed( addr: *mut u8, size: usize, prot: Protection ) -> Result<MemoryMapping>

Creates an anonymous shared mapping of size bytes with prot protection.

§Arguments
  • addr - Memory address to mmap at.
  • size - Size of memory region in bytes.
  • prot - Protection (e.g. readable/writable) of the memory region.
§Safety

This function should not be called before the caller unmaps any mmap’d regions already present at (addr..addr+size).

source

pub unsafe fn from_descriptor_offset_protection_fixed( addr: *mut u8, fd: &dyn AsRawDescriptor, size: usize, offset: u64, prot: Protection ) -> Result<MemoryMapping>

Maps the size bytes starting at offset bytes of the given fd with prot protections.

§Arguments
  • addr - Memory address to mmap at.
  • fd - File descriptor to mmap from.
  • size - Size of memory region in bytes.
  • offset - Offset in bytes from the beginning of fd to start the mmap.
  • prot - Protection (e.g. readable/writable) of the memory region.
§Safety

This function should not be called before the caller unmaps any mmap’d regions already present at (addr..addr+size).

source

unsafe fn try_mmap( addr: Option<*mut u8>, size: usize, align: Option<u64>, prot: c_int, fd: Option<(&dyn AsRawDescriptor, u64)> ) -> Result<MemoryMapping>

Helper wrapper around try_mmap_populate when without MAP_POPULATE

source

unsafe fn try_mmap_populate( addr: Option<*mut u8>, size: usize, align: Option<u64>, prot: c_int, fd: Option<(&dyn AsRawDescriptor, u64)>, populate: bool ) -> Result<MemoryMapping>

Helper wrapper around libc::mmap that does some basic validation, and calls madvise with MADV_DONTDUMP on the created mmap

source

pub fn use_dontfork(&self) -> Result<()>

Madvise the kernel to unmap on fork.

source

pub fn use_hugepages(&self) -> Result<()>

Madvise the kernel to use Huge Pages for this mapping.

source

pub fn msync(&self) -> Result<()>

Calls msync with MS_SYNC on the mapping.

source

pub fn remove_range(&self, mem_offset: usize, count: usize) -> Result<()>

Uses madvise to tell the kernel to remove the specified range. Subsequent reads to the pages in the range will return zero bytes.

source

pub fn async_prefetch(&self, mem_offset: usize, count: usize) -> Result<()>

Tell the kernel to readahead the range.

This does not block the thread by I/O wait from reading the backed file. This does not guarantee that the pages are surely present unless the pages are mlock(2)ed by lock_on_fault_unchecked().

The mem_offset and count must be validated by caller.

§Arguments
  • mem_offset - The offset of the head of the range.
  • count - The size in bytes of the range.
source

pub fn drop_page_cache(&self, mem_offset: usize, count: usize) -> Result<()>

Tell the kernel to drop the page cache.

This cannot be applied to locked pages.

The mem_offset and count must be validated by caller.

NOTE: This function has destructive semantics. It throws away data in the page cache without writing it to the backing file. If the data is important, the caller should ensure it is written to disk before calling this function or should use MADV_PAGEOUT instead.

§Arguments
  • mem_offset - The offset of the head of the range.
  • count - The size in bytes of the range.
source

pub fn lock_on_fault(&self, mem_offset: usize, count: usize) -> Result<()>

Lock the resident pages in the range not to be swapped out.

The remaining nonresident page are locked when they are populated.

The mem_offset and count must be validated by caller.

§Arguments
  • mem_offset - The offset of the head of the range.
  • count - The size in bytes of the range.
source

pub fn unlock(&self, mem_offset: usize, count: usize) -> Result<()>

Unlock the range of pages.

Unlocking non-locked pages does not fail.

The mem_offset and count must be validated by caller.

§Arguments
  • mem_offset - The offset of the head of the range.
  • count - The size in bytes of the range.
source

pub(crate) fn range_end(&self, offset: usize, count: usize) -> Result<usize>

Trait Implementations§

source§

impl Debug for MemoryMapping

source§

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

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

impl Drop for MemoryMapping

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl From<MemoryMapping> for MemoryMappingArena

source§

fn from(mmap: MemoryMapping) -> Self

Converts to this type from the input type.
source§

impl MappedRegion for MemoryMapping

source§

fn as_ptr(&self) -> *mut u8

Returns a pointer to the beginning of the memory region. Should only be used for passing this region to ioctls for setting guest memory.
source§

fn size(&self) -> usize

Returns the size of the memory region in bytes.
source§

fn add_fd_mapping( &mut self, _offset: usize, _size: usize, _fd: &dyn AsRawDescriptor, _fd_offset: u64, _prot: Protection ) -> Result<()>

Maps size bytes starting at fd_offset bytes from within the given fd at offset bytes from the start of the region with prot protections. offset must be page aligned. Read more
source§

fn remove_mapping(&mut self, _offset: usize, _size: usize) -> Result<()>

Remove size-byte mapping starting at offset.
source§

impl Send for MemoryMapping

source§

impl Sync for MemoryMapping

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, 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>,

§

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>,

§

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.