Struct userfaultfd::Uffd

source ·
pub struct Uffd { /* private fields */ }
Expand description

The userfaultfd object.

The userspace representation of the object is a file descriptor, so this type implements AsRawFd, FromRawFd, and IntoRawFd. These methods should be used with caution, but can be essential for using functions like poll on a worker thread.

Implementations§

source§

impl Uffd

source

pub fn register(&self, start: *mut c_void, len: usize) -> Result<IoctlFlags>

Register a memory address range with the userfaultfd object, and returns the IoctlFlags that are available for the selected range.

This method only registers the given range for missing page faults.

source

pub fn register_with_mode( &self, start: *mut c_void, len: usize, mode: RegisterMode ) -> Result<IoctlFlags>

Register a memory address range with the userfaultfd object for the given mode and returns the IoctlFlags that are available for the selected range.

source

pub fn unregister(&self, start: *mut c_void, len: usize) -> Result<()>

Unregister a memory address range from the userfaultfd object.

source

pub unsafe fn copy( &self, src: *const c_void, dst: *mut c_void, len: usize, wake: bool ) -> Result<usize>

Atomically copy a continuous memory chunk into the userfaultfd-registered range, and return the number of bytes that were successfully copied.

If wake is true, wake up the thread waiting for page fault resolution on the memory range.

source

pub unsafe fn zeropage( &self, start: *mut c_void, len: usize, wake: bool ) -> Result<usize>

Zero out a memory address range registered with userfaultfd, and return the number of bytes that were successfully zeroed.

If wake is true, wake up the thread waiting for page fault resolution on the memory address range.

source

pub fn wake(&self, start: *mut c_void, len: usize) -> Result<()>

Wake up the thread waiting for page fault resolution on the specified memory address range.

source

pub fn read_event(&self) -> Result<Option<Event>>

Read an Event from the userfaultfd object.

If the Uffd object was created with non_blocking set to false, this will block until an event is successfully read (returning Some(event), or an error is returned.

If non_blocking was true, this will immediately return None if no event is ready to read.

Note that while this method doesn’t require a mutable reference to the Uffd object, it does consume bytes (thread-safely) from the underlying file descriptor.

Examples
fn read_event(uffd: &Uffd) -> Result<()> {
    // Read a single event
    match uffd.read_event()? {
        Some(e) => {
            // Do something with the event
        },
        None => {
            // This was a non-blocking read and the descriptor was not ready for read
        },
    }
    Ok(())
}
source

pub fn read_events<'a>( &self, buf: &'a mut EventBuffer ) -> Result<impl Iterator<Item = Result<Event>> + 'a>

Read multiple events from the userfaultfd object using the given event buffer.

If the Uffd object was created with non_blocking set to false, this will block until an event is successfully read or an error is returned.

If non_blocking was true, this will immediately return an empty iterator if the file descriptor is not ready for reading.

Examples
fn read_events(uffd: &Uffd) -> userfaultfd::Result<()> {
    // Read up to 100 events at a time
    let mut buf = EventBuffer::new(100);
    for event in uffd.read_events(&mut buf)? {
        let event = event?;
        // Do something with the event...
    }
    Ok(())
}

Trait Implementations§

source§

impl AsFd for Uffd

source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
source§

impl AsRawFd for Uffd

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for Uffd

source§

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

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

impl Drop for Uffd

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl FromRawFd for Uffd

source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

Constructs a new instance of Self from the given raw file descriptor. Read more
source§

impl IntoRawFd for Uffd

source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Uffd

§

impl Send for Uffd

§

impl Sync for Uffd

§

impl Unpin for Uffd

§

impl UnwindSafe for Uffd

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.