From 89a0b05862f8be723130c6bcb5bb742ee30452f3 Mon Sep 17 00:00:00 2001 From: Andreas Fackler Date: Sat, 24 Mar 2018 15:18:10 +0100 Subject: [PATCH 1/2] add mlockall and munlockall --- CHANGELOG.md | 2 ++ src/sys/mman.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c7b9d24fd..d77a77179a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added +- Added `mlockall` and `munlockall` + ([#876](https://github.com/nix-rust/nix/pull/876)) - Added `SO_MARK` on Linux. - ([#873](https://github.com/nix-rust/nix/pull/873)) - Added `getsid` in `::nix::unistd` diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 9d1f88df89..4aac67ac32 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -205,6 +205,16 @@ libc_bitflags!{ } } +libc_bitflags!{ + /// Flags for `mlockall`. + pub struct MlockAllFlags: c_int { + /// Lock pages that are currently mapped into the address space of the process. + MCL_CURRENT; + /// Lock pages which will become mapped into the address space of the process in the future. + MCL_FUTURE; + } +} + pub unsafe fn mlock(addr: *const c_void, length: size_t) -> Result<()> { Errno::result(libc::mlock(addr, length)).map(drop) } @@ -213,6 +223,14 @@ pub unsafe fn munlock(addr: *const c_void, length: size_t) -> Result<()> { Errno::result(libc::munlock(addr, length)).map(drop) } +pub fn mlockall(flags: MlockAllFlags) -> Result<()> { + unsafe { Errno::result(libc::mlockall(flags.bits())) }.map(drop) +} + +pub fn munlockall() -> Result<()> { + unsafe { Errno::result(libc::munlockall()) }.map(drop) +} + /// Calls to mmap are inherently unsafe, so they must be made in an unsafe block. Typically /// a higher-level abstraction will hide the unsafe interactions with the mmap'd region. pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: MapFlags, fd: RawFd, offset: off_t) -> Result<*mut c_void> { From e262fd0b88e0a2e16228cebf8e3da0f2bc110c77 Mon Sep 17 00:00:00 2001 From: Andreas Fackler Date: Tue, 27 Mar 2018 16:24:14 +0200 Subject: [PATCH 2/2] add docs for memory locking functions --- src/sys/mman.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 4aac67ac32..f2cc699b04 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -215,18 +215,25 @@ libc_bitflags!{ } } +/// Locks all memory pages that contain part of the address range with `length` bytes starting at +/// `addr`. Locked pages never move to the swap area. pub unsafe fn mlock(addr: *const c_void, length: size_t) -> Result<()> { Errno::result(libc::mlock(addr, length)).map(drop) } +/// Unlocks all memory pages that contain part of the address range with `length` bytes starting at +/// `addr`. pub unsafe fn munlock(addr: *const c_void, length: size_t) -> Result<()> { Errno::result(libc::munlock(addr, length)).map(drop) } +/// Locks all memory pages mapped into this process' address space. Locked pages never move to the +/// swap area. pub fn mlockall(flags: MlockAllFlags) -> Result<()> { unsafe { Errno::result(libc::mlockall(flags.bits())) }.map(drop) } +/// Unlocks all memory pages mapped into this process' address space. pub fn munlockall() -> Result<()> { unsafe { Errno::result(libc::munlockall()) }.map(drop) }