diff options
author | Jonathan <jonathanwoollettlight@gmail.com> | 2022-11-20 17:16:21 +0000 |
---|---|---|
committer | Jonathan <jonathanwoollettlight@gmail.com> | 2022-11-20 17:16:25 +0000 |
commit | d34696c84b0d3b49456fa6f0f12af91d94b11371 (patch) | |
tree | 60f52186b23a99f2a5abc1fa44adb34c5974a64b /src/sys | |
parent | fbebb21dd8df447a1408795b4b5706d9ca6c55df (diff) | |
download | nix-d34696c84b0d3b49456fa6f0f12af91d94b11371.zip |
mmap addr
Diffstat (limited to 'src/sys')
-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) |