pub struct Shm { /* private fields */ }
Implementations§
Source§impl Shm
impl Shm
Sourcepub fn open(name: &str, oflags: OFlags, mode: Mode) -> Result<Self>
pub fn open(name: &str, oflags: OFlags, mode: Mode) -> Result<Self>
Opens shared memory at name
.
Sourcepub fn set_size(&mut self, size: usize) -> Result<()>
pub fn set_size(&mut self, size: usize) -> Result<()>
Sets the size of the shared memory with ftruncate
.
pub fn name(&self) -> &str
Sourcepub unsafe fn map(&mut self, offset: usize) -> Result<BorrowedMap<'_>>
pub unsafe fn map(&mut self, offset: usize) -> Result<BorrowedMap<'_>>
Try to create a memmap2::MmapMut
by which we can read and write to this shared memory
object. The provided offset
may not be greater than or equal to the value returned by
Self::size
(if it is, this function will return an error).
This function is generally only useful if one has already called Self::set_size
. If one
hasn’t, this function will return a mapped area with a length of 0, so writing to and
reading from it will either fail or do nothing.
§Safety
This is unsafe due to the fundamental nature of memory shared between processes. The
documentation for memmap2::MmapOptions::map_mut
can share more details, but doesn’t
paint the whole picture. We aren’t using this to map a file, but rather just a chunk of
memory that can be shared between processes. Because this can be shared between processes
and the changes from one process are immediately visible from a different process, this
very easily allows one to run into use-after-free issues if they are not safe.
There is no way to prevent this sort of intra-process borrow-dependency in safe rust, so
one must simply be safe when using this. However, we do prevent one from easily
accidentally making two mmaps from the same Shm
by modeling the fact that the map
borrows from the Shm in the type system with the BorrowedMap
type.