Skip to main content

AnonymousMmap

Struct AnonymousMmap 

Source
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

Source

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::ResizeFailed if size is zero or exceeds the platform maximum (128 TB on 64-bit, 2 GB on 32-bit).
  • MmapIoError::Io if the kernel rejects the allocation (out of address space, out of memory, etc.).
Source

pub fn len(&self) -> u64

Length of the mapping in bytes.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub unsafe fn as_mut_ptr(&self) -> *mut u8

Raw mutable pointer to the start of the mapping.

§Safety

Same constraints as as_ptr, plus: the caller is responsible for ensuring no aliasing mutable references exist for any byte they write to via this pointer.

Trait Implementations§

Source§

impl Debug for AnonymousMmap

Source§

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

Formats the value using the given formatter. Read more

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

Source§

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

Source§

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.