shared_memory 0.1.0

A wrapper around native shared memory for use across processes
Documentation

shared_memory

Build Status crates.io mio Lines of Code

This crate provides a simple interface to shared memory OS APIs.

Shared memory is well suited for sharing large amounts of data between processes as it relies purely on memory accesses. Other than when managing concurent access through locks/events, reading and writing memory from a SharedMem relies only on CPU features (the operating system is not involved, no context switches like system calls, etc...).

Usage

Writer based on examples/create.rs

//Creates a new SharedMem link "shared_mem.link" that points to shared memory of size 4096
let mut my_shmem: SharedMem = match SharedMem::create(
  PathBuf::from("shared_mem.link"),
  LockType::Mutex, //Concurent accesses will be managed by a mutex
  4096
).unwrap();

//Acquire write lock
{
    let mut shared_data: WriteLockGuardSlice<u8> = match my_shmem.wlock_as_slice().unwrap();
    let src = b"Hello World !\x00";
    shared_data[0..src.len()].copy_from_slice(src);
}

Reader based on examples/open.rs

// Open an existing SharedMem link named "shared_mem.link"
let mut my_shmem: SharedMem = match SharedMem::open(PathBuf::from("shared_mem.link")).unwrap();
//Aquire Read lock
{
   let mut shared_data = match my_shmem.rlock_as_slice::<u8>().unwrap();
   //Print the content of the shared memory as chars
   for byte in &shared_data[0..256] {
       if *byte == 0 { break; }
       print!("{}", *byte as char);
   }
}

Operating System Support

Feature Description Linux Windows Mac[1]
SharedMem.create/open Create/open a SharedMem X
SharedMem.*_raw Create/open a raw shared memory map X
LockType::Mutex Mutually exclusive lock X
LockType::RwLock Exlusive write/shared read X[2] X

[1] I do not own a Mac so cannot implement that side of things myself. Contributions are welcome !

[2] Windows provides no default implementation of Rwlock that is safe to share between processes. See Issue #1

License

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.