Function nc::madvise

source ·
pub unsafe fn madvise(
    addr: usize,
    len: size_t,
    advice: i32
) -> Result<(), Errno>
Expand description

Give advice about use of memory.

§Example

// Initialize an anonymous mapping with 4 pages.
let map_length = 4 * nc::PAGE_SIZE;
let addr = unsafe {
    nc::mmap(
        0,
        map_length,
        nc::PROT_READ | nc::PROT_WRITE,
        nc::MAP_PRIVATE | nc::MAP_ANONYMOUS,
        -1,
        0,
    )
};
assert!(addr.is_ok());
let addr = addr.unwrap();

// Set the third page readonly. And we will run into SIGSEGV when updating it.
let ret = unsafe { nc::madvise(addr + 2 * nc::PAGE_SIZE, nc::PAGE_SIZE, nc::MADV_RANDOM) };
assert!(ret.is_ok());

let ret = unsafe { nc::munmap(addr, map_length) };
assert!(ret.is_ok());