use rocketmq_error::RocketMQError;
use rocketmq_error::RocketMQResult;
pub const MADV_NORMAL: i32 = 0;
pub const MADV_RANDOM: i32 = 1;
pub const MADV_WILLNEED: i32 = 3;
pub const MADV_DONTNEED: i32 = 4;
#[inline]
pub fn get_page_size() -> usize {
page_size::get()
}
#[inline]
pub fn mlock(addr: *const u8, len: usize) -> RocketMQResult<()> {
#[cfg(unix)]
{
use std::ffi::c_void;
let result = unsafe { libc::mlock(addr as *const c_void, len) };
if result != 0 {
return Err(RocketMQError::StorageLockFailed {
path: "memory lock (mlock)".to_string(),
});
}
Ok(())
}
#[cfg(windows)]
{
use windows::Win32::System::Memory::VirtualLock;
let result = unsafe { VirtualLock(addr as _, len) };
result.map_err(|e| RocketMQError::StorageLockFailed {
path: format!("memory lock (VirtualLock): {}", e),
})?;
Ok(())
}
}
#[inline]
pub fn munlock(addr: *const u8, len: usize) -> RocketMQResult<()> {
#[cfg(unix)]
{
use std::ffi::c_void;
let result = unsafe { libc::munlock(addr as *const c_void, len) };
if result != 0 {
return Err(RocketMQError::StorageLockFailed {
path: "memory unlock (munlock)".to_string(),
});
}
Ok(())
}
#[cfg(windows)]
{
use windows::Win32::System::Memory::VirtualUnlock;
let result = unsafe { VirtualUnlock(addr as _, len) };
result.map_err(|e| RocketMQError::StorageLockFailed {
path: format!("memory unlock (VirtualUnlock): {}", e),
})?;
Ok(())
}
}
pub fn madvise(addr: *const u8, len: usize, advice: i32) -> i32 {
#[cfg(unix)]
{
use std::ffi::c_void;
unsafe { libc::madvise(addr as *mut c_void, len, advice) }
}
#[cfg(windows)]
{
0
}
}
pub fn mincore(addr: *const u8, len: usize, vec: *const u8) -> i32 {
#[cfg(target_os = "linux")]
{
use std::ffi::c_void;
use libc::c_uchar;
unsafe { libc::mincore(addr as *mut c_void, len, vec as *mut c_uchar) }
}
#[cfg(target_os = "macos")]
{
use std::ffi::c_void;
use libc::c_char;
unsafe { libc::mincore(addr as *mut c_void, len, vec as *mut c_char) }
}
#[cfg(target_os = "windows")]
{
0
}
}