From f63d9cefadd6d53488f457f9e40de8872ddb7734 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Thu, 17 Mar 2022 14:03:45 -0600
Subject: [PATCH] Add mlock and friends

---
 src/header/sys_mman/mod.rs | 20 ++++++++++----------
 src/platform/linux/mod.rs  | 16 ++++++++++++++++
 src/platform/pal/mod.rs    |  8 ++++++++
 src/platform/redox/mod.rs  | 20 ++++++++++++++++++++
 4 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/src/header/sys_mman/mod.rs b/src/header/sys_mman/mod.rs
index de9f15eb..e0c1c711 100644
--- a/src/header/sys_mman/mod.rs
+++ b/src/header/sys_mman/mod.rs
@@ -24,14 +24,14 @@ pub const MS_ASYNC: c_int = 0x0001;
 pub const MS_INVALIDATE: c_int = 0x0002;
 pub const MS_SYNC: c_int = 0x0004;
 
-// #[no_mangle]
-pub extern "C" fn mlock(addr: *const c_void, len: usize) -> c_int {
-    unimplemented!();
+#[no_mangle]
+pub unsafe extern "C" fn mlock(addr: *const c_void, len: usize) -> c_int {
+    Sys::mlock(addr, len)
 }
 
-// #[no_mangle]
+#[no_mangle]
 pub extern "C" fn mlockall(flags: c_int) -> c_int {
-    unimplemented!();
+    Sys::mlockall(flags)
 }
 
 #[no_mangle]
@@ -56,14 +56,14 @@ pub unsafe extern "C" fn msync(addr: *mut c_void, len: size_t, flags: c_int) ->
     Sys::msync(addr, len, flags)
 }
 
-// #[no_mangle]
-pub extern "C" fn munlock(addr: *const c_void, len: usize) -> c_int {
-    unimplemented!();
+#[no_mangle]
+pub unsafe extern "C" fn munlock(addr: *const c_void, len: usize) -> c_int {
+    Sys::munlock(addr, len)
 }
 
-// #[no_mangle]
+#[no_mangle]
 pub extern "C" fn munlockall() -> c_int {
-    unimplemented!();
+    Sys::munlockall()
 }
 
 #[no_mangle]
diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs
index 1660ca29..c196be4a 100644
--- a/src/platform/linux/mod.rs
+++ b/src/platform/linux/mod.rs
@@ -304,6 +304,14 @@ impl Pal for Sys {
         e(unsafe { syscall!(MKNODAT, AT_FDCWD, path.as_ptr(), mode | S_IFIFO, 0) }) as c_int
     }
 
+    unsafe fn mlock(addr: *const c_void, len: usize) -> c_int {
+        e(syscall!(MLOCK, addr, len)) as c_int
+    }
+
+    fn mlockall(flags: c_int) -> c_int {
+        e(unsafe { syscall!(MLOCKALL, flags) }) as c_int
+    }
+
     unsafe fn mmap(
         addr: *mut c_void,
         len: usize,
@@ -323,6 +331,14 @@ impl Pal for Sys {
         e(syscall!(MSYNC, addr, len, flags)) as c_int
     }
 
+    unsafe fn munlock(addr: *const c_void, len: usize) -> c_int {
+        e(syscall!(MUNLOCK, addr, len)) as c_int
+    }
+
+    fn munlockall() -> c_int {
+        e(unsafe { syscall!(MUNLOCKALL) }) as c_int
+    }
+
     unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int {
         e(syscall!(MUNMAP, addr, len)) as c_int
     }
diff --git a/src/platform/pal/mod.rs b/src/platform/pal/mod.rs
index dd953b85..8317d2e5 100644
--- a/src/platform/pal/mod.rs
+++ b/src/platform/pal/mod.rs
@@ -114,6 +114,10 @@ pub trait Pal {
 
     fn mkfifo(path: &CStr, mode: mode_t) -> c_int;
 
+    unsafe fn mlock(addr: *const c_void, len: usize) -> c_int;
+
+    fn mlockall(flags: c_int) -> c_int;
+
     unsafe fn mmap(
         addr: *mut c_void,
         len: usize,
@@ -127,6 +131,10 @@ pub trait Pal {
 
     unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> c_int;
 
+    unsafe fn munlock(addr: *const c_void, len: usize) -> c_int;
+
+    fn munlockall() -> c_int;
+
     unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int;
 
     fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int;
diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs
index b236912e..4a227175 100644
--- a/src/platform/redox/mod.rs
+++ b/src/platform/redox/mod.rs
@@ -721,6 +721,16 @@ impl Pal for Sys {
         }
     }
 
+    unsafe fn mlock(addr: *const c_void, len: usize) -> c_int {
+        // Redox never swaps
+        0
+    }
+
+    fn mlockall(flags: c_int) -> c_int {
+        // Redox never swaps
+        0
+    }
+
     unsafe fn mmap(
         addr: *mut c_void,
         len: usize,
@@ -766,6 +776,16 @@ impl Pal for Sys {
         */
     }
 
+    unsafe fn munlock(addr: *const c_void, len: usize) -> c_int {
+        // Redox never swaps
+        0
+    }
+
+    fn munlockall() -> c_int {
+        // Redox never swaps
+        0
+    }
+
     unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int {
         if e(syscall::funmap(addr as usize, len)) == !0 {
             return !0;
-- 
GitLab