shmem-bind
A safe and idiomatic wrapper over shared memory APIs in rust with proper cleanups.
Quick start:
check the message-passing example for better understanding
Semantics:
Allocation:
import shared memory abstractions:
use ;
in order to create new shared memory, use the following builder snippet:
let shared_mem = new
.with_size
.open?;
this will allocate a shared memory file with the specified size if the shared memory is not present on the machine.
the handle, here shared_mem, would claim ownership of the shared memory if the shared memory is not present and created via call to open function.
this is useful information for cleanup process since there is only one owner for each shared memory and only the owner can and will unlink the shared memory.
you can wrap the shared memory configuration into a ShmemBox<T> via call to boxed function.
let boxed_val = unsafe ;
call to this function is inherently unsafe since there is no guarantee that the memory behind the pointer is initialized or valid.
type NotZeroI32 = i32;
let boxed_val = unsafe ;
the ShmemBox type implements Deref and DerefMut so you can use all the rust semantics and guarantee of T in your code
Cleanup:
When the variable goes out of scope, the drop implementation is called. if the shared memory is owned, i.e. shared memory is created by this handle, the shared memory would unlink.
in order to prevent this, you can use the ShmemBox::leak method:
;
you can also use the ShmemBox::own to ensure cleanup of the shared memory:
;
ShmemBox<T> implements Sync and Send if the underlying T implements Sync and Send respectively.