11//! POSIX shared memory
2+ //!
3+ //! # Example
4+ //!
5+ //! ```
6+ //! use rustix::fs::{ftruncate, Mode};
7+ //! use rustix::mm::{mmap, MapFlags, ProtFlags};
8+ //! use rustix::{io, shm};
9+ //! use std::mem::size_of;
10+ //! use std::ptr::null_mut;
11+ //!
12+ //! # fn example() -> io::Result<()> {
13+ //! // A type describing the data to be shared.
14+ //! #[repr(C)]
15+ //! struct MyBufferType {
16+ //! // ...
17+ //! }
18+ //!
19+ //! // Create the shared memory object.
20+ //! let shm_path = "/rustix-shm-example";
21+ //! let fd = shm::open(
22+ //! shm_path,
23+ //! shm::OFlags::CREATE | shm::OFlags::EXCL | shm::OFlags::RDWR,
24+ //! Mode::RUSR | Mode::WUSR,
25+ //! )?;
26+ //!
27+ //! // Resize the shared memory object to the size of our data.
28+ //! ftruncate(&fd, size_of::<MyBufferType>() as u64)?;
29+ //!
30+ //! // Map the shared memory object into our address space.
31+ //! //
32+ //! // SAFETY: We're creating a new mapping that's independent of any existing
33+ //! // memory allocations. There are interesting things to say about *using*
34+ //! // `ptr`, but that's for another safety comment.
35+ //! let ptr = unsafe {
36+ //! mmap(
37+ //! null_mut(),
38+ //! size_of::<MyBufferType>(),
39+ //! ProtFlags::READ | ProtFlags::WRITE,
40+ //! MapFlags::SHARED,
41+ //! &fd,
42+ //! 0,
43+ //! )?
44+ //! };
45+ //!
46+ //! // Use `ptr`...
47+ //!
48+ //! // Remove the shared memory object name.
49+ //! shm::unlink(shm_path)?;
50+ //! # Ok(())
51+ //! # }
52+ //! ```
253
354#![ allow( unused_qualifications) ]
455
@@ -8,13 +59,13 @@ use crate::{backend, io, path};
859use super :: shm;
960pub use crate :: backend:: fs:: types:: Mode ;
1061pub use crate :: backend:: shm:: types:: ShmOFlags as OFlags ;
11- #[ deprecated( note = "Use OFlags." ) ]
62+ #[ deprecated( note = "Use `shm:: OFlags` ." ) ]
1263#[ doc( hidden) ]
1364pub use crate :: backend:: shm:: types:: ShmOFlags ;
14- #[ deprecated( note = "Use open." ) ]
65+ #[ deprecated( note = "Use `shm:: open` ." ) ]
1566#[ doc( hidden) ]
1667pub use open as shm_open;
17- #[ deprecated( note = "Use unlink." ) ]
68+ #[ deprecated( note = "Use `shm:: unlink` ." ) ]
1869#[ doc( hidden) ]
1970pub use unlink as shm_unlink;
2071
0 commit comments