diff options
author | Jim Newsome <jnewsome@torproject.org> | 2020-10-05 09:18:44 -0500 |
---|---|---|
committer | Jim Newsome <jnewsome@torproject.org> | 2020-10-13 10:17:38 -0500 |
commit | 6f84f14927460ade47c5e04836c79c17fa9fc42c (patch) | |
tree | 45e7d6f0f664ae476795982c5e68b5b48b9ad4d5 /src/sys | |
parent | c8e8deff0e26a05165e66db17732794c2cd67324 (diff) | |
download | nix-6f84f14927460ade47c5e04836c79c17fa9fc42c.zip |
Add wrapper for mremap
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/mman.rs | 35 |
1 files changed, 35 insertions, 0 deletions
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 |