Struct base::sys::unix::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§

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

Arguments
  • size - Size of memory region in bytes.

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

Arguments
  • size - Size of memory region in bytes.
  • prot - Protection (e.g. readable/writable) of the memory region.

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.

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.
  • flags - flags passed directly to mmap.
  • prot - Protection (e.g. readable/writable) of the memory region.

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.

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.

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).

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).

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

Madvise the kernel to unmap on fork.

Madvise the kernel to use Huge Pages for this mapping.

Calls msync with MS_SYNC on the mapping.

Reads data from a file descriptor and writes it to guest memory.

Arguments
  • mem_offset - Begin writing memory at this offset.
  • src - Read from src to memory.
  • count - Read count bytes from src to memory.
Examples
  • Read bytes from /dev/urandom
      let mut file = File::open(Path::new("/dev/urandom")).map_err(|_| ())?;
      mem_map.read_to_memory(32, &mut file, 128).map_err(|_| ())?;
      let rand_val: u32 =  mem_map.read_obj(40).map_err(|_| ())?;

Writes data from memory to a file descriptor.

Arguments
  • mem_offset - Begin reading memory from this offset.
  • dst - Write from memory to dst.
  • count - Read count bytes from memory to src.
Examples
  • Write 128 bytes to /dev/null
      let mut file = File::open(Path::new("/dev/null")).map_err(|_| ())?;
      mem_map.write_from_memory(32, &mut file, 128).map_err(|_| ())?;

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

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.

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.

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.

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.

Trait Implementations§

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
Converts to this type from the input type.
Returns a pointer to the beginning of the memory region. Should only be used for passing this region to ioctls for setting guest memory.
Returns the size of the memory region in bytes.
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
Remove size-byte mapping starting at offset.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.