pub struct AnonymousMmap { /* private fields */ }Expand description
Process-local anonymous memory mapping (no backing file).
Created via AnonymousMmap::new. The mapping is RW; pages are
zero-initialized by the kernel on first touch. Memory is released
when the value is dropped.
§Examples
use mmap_io::AnonymousMmap;
let mmap = AnonymousMmap::new(4096)?;
mmap.update_region(0, b"hello")?;
let mut buf = [0u8; 5];
mmap.read_into(0, &mut buf)?;
assert_eq!(&buf, b"hello");Implementations§
Source§impl AnonymousMmap
impl AnonymousMmap
Sourcepub fn new(size: u64) -> Result<Self>
pub fn new(size: u64) -> Result<Self>
Allocate an anonymous RW mapping of size bytes.
Pages are zero-initialized on first touch (kernel guarantee on every supported platform: Linux, macOS, Windows).
§Errors
MmapIoError::ResizeFailedifsizeis zero or exceeds the platform maximum (128 TB on 64-bit, 2 GB on 32-bit).MmapIoError::Ioif the kernel rejects the allocation (out of address space, out of memory, etc.).
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the mapping has zero length. Always false for a
successfully-constructed AnonymousMmap (the constructor
rejects zero size); provided for API symmetry with
MemoryMappedFile::is_empty.
Sourcepub fn read_into(&self, offset: u64, buf: &mut [u8]) -> Result<()>
pub fn read_into(&self, offset: u64, buf: &mut [u8]) -> Result<()>
Copy buf.len() bytes from the mapping starting at offset
into buf.
§Errors
Returns MmapIoError::OutOfBounds if the range exceeds the
mapping length.
Sourcepub fn update_region(&self, offset: u64, data: &[u8]) -> Result<()>
pub fn update_region(&self, offset: u64, data: &[u8]) -> Result<()>
Write data.len() bytes into the mapping starting at offset.
§Errors
Returns MmapIoError::OutOfBounds if the range exceeds the
mapping length.
Sourcepub fn as_slice(&self, offset: u64, len: u64) -> Result<MappedSlice<'_>>
pub fn as_slice(&self, offset: u64, len: u64) -> Result<MappedSlice<'_>>
Borrow a read-only slice of the mapping.
The returned MappedSlice holds a read lock for its lifetime;
concurrent reads are fine, but any concurrent as_mut_slice or
update_region blocks until the slice is dropped.
§Errors
Returns MmapIoError::OutOfBounds if the range exceeds the
mapping length.
Sourcepub fn as_mut_slice(&self, offset: u64, len: u64) -> Result<MappedSliceMut<'_>>
pub fn as_mut_slice(&self, offset: u64, len: u64) -> Result<MappedSliceMut<'_>>
Borrow a mutable slice of the mapping.
The returned MappedSliceMut holds an exclusive write lock for
its lifetime; any concurrent reader or writer blocks until the
slice is dropped.
§Errors
Returns MmapIoError::OutOfBounds if the range exceeds the
mapping length.
Sourcepub unsafe fn as_ptr(&self) -> *const u8
pub unsafe fn as_ptr(&self) -> *const u8
Raw pointer to the start of the mapping.
§Safety
The caller must not retain the pointer beyond the lifetime of
this AnonymousMmap. Reads through the pointer require no
active mutable borrow elsewhere; writes through the pointer
require no other active borrow at all. Use this only when
bridging to FFI or unsafe code that needs a raw byte pointer.