Skip to main content

NativeRegion

Struct NativeRegion 

Source
pub struct NativeRegion { /* private fields */ }
Expand description

Common owner of the best private native shared-memory object on this target.

It exposes logical initialization bytes only through a closure. Consuming NativeRegion::prepare_for_sharing hands the region to the existing authenticated platform transfer typestate, which applies real native seals and exact permissions. Growth and arbitrary permission changes are no longer possible after that transition.

Implementations§

Source§

impl NativeRegion

Source

pub fn allocate(options: RegionOptions) -> Result<Self, MemoryError>

Allocates a zeroed, anonymous, non-executable native region.

Examples found in repository?
examples/common_memory.rs (line 8)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let capabilities = native_memory_capabilities();
7    let options = RegionOptions::growable(128, 4096, WriterOwner::Creator);
8    let mut region = NativeRegion::allocate(options)?;
9
10    region.initialize(|bytes| bytes[..8].copy_from_slice(b"NIPCDEMO"));
11    region.grow(512)?;
12    region.initialize(|bytes| assert_eq!(&bytes[..8], b"NIPCDEMO"));
13
14    let status = region.status();
15    println!(
16        "backend={:?} authority={:?} logical={} mapped={} maximum={}",
17        capabilities.platform(),
18        capabilities.authority_mechanism(),
19        status.logical_len,
20        status.mapped_len,
21        status.maximum_len,
22    );
23
24    region.clear();
25    region.initialize(|bytes| bytes[..8].copy_from_slice(b"REUSABLE"));
26
27    // `destroy` explicitly clears the complete mapping before releasing it.
28    // Use `prepare_for_sharing` instead when handing the region to the
29    // authenticated platform transfer typestate.
30    region.destroy();
31    Ok(())
32}
Source

pub const fn options(&self) -> RegionOptions

Returns the immutable allocation and authority policy.

Source

pub fn status(&self) -> RegionStatus

Returns a portable status snapshot.

Examples found in repository?
examples/common_memory.rs (line 14)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let capabilities = native_memory_capabilities();
7    let options = RegionOptions::growable(128, 4096, WriterOwner::Creator);
8    let mut region = NativeRegion::allocate(options)?;
9
10    region.initialize(|bytes| bytes[..8].copy_from_slice(b"NIPCDEMO"));
11    region.grow(512)?;
12    region.initialize(|bytes| assert_eq!(&bytes[..8], b"NIPCDEMO"));
13
14    let status = region.status();
15    println!(
16        "backend={:?} authority={:?} logical={} mapped={} maximum={}",
17        capabilities.platform(),
18        capabilities.authority_mechanism(),
19        status.logical_len,
20        status.mapped_len,
21        status.maximum_len,
22    );
23
24    region.clear();
25    region.initialize(|bytes| bytes[..8].copy_from_slice(b"REUSABLE"));
26
27    // `destroy` explicitly clears the complete mapping before releasing it.
28    // Use `prepare_for_sharing` instead when handing the region to the
29    // authenticated platform transfer typestate.
30    region.destroy();
31    Ok(())
32}
Source

pub fn initialize<R>(&mut self, operation: impl FnOnce(&mut [u8]) -> R) -> R

Runs one initialization operation over logical bytes only.

Page-rounded padding remains zero and inaccessible through this common method. No capability has escaped while the closure runs.

Examples found in repository?
examples/common_memory.rs (line 10)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let capabilities = native_memory_capabilities();
7    let options = RegionOptions::growable(128, 4096, WriterOwner::Creator);
8    let mut region = NativeRegion::allocate(options)?;
9
10    region.initialize(|bytes| bytes[..8].copy_from_slice(b"NIPCDEMO"));
11    region.grow(512)?;
12    region.initialize(|bytes| assert_eq!(&bytes[..8], b"NIPCDEMO"));
13
14    let status = region.status();
15    println!(
16        "backend={:?} authority={:?} logical={} mapped={} maximum={}",
17        capabilities.platform(),
18        capabilities.authority_mechanism(),
19        status.logical_len,
20        status.mapped_len,
21        status.maximum_len,
22    );
23
24    region.clear();
25    region.initialize(|bytes| bytes[..8].copy_from_slice(b"REUSABLE"));
26
27    // `destroy` explicitly clears the complete mapping before releasing it.
28    // Use `prepare_for_sharing` instead when handing the region to the
29    // authenticated platform transfer typestate.
30    region.destroy();
31    Ok(())
32}
Source

pub fn clear(&mut self)

Clears the complete page-rounded mapping and retains it for reuse.

Every byte, including native page padding, is overwritten through the live mapping before this method returns.

Examples found in repository?
examples/common_memory.rs (line 24)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let capabilities = native_memory_capabilities();
7    let options = RegionOptions::growable(128, 4096, WriterOwner::Creator);
8    let mut region = NativeRegion::allocate(options)?;
9
10    region.initialize(|bytes| bytes[..8].copy_from_slice(b"NIPCDEMO"));
11    region.grow(512)?;
12    region.initialize(|bytes| assert_eq!(&bytes[..8], b"NIPCDEMO"));
13
14    let status = region.status();
15    println!(
16        "backend={:?} authority={:?} logical={} mapped={} maximum={}",
17        capabilities.platform(),
18        capabilities.authority_mechanism(),
19        status.logical_len,
20        status.mapped_len,
21        status.maximum_len,
22    );
23
24    region.clear();
25    region.initialize(|bytes| bytes[..8].copy_from_slice(b"REUSABLE"));
26
27    // `destroy` explicitly clears the complete mapping before releasing it.
28    // Use `prepare_for_sharing` instead when handing the region to the
29    // authenticated platform transfer typestate.
30    region.destroy();
31    Ok(())
32}
Source

pub fn grow(&mut self, requested: usize) -> Result<(), MemoryError>

Grows by replacing the still-private mapping and preserving logical bytes.

Examples found in repository?
examples/common_memory.rs (line 11)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let capabilities = native_memory_capabilities();
7    let options = RegionOptions::growable(128, 4096, WriterOwner::Creator);
8    let mut region = NativeRegion::allocate(options)?;
9
10    region.initialize(|bytes| bytes[..8].copy_from_slice(b"NIPCDEMO"));
11    region.grow(512)?;
12    region.initialize(|bytes| assert_eq!(&bytes[..8], b"NIPCDEMO"));
13
14    let status = region.status();
15    println!(
16        "backend={:?} authority={:?} logical={} mapped={} maximum={}",
17        capabilities.platform(),
18        capabilities.authority_mechanism(),
19        status.logical_len,
20        status.mapped_len,
21        status.maximum_len,
22    );
23
24    region.clear();
25    region.initialize(|bytes| bytes[..8].copy_from_slice(b"REUSABLE"));
26
27    // `destroy` explicitly clears the complete mapping before releasing it.
28    // Use `prepare_for_sharing` instead when handing the region to the
29    // authenticated platform transfer typestate.
30    region.destroy();
31    Ok(())
32}
Source

pub fn close(self)

Explicitly clears according to policy and releases the anonymous object.

Source

pub fn destroy(self)

Overwrites the complete mapping, then explicitly releases it.

This ignores CleanupPolicy and always performs the clearing pass. It cannot erase copies previously made by a process, the kernel, or a device. The region must still be quiescent and exclusively owned here; shared regions are destroyed by their transferred lifecycle owner.

Examples found in repository?
examples/common_memory.rs (line 30)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let capabilities = native_memory_capabilities();
7    let options = RegionOptions::growable(128, 4096, WriterOwner::Creator);
8    let mut region = NativeRegion::allocate(options)?;
9
10    region.initialize(|bytes| bytes[..8].copy_from_slice(b"NIPCDEMO"));
11    region.grow(512)?;
12    region.initialize(|bytes| assert_eq!(&bytes[..8], b"NIPCDEMO"));
13
14    let status = region.status();
15    println!(
16        "backend={:?} authority={:?} logical={} mapped={} maximum={}",
17        capabilities.platform(),
18        capabilities.authority_mechanism(),
19        status.logical_len,
20        status.mapped_len,
21        status.maximum_len,
22    );
23
24    region.clear();
25    region.initialize(|bytes| bytes[..8].copy_from_slice(b"REUSABLE"));
26
27    // `destroy` explicitly clears the complete mapping before releasing it.
28    // Use `prepare_for_sharing` instead when handing the region to the
29    // authenticated platform transfer typestate.
30    region.destroy();
31    Ok(())
32}
Source

pub fn prepare_for_sharing(self) -> NativeShareRequest

Freezes common initialization/growth and creates a native share request.

The request retains the permission and mandatory seal policy alongside the native region. Its platform parts must be consumed by the matching authenticated prepare/transfer operation.

Trait Implementations§

Source§

impl Drop for NativeRegion

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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.