Skip to main content

common_memory/
common_memory.rs

1//! Uses one interface while selecting the best backend for the current OS.
2
3use native_ipc::memory::{NativeRegion, RegionOptions, WriterOwner, native_memory_capabilities};
4
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}