1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
use libc::{PROT_EXEC, PROT_NONE, PROT_READ, PROT_WRITE}; use std::io; use {Error, Protection, Result}; impl Protection { fn to_native(self) -> ::libc::c_int { let mut result = PROT_NONE; let prots = [ (Protection::READ, PROT_READ), (Protection::WRITE, PROT_WRITE), (Protection::EXECUTE, PROT_EXEC), ]; for &(prot, unix_flag) in &prots { if self.contains(prot) { result |= unix_flag; } } result } } pub fn page_size() -> usize { unsafe { ::libc::sysconf(::libc::_SC_PAGESIZE) as usize } } pub fn set_protection(base: *const u8, size: usize, protection: Protection) -> Result<()> { let result = unsafe { ::libc::mprotect(base as *mut ::libc::c_void, size, protection.to_native()) }; match result { 0 => Ok(()), _ => Err(Error::SystemCall(io::Error::last_os_error())), } } pub fn lock(base: *const u8, size: usize) -> Result<()> { let result = unsafe { ::libc::mlock(base as *const ::libc::c_void, size) }; match result { 0 => Ok(()), _ => Err(Error::SystemCall(io::Error::last_os_error())), } } pub fn unlock(base: *const u8, size: usize) -> Result<()> { let result = unsafe { ::libc::munlock(base as *const ::libc::c_void, size) }; match result { 0 => Ok(()), _ => Err(Error::SystemCall(io::Error::last_os_error())), } }