extern crate libc;
use std::error::Error;
use std::io;
use std::fmt;
use libc::{c_void, c_int};
use std::ops::Drop;
use std::ptr;
use self::MemoryMapKind::*;
use self::MapOption::*;
use self::MapError::*;
#[cfg(windows)]
use std::mem;
fn errno() -> i32 {
io::Error::last_os_error().raw_os_error().unwrap_or(-1)
}
#[cfg(unix)]
fn page_size() -> usize {
unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
}
#[cfg(windows)]
fn page_size() -> usize {
unsafe {
let mut info = mem::zeroed();
libc::GetSystemInfo(&mut info);
return info.dwPageSize as usize;
}
}
pub struct MemoryMap {
data: *mut u8,
len: usize,
kind: MemoryMapKind,
}
#[allow(raw_pointer_derive)]
#[derive(Copy, Clone)]
pub enum MemoryMapKind {
MapFile(*const u8),
MapVirtual
}
#[allow(raw_pointer_derive)]
#[derive(Copy, Clone)]
pub enum MapOption {
MapReadable,
MapWritable,
MapExecutable,
MapAddr(*const u8),
#[cfg(windows)]
MapFd(libc::HANDLE),
#[cfg(not(windows))]
MapFd(c_int),
MapOffset(usize),
MapNonStandardFlags(c_int),
}
#[derive(Copy, Clone, Debug)]
pub enum MapError {
ErrFdNotAvail,
ErrInvalidFd,
ErrUnaligned,
ErrNoMapSupport,
ErrNoMem,
ErrZeroLength,
ErrUnknown(isize),
ErrUnsupProt,
ErrUnsupOffset,
ErrAlreadyExists,
ErrVirtualAlloc(i32),
ErrCreateFileMappingW(i32),
ErrMapViewOfFile(i32)
}
impl fmt::Display for MapError {
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
ErrFdNotAvail => "fd not available for reading or writing",
ErrInvalidFd => "Invalid fd",
ErrUnaligned => {
"Unaligned address, invalid flags, negative length or \
unaligned offset"
}
ErrNoMapSupport=> "File doesn't support mapping",
ErrNoMem => "Invalid address, or not enough available memory",
ErrUnsupProt => "Protection mode unsupported",
ErrUnsupOffset => "Offset in virtual memory mode is unsupported",
ErrAlreadyExists => "File mapping for specified file already exists",
ErrZeroLength => "Zero-length mapping not allowed",
ErrUnknown(code) => {
return write!(out, "Unknown error = {}", code)
},
ErrVirtualAlloc(code) => {
return write!(out, "VirtualAlloc failure = {}", code)
},
ErrCreateFileMappingW(code) => {
return write!(out, "CreateFileMappingW failure = {}", code)
},
ErrMapViewOfFile(code) => {
return write!(out, "MapViewOfFile failure = {}", code)
}
};
write!(out, "{}", str)
}
}
impl Error for MapError {
fn description(&self) -> &str { "memory map error" }
}
fn round_up(from: usize, to: usize) -> usize {
let r = if from % to == 0 {
from
} else {
from + to - (from % to)
};
if r == 0 {
to
} else {
r
}
}
#[cfg(unix)]
impl MemoryMap {
pub fn new(min_len: usize, options: &[MapOption]) -> Result<MemoryMap, MapError> {
use libc::off_t;
if min_len == 0 {
return Err(ErrZeroLength)
}
let mut addr: *const u8 = ptr::null();
let mut prot = 0;
let mut flags = libc::MAP_PRIVATE;
let mut fd = -1;
let mut offset = 0;
let mut custom_flags = false;
let len = round_up(min_len, page_size());
for &o in options {
match o {
MapReadable => { prot |= libc::PROT_READ; },
MapWritable => { prot |= libc::PROT_WRITE; },
MapExecutable => { prot |= libc::PROT_EXEC; },
MapAddr(addr_) => {
flags |= libc::MAP_FIXED;
addr = addr_;
},
MapFd(fd_) => {
flags |= libc::MAP_FILE;
fd = fd_;
},
MapOffset(offset_) => { offset = offset_ as off_t; },
MapNonStandardFlags(f) => { custom_flags = true; flags = f },
}
}
if fd == -1 && !custom_flags { flags |= libc::MAP_ANON; }
let r = unsafe {
libc::mmap(addr as *mut c_void, len as libc::size_t, prot, flags,
fd, offset)
};
if r == libc::MAP_FAILED {
Err(match errno() {
libc::EACCES => ErrFdNotAvail,
libc::EBADF => ErrInvalidFd,
libc::EINVAL => ErrUnaligned,
libc::ENODEV => ErrNoMapSupport,
libc::ENOMEM => ErrNoMem,
code => ErrUnknown(code as isize)
})
} else {
Ok(MemoryMap {
data: r as *mut u8,
len: len,
kind: if fd == -1 {
MapVirtual
} else {
MapFile(ptr::null())
}
})
}
}
pub fn granularity() -> usize {
page_size()
}
}
#[cfg(unix)]
impl Drop for MemoryMap {
fn drop(&mut self) {
if self.len == 0 { return; }
unsafe {
libc::munmap(self.data as *mut c_void, self.len as libc::size_t);
}
}
}
#[cfg(windows)]
impl MemoryMap {
pub fn new(min_len: usize, options: &[MapOption]) -> Result<MemoryMap, MapError> {
use libc::types::os::arch::extra::{LPVOID, DWORD, SIZE_T, HANDLE};
let mut lpAddress: LPVOID = ptr::null_mut();
let mut readable = false;
let mut writable = false;
let mut executable = false;
let mut handle: HANDLE = libc::INVALID_HANDLE_VALUE;
let mut offset: usize = 0;
let len = round_up(min_len, page_size());
for &o in options {
match o {
MapReadable => { readable = true; },
MapWritable => { writable = true; },
MapExecutable => { executable = true; }
MapAddr(addr_) => { lpAddress = addr_ as LPVOID; },
MapFd(handle_) => { handle = handle_; },
MapOffset(offset_) => { offset = offset_; },
MapNonStandardFlags(..) => {}
}
}
let flProtect = match (executable, readable, writable) {
(false, false, false) if handle == libc::INVALID_HANDLE_VALUE => libc::PAGE_NOACCESS,
(false, true, false) => libc::PAGE_READONLY,
(false, true, true) => libc::PAGE_READWRITE,
(true, false, false) if handle == libc::INVALID_HANDLE_VALUE => libc::PAGE_EXECUTE,
(true, true, false) => libc::PAGE_EXECUTE_READ,
(true, true, true) => libc::PAGE_EXECUTE_READWRITE,
_ => return Err(ErrUnsupProt)
};
if handle == libc::INVALID_HANDLE_VALUE {
if offset != 0 {
return Err(ErrUnsupOffset);
}
let r = unsafe {
libc::VirtualAlloc(lpAddress,
len as SIZE_T,
libc::MEM_COMMIT | libc::MEM_RESERVE,
flProtect)
};
match r as usize {
0 => Err(ErrVirtualAlloc()),
_ => Ok(MemoryMap {
data: r as *mut u8,
len: len,
kind: MapVirtual
})
}
} else {
let dwDesiredAccess = match (executable, readable, writable) {
(false, true, false) => libc::FILE_MAP_READ,
(false, true, true) => libc::FILE_MAP_WRITE,
(true, true, false) => libc::FILE_MAP_READ | libc::FILE_MAP_EXECUTE,
(true, true, true) => libc::FILE_MAP_WRITE | libc::FILE_MAP_EXECUTE,
_ => return Err(ErrUnsupProt) };
unsafe {
let hFile = handle;
let mapping = libc::CreateFileMappingW(hFile,
ptr::null_mut(),
flProtect,
0,
0,
ptr::null());
if mapping == ptr::null_mut() {
return Err(ErrCreateFileMappingW(errno()));
}
if errno() as c_int == libc::ERROR_ALREADY_EXISTS {
return Err(ErrAlreadyExists);
}
let r = libc::MapViewOfFile(mapping,
dwDesiredAccess,
((len as u64) >> 32) as DWORD,
(offset & 0xffff_ffff) as DWORD,
0);
match r as usize {
0 => Err(ErrMapViewOfFile(errno())),
_ => Ok(MemoryMap {
data: r as *mut u8,
len: len,
kind: MapFile(mapping as *const u8)
})
}
}
}
}
pub fn granularity() -> usize {
use mem;
unsafe {
let mut info = mem::zeroed();
libc::GetSystemInfo(&mut info);
return info.dwAllocationGranularity as usize;
}
}
}
#[cfg(windows)]
impl Drop for MemoryMap {
fn drop(&mut self) {
use libc::types::os::arch::extra::{LPCVOID, HANDLE};
use libc::consts::os::extra::FALSE;
if self.len == 0 { return }
unsafe {
match self.kind {
MapVirtual => {
if libc::VirtualFree(self.data as *mut c_void, 0,
libc::MEM_RELEASE) == 0 {
println!("VirtualFree failed: {}", errno());
}
},
MapFile(mapping) => {
if libc::UnmapViewOfFile(self.data as LPCVOID) == FALSE {
println!("UnmapViewOfFile failed: {}", errno());
}
if libc::CloseHandle(mapping as HANDLE) == FALSE {
println!("CloseHandle failed: {}", errno());
}
}
}
}
}
}
impl MemoryMap {
#[inline(always)]
pub fn data(&self) -> *mut u8 { self.data }
#[inline(always)]
pub fn len(&self) -> usize { self.len }
pub fn kind(&self) -> MemoryMapKind { self.kind }
}
#[cfg(test)]
mod tests {
extern crate libc;
extern crate tempdir;
use super::{MemoryMap, MapOption};
#[test]
fn memory_map_rw() {
let chunk = match MemoryMap::new(16, &[
MapOption::MapReadable,
MapOption::MapWritable
]) {
Ok(chunk) => chunk,
Err(msg) => panic!("{:?}", msg)
};
assert!(chunk.len >= 16);
unsafe {
*chunk.data = 0xBE;
assert!(*chunk.data == 0xBE);
}
}
#[test]
fn memory_map_file() {
use std::fs;
use std::io::{Seek, SeekFrom, Write};
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
#[cfg(unix)]
fn get_fd(file: &fs::File) -> libc::c_int {
file.as_raw_fd()
}
#[cfg(windows)]
fn get_fd(file: &fs::File) -> libc::HANDLE {
file.as_raw_handle()
}
let tmpdir = tempdir::TempDir::new("").unwrap();
let mut path = tmpdir.path().to_path_buf();
path.push("mmap_file.tmp");
let size = MemoryMap::granularity() * 2;
let mut file = fs::OpenOptions::new()
.create(true)
.read(true)
.write(true)
.open(&path)
.unwrap();
file.seek(SeekFrom::Start(size as u64)).unwrap();
file.write(b"\0").unwrap();
let fd = get_fd(&file);
let chunk = MemoryMap::new(size / 2, &[
MapOption::MapReadable,
MapOption::MapWritable,
MapOption::MapFd(fd),
MapOption::MapOffset(size / 2)
]).unwrap();
assert!(chunk.len > 0);
unsafe {
*chunk.data = 0xbe;
assert!(*chunk.data == 0xbe);
}
drop(chunk);
fs::remove_file(&path).unwrap();
}
}