lx 0.4.0

A no_std crate to use Linux system calls
Documentation
use crate::{
    iovec,
    result_from_value,
    unit_result_from_value,
    AsRawFd,
};

use super::abi::*;

pub const MADV_NORMAL: u32 = 0;
pub const MADV_RANDOM: u32 = 1;
pub const MADV_SEQUENTIAL: u32 = 2;
pub const MADV_WILLNEED: u32 = 3;
pub const MADV_DONTNEED: u32 = 4;
pub const MADV_FREE: u32 = 8;
pub const MADV_REMOVE: u32 = 9;
pub const MADV_DONTFORK: u32 = 10;
pub const MADV_DOFORK: u32 = 11;
pub const MADV_MERGEABLE: u32 = 12;
pub const MADV_UNMERGEABLE: u32 = 13;
pub const MADV_HUGEPAGE: u32 = 14;
pub const MADV_NOHUGEPAGE: u32 = 15;
pub const MADV_DONTDUMP: u32 = 16;
pub const MADV_DODUMP: u32 = 17;
pub const MADV_WIPEONFORK: u32 = 18;
pub const MADV_KEEPONFORK: u32 = 19;
pub const MADV_COLD: u32 = 20;
pub const MADV_PAGEOUT: u32 = 21;
pub const MADV_POPULATE_READ: u32 = 22;
pub const MADV_POPULATE_WRITE: u32 = 23;
pub const MADV_DONTNEED_LOCKED: u32 = 24;
pub const MADV_COLLAPSE: u32 = 25;
pub const MADV_HWPOISON: u32 = 100;

/// Give advice about a memory range.
///
/// # Safety
///
/// The application must ensure that applying the advice is safe. For example, a `MADV_DONTNEED`
/// advice on a memory range in the heap could lead to memory corruption.
#[inline]
pub unsafe fn madvise(mem: *mut [u8], advice: u32) -> crate::Result<()> {
    let mem = &mut *mem;
    let ret = syscall_3(28, mem.as_mut_ptr() as usize, mem.len(), advice as usize) as i32;
    unit_result_from_value(ret)
}

/// Give advice about a memory range in a process.
///
/// # Safety
///
/// The application must ensure that applying the advice is safe. For example, a `MADV_DONTNEED`
/// advice on a memory range in the current process' heap could lead to memory corruption.
#[inline]
pub unsafe fn process_madvise(
    pid_fd: &impl AsRawFd,
    mem: &[iovec],
    advice: u32,
    flags: u32,
) -> crate::Result<usize> {
    let ret = syscall_5(
        440,
        pid_fd.as_raw_fd() as usize,
        mem.as_ptr() as usize,
        mem.len(),
        advice as usize,
        flags as usize,
    );
    result_from_value(ret)
}