indexbus_platform_ops/memory.rs
1//! Memory-related operational helpers.
2//!
3//! **Platform support**
4//! - Linux: uses `mlockall`.
5//! - Other OSes: functions return an error.
6//!
7//! **Operational note**
8//! `mlockall` may fail due to low `memlock` limits; see Linux `ulimit -l`/systemd
9//! `LimitMEMLOCK`.
10
11use crate::errors::{Error, Result};
12
13/// Attempts to lock current and future mappings into RAM (`mlockall`).
14///
15/// Linux-only; on other targets returns an error.
16///
17/// # Errors
18/// Returns an error if `mlockall` fails or is not supported on this platform.
19pub fn mlockall_current_and_future() -> Result<()> {
20 #[cfg(target_os = "linux")]
21 {
22 let flags = libc::MCL_CURRENT | libc::MCL_FUTURE;
23 let rc = unsafe { libc::mlockall(flags) };
24 if rc != 0 {
25 return Err(Error::msg(format!(
26 "mlockall(MCL_CURRENT|MCL_FUTURE) failed: {}",
27 std::io::Error::last_os_error()
28 )));
29 }
30 Ok(())
31 }
32
33 #[cfg(not(target_os = "linux"))]
34 {
35 Err(Error::msg("mlockall is only supported on Linux"))
36 }
37}