From 6f84f14927460ade47c5e04836c79c17fa9fc42c Mon Sep 17 00:00:00 2001 From: Jim Newsome Date: Mon, 5 Oct 2020 09:18:44 -0500 Subject: Add wrapper for mremap --- src/sys/mman.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/sys/mman.rs') diff --git a/src/sys/mman.rs b/src/sys/mman.rs index b2bed6e4..63a0779c 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -139,6 +139,17 @@ libc_bitflags!{ } } +#[cfg(target_os = "linux")] +libc_bitflags!{ + /// Options for `mremap()`. + pub struct MRemapFlags: c_int { + /// Permit the kernel to relocate the mapping to a new virtual address, if necessary. + MREMAP_MAYMOVE; + /// Place the mapping at exactly the address specified in `new_address`. + MREMAP_FIXED; + } +} + libc_enum!{ /// Usage information for a range of memory to allow for performance optimizations by the kernel. /// @@ -315,6 +326,30 @@ pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: Ma } } +/// Expands (or shrinks) an existing memory mapping, potentially moving it at +/// the same time. +/// +/// # Safety +/// +/// See the `mremap(2)` [man page](https://man7.org/linux/man-pages/man2/mremap.2.html) for +/// detailed requirements. +#[cfg(target_os = "linux")] +pub unsafe fn mremap( + addr: *mut c_void, + old_size: size_t, + new_size: size_t, + flags: MRemapFlags, + new_address: Option<* mut c_void>, +) -> Result<*mut c_void> { + let ret = libc::mremap(addr, old_size, new_size, flags.bits(), new_address.unwrap_or(std::ptr::null_mut())); + + if ret == libc::MAP_FAILED { + Err(Error::Sys(Errno::last())) + } else { + Ok(ret) + } +} + /// remove a mapping /// /// # Safety -- cgit v1.2.3