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
impl MemoryMapping
sourcepub fn new(size: usize) -> Result<MemoryMapping>
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.
sourcepub fn new_protection(
size: usize,
align: Option<u64>,
prot: Protection
) -> Result<MemoryMapping>
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.
sourcepub fn from_fd(fd: &dyn AsRawDescriptor, size: usize) -> Result<MemoryMapping>
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.
pub fn from_fd_offset( fd: &dyn AsRawDescriptor, size: usize, offset: u64 ) -> Result<MemoryMapping>
sourcepub fn from_fd_offset_protection(
fd: &dyn AsRawDescriptor,
size: usize,
offset: u64,
prot: Protection
) -> Result<MemoryMapping>
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 offd
to start the mmap.prot
- Protection (e.g. readable/writable) of the memory region.
sourcepub fn from_fd_offset_protection_populate(
fd: &dyn AsRawDescriptor,
size: usize,
offset: u64,
align: u64,
prot: Protection,
populate: bool
) -> Result<MemoryMapping>
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 offd
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.
sourcepub unsafe fn new_protection_fixed(
addr: *mut u8,
size: usize,
prot: Protection
) -> Result<MemoryMapping>
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)
.
sourcepub unsafe fn from_descriptor_offset_protection_fixed(
addr: *mut u8,
fd: &dyn AsRawDescriptor,
size: usize,
offset: u64,
prot: Protection
) -> Result<MemoryMapping>
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 offd
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)
.
sourceunsafe fn try_mmap(
addr: Option<*mut u8>,
size: usize,
align: Option<u64>,
prot: c_int,
fd: Option<(&dyn AsRawDescriptor, u64)>
) -> Result<MemoryMapping>
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
sourceunsafe 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>
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
sourcepub fn use_dontfork(&self) -> Result<()>
pub fn use_dontfork(&self) -> Result<()>
Madvise the kernel to unmap on fork.
sourcepub fn use_hugepages(&self) -> Result<()>
pub fn use_hugepages(&self) -> Result<()>
Madvise the kernel to use Huge Pages for this mapping.
sourcepub fn remove_range(&self, mem_offset: usize, count: usize) -> Result<()>
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.
sourcepub fn async_prefetch(&self, mem_offset: usize, count: usize) -> Result<()>
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.
sourcepub fn drop_page_cache(&self, mem_offset: usize, count: usize) -> Result<()>
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.
sourcepub fn lock_on_fault(&self, mem_offset: usize, count: usize) -> Result<()>
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.
sourcepub fn unlock(&self, mem_offset: usize, count: usize) -> Result<()>
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.
pub(crate) fn range_end(&self, offset: usize, count: usize) -> Result<usize>
Trait Implementations§
source§impl Debug for MemoryMapping
impl Debug for MemoryMapping
source§impl Drop for MemoryMapping
impl Drop for MemoryMapping
source§impl From<MemoryMapping> for MemoryMappingArena
impl From<MemoryMapping> for MemoryMappingArena
source§fn from(mmap: MemoryMapping) -> Self
fn from(mmap: MemoryMapping) -> Self
source§impl MappedRegion for MemoryMapping
impl MappedRegion for MemoryMapping
source§fn as_ptr(&self) -> *mut u8
fn as_ptr(&self) -> *mut u8
source§fn add_fd_mapping(
&mut self,
_offset: usize,
_size: usize,
_fd: &dyn AsRawDescriptor,
_fd_offset: u64,
_prot: Protection
) -> Result<()>
fn add_fd_mapping( &mut self, _offset: usize, _size: usize, _fd: &dyn AsRawDescriptor, _fd_offset: u64, _prot: Protection ) -> Result<()>
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