Skip to content

Commit 584d5a5

Browse files
committed
Update mman related docs
- Add docs for `memfd_create`, `shm_open`, and `shm_unlink` - Add man-page links - Remove` #[allow(missing_docs)]`on the `memfd` module
1 parent 055818a commit 584d5a5

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
lines changed

src/sys/memfd.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1+
//! Interfaces for managing memory-backed files.
2+
13
use std::os::unix::io::RawFd;
24
use crate::Result;
35
use crate::errno::Errno;
46
use std::ffi::CStr;
57

68
libc_bitflags!(
9+
/// Options that change the behavior of [`memfd_create`].
710
pub struct MemFdCreateFlag: libc::c_uint {
11+
/// Set the close-on-exec ([`FD_CLOEXEC`]) flag on the new file descriptor.
12+
///
13+
/// By default, the new file descriptor is set to remain open across an [`execve`]
14+
/// (the `FD_CLOEXEC` flag is initially disabled). This flag can be used to change
15+
/// this default. The file offset is set to the beginning of the file (see [`lseek`]).
16+
///
17+
/// See also the description of the `O_CLOEXEC` flag in [`open(2)`].
18+
///
19+
/// [`execve`]: crate::unistd::execve
20+
/// [`lseek`]: crate::unistd::lseek
21+
/// [`FD_CLOEXEC`]: crate::fcntl::FdFlag::FD_CLOEXEC
22+
/// [`open(2)`]: https://man7.org/linux/man-pages/man2/open.2.html
823
MFD_CLOEXEC;
24+
/// Allow sealing operations on this file.
25+
///
26+
/// See also the file sealing notes given in [`memfd_create(2)`].
27+
///
28+
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
929
MFD_ALLOW_SEALING;
1030
}
1131
);
1232

33+
/// Creates an anonymous file that lives in memory, and return a file-descriptor to it.
34+
///
35+
/// The file behaves like a regular file, and so can be modified, truncated, memory-mapped, and so on.
36+
/// However, unlike a regular file, it lives in RAM and has a volatile backing storage.
37+
///
38+
/// For more information, see [`memfd_create(2)`].
39+
///
40+
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
1341
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
1442
let res = unsafe {
1543
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())

src/sys/mman.rs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Memory management declarations.
2+
13
use crate::Result;
24
#[cfg(not(target_os = "android"))]
35
use crate::NixPath;
@@ -30,7 +32,7 @@ libc_bitflags!{
3032
}
3133

3234
libc_bitflags!{
33-
/// Additional parameters for `mmap()`.
35+
/// Additional parameters for [`mmap`].
3436
pub struct MapFlags: c_int {
3537
/// Compatibility flag. Ignored.
3638
MAP_FILE;
@@ -151,7 +153,7 @@ libc_bitflags!{
151153

152154
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
153155
libc_bitflags!{
154-
/// Options for `mremap()`.
156+
/// Options for [`mremap`].
155157
pub struct MRemapFlags: c_int {
156158
/// Permit the kernel to relocate the mapping to a new virtual address, if necessary.
157159
#[cfg(target_os = "linux")]
@@ -171,7 +173,7 @@ libc_bitflags!{
171173
libc_enum!{
172174
/// Usage information for a range of memory to allow for performance optimizations by the kernel.
173175
///
174-
/// Used by [`madvise`](./fn.madvise.html).
176+
/// Used by [`madvise`].
175177
#[repr(i32)]
176178
#[non_exhaustive]
177179
pub enum MmapAdvise {
@@ -264,7 +266,7 @@ libc_enum!{
264266
}
265267

266268
libc_bitflags!{
267-
/// Configuration flags for `msync`.
269+
/// Configuration flags for [`msync`].
268270
pub struct MsFlags: c_int {
269271
/// Schedule an update but return immediately.
270272
MS_ASYNC;
@@ -282,7 +284,7 @@ libc_bitflags!{
282284
}
283285

284286
libc_bitflags!{
285-
/// Flags for `mlockall`.
287+
/// Flags for [`mlockall`].
286288
pub struct MlockAllFlags: c_int {
287289
/// Lock pages that are currently mapped into the address space of the process.
288290
MCL_CURRENT;
@@ -298,7 +300,9 @@ libc_bitflags!{
298300
///
299301
/// # Safety
300302
///
301-
/// `addr` must meet all the requirements described in the `mlock(2)` man page.
303+
/// `addr` must meet all the requirements described in the [`mlock(2)`] man page.
304+
///
305+
/// [`mlock(2)`]: https://man7.org/linux/man-pages/man2/mlock.2.html
302306
pub unsafe fn mlock(addr: *const c_void, length: size_t) -> Result<()> {
303307
Errno::result(libc::mlock(addr, length)).map(drop)
304308
}
@@ -308,25 +312,28 @@ pub unsafe fn mlock(addr: *const c_void, length: size_t) -> Result<()> {
308312
///
309313
/// # Safety
310314
///
311-
/// `addr` must meet all the requirements described in the `munlock(2)` man
315+
/// `addr` must meet all the requirements described in the [`munlock(2)`] man
312316
/// page.
317+
///
318+
/// [`munlock(2)`]: https://man7.org/linux/man-pages/man2/munlock.2.html
313319
pub unsafe fn munlock(addr: *const c_void, length: size_t) -> Result<()> {
314320
Errno::result(libc::munlock(addr, length)).map(drop)
315321
}
316322

317323
/// Locks all memory pages mapped into this process' address space.
318324
///
319-
/// Locked pages never move to the swap area.
325+
/// Locked pages never move to the swap area. For more information, see [`mlockall(2)`].
320326
///
321-
/// # Safety
322-
///
323-
/// `addr` must meet all the requirements described in the `mlockall(2)` man
324-
/// page.
327+
/// [`mlockall(2)`]: https://man7.org/linux/man-pages/man2/mlockall.2.html
325328
pub fn mlockall(flags: MlockAllFlags) -> Result<()> {
326329
unsafe { Errno::result(libc::mlockall(flags.bits())) }.map(drop)
327330
}
328331

329332
/// Unlocks all memory pages mapped into this process' address space.
333+
///
334+
/// For more information, see [`munlockall(2)`].
335+
///
336+
/// [`munlockall(2)`]: https://man7.org/linux/man-pages/man2/munlockall.2.html
330337
pub fn munlockall() -> Result<()> {
331338
unsafe { Errno::result(libc::munlockall()) }.map(drop)
332339
}
@@ -335,7 +342,9 @@ pub fn munlockall() -> Result<()> {
335342
///
336343
/// # Safety
337344
///
338-
/// See the `mmap(2)` man page for detailed requirements.
345+
/// See the [`mmap(2)`] man page for detailed requirements.
346+
///
347+
/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
339348
pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: MapFlags, fd: RawFd, offset: off_t) -> Result<*mut c_void> {
340349
let ret = libc::mmap(addr, length, prot.bits(), flags.bits(), fd, offset);
341350

@@ -383,8 +392,10 @@ pub unsafe fn mremap(
383392
///
384393
/// # Safety
385394
///
386-
/// `addr` must meet all the requirements described in the `munmap(2)` man
395+
/// `addr` must meet all the requirements described in the [`munmap(2)`] man
387396
/// page.
397+
///
398+
/// [`munmap(2)`]: https://man7.org/linux/man-pages/man2/munmap.2.html
388399
pub unsafe fn munmap(addr: *mut c_void, len: size_t) -> Result<()> {
389400
Errno::result(libc::munmap(addr, len)).map(drop)
390401
}
@@ -393,8 +404,10 @@ pub unsafe fn munmap(addr: *mut c_void, len: size_t) -> Result<()> {
393404
///
394405
/// # Safety
395406
///
396-
/// See the `madvise(2)` man page. Take special care when using
397-
/// `MmapAdvise::MADV_FREE`.
407+
/// See the [`madvise(2)`] man page. Take special care when using
408+
/// [`MmapAdvise::MADV_FREE`].
409+
///
410+
/// [`madvise(2)`]: https://man7.org/linux/man-pages/man2/madvise.2.html
398411
pub unsafe fn madvise(addr: *mut c_void, length: size_t, advise: MmapAdvise) -> Result<()> {
399412
Errno::result(libc::madvise(addr, length, advise as i32)).map(drop)
400413
}
@@ -432,12 +445,19 @@ pub unsafe fn mprotect(addr: *mut c_void, length: size_t, prot: ProtFlags) -> Re
432445
///
433446
/// # Safety
434447
///
435-
/// `addr` must meet all the requirements described in the `msync(2)` man
448+
/// `addr` must meet all the requirements described in the [`msync(2)`] man
436449
/// page.
450+
///
451+
/// [`msync(2)`]: https://man7.org/linux/man-pages/man2/msync.2.html
437452
pub unsafe fn msync(addr: *mut c_void, length: size_t, flags: MsFlags) -> Result<()> {
438453
Errno::result(libc::msync(addr, length, flags.bits())).map(drop)
439454
}
440455

456+
/// Creates and opens a new, or opens an existing, POSIX shared memory object.
457+
///
458+
/// For more information, see [`shm_open(3)`].
459+
///
460+
/// [`shm_open(3)`]: https://man7.org/linux/man-pages/man3/shm_open.3.html
441461
#[cfg(not(target_os = "android"))]
442462
pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Result<RawFd> {
443463
let ret = name.with_nix_path(|cstr| {
@@ -454,6 +474,11 @@ pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Resul
454474
Errno::result(ret)
455475
}
456476

477+
/// Performs the converse of [`shm_open`], removing an object previously created.
478+
///
479+
/// For more information, see [`shm_unlink(3)`].
480+
///
481+
/// [`shm_unlink(3)`]: https://man7.org/linux/man-pages/man3/shm_unlink.3.html
457482
#[cfg(not(target_os = "android"))]
458483
pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> {
459484
let ret = name.with_nix_path(|cstr| {

src/sys/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub mod eventfd;
3838
pub mod ioctl;
3939

4040
#[cfg(target_os = "linux")]
41-
#[allow(missing_docs)]
4241
pub mod memfd;
4342

4443
#[cfg(not(target_os = "redox"))]

0 commit comments

Comments
 (0)