diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-11-20 21:48:28 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-20 21:48:28 +0000 |
commit | 6e2b0a02e662a83c1748dda9825dc96da5374f6f (patch) | |
tree | d9bd34d4f9d9212ccec761e056ed184e250853bf /src | |
parent | 2ad2f48693b7ecb262d4f887573d1170f4f79133 (diff) | |
parent | d34696c84b0d3b49456fa6f0f12af91d94b11371 (diff) | |
download | nix-6e2b0a02e662a83c1748dda9825dc96da5374f6f.zip |
Merge #1870
1870: mmap addr r=asomers a=JonathanWoollett-Light
Uses `Some<size_t>` instead of `*mut c_void` for the `addr` passed to [`sys::mman::mmap`](https://docs.rs/nix/latest/nix/sys/mman/fn.mmap.html).
In this instance we are not usefully passing a pointer, it will never be dereferenced. We are passing a location which represents where to attach the shared memory to.
In this case `size_t` better represents an address and not a pointer, and `Option<size_t>` better represents an optional argument than `NULLPTR`.
In C since there is no optional type this is a pointer as this allows it be null which is an alias here for `None`.
Co-authored-by: Jonathan <jonathanwoollettlight@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/sys/mman.rs | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 869f44c4..dab8f445 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -8,7 +8,7 @@ use crate::Result; #[cfg(feature = "fs")] use crate::{fcntl::OFlag, sys::stat::Mode}; use libc::{self, c_int, c_void, off_t, size_t}; -use std::os::unix::io::RawFd; +use std::{os::unix::io::RawFd, num::NonZeroUsize}; libc_bitflags! { /// Desired memory protection of a memory mapping. @@ -417,14 +417,19 @@ pub fn munlockall() -> Result<()> { /// /// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html pub unsafe fn mmap( - addr: *mut c_void, + addr: Option<NonZeroUsize>, length: size_t, prot: ProtFlags, flags: MapFlags, fd: RawFd, offset: off_t, ) -> Result<*mut c_void> { - let ret = libc::mmap(addr, length, prot.bits(), flags.bits(), fd, offset); + let ptr = addr.map_or( + std::ptr::null_mut(), + |a| usize::from(a) as *mut c_void + ); + + let ret = libc::mmap(ptr, length, prot.bits(), flags.bits(), fd, offset); if ret == libc::MAP_FAILED { Err(Errno::last()) @@ -516,7 +521,7 @@ pub unsafe fn madvise( /// # use std::ptr; /// const ONE_K: size_t = 1024; /// let mut slice: &mut [u8] = unsafe { -/// let mem = mmap(ptr::null_mut(), ONE_K, ProtFlags::PROT_NONE, +/// let mem = mmap(None, ONE_K, ProtFlags::PROT_NONE, /// MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, -1, 0).unwrap(); /// mprotect(mem, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap(); /// std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K) |