Add support for arbitrary flags to MemoryMap.

This also fixes up the documentation a bit, it was subtly incorrect.
This commit is contained in:
Corey Richardson 2013-11-13 05:20:47 -05:00 committed by Alex Crichton
parent de57a22b9a
commit 462f09e949

View File

@ -847,11 +847,11 @@ pub struct MemoryMap {
/// Type of memory map
pub enum MemoryMapKind {
/// Memory-mapped file. On Windows, the inner pointer is a handle to the mapping, and
/// corresponds to `CreateFileMapping`. Elsewhere, it is null.
MapFile(*u8),
/// Virtual memory map. Usually used to change the permissions of a given chunk of memory.
/// Corresponds to `VirtualAlloc` on Windows.
MapFile(*u8),
/// Virtual memory map. Usually used to change the permissions of a given chunk of memory, or
/// for allocation. Corresponds to `VirtualAlloc` on Windows.
MapVirtual
}
@ -868,7 +868,11 @@ pub enum MapOption {
/// Create a memory mapping for a file with a given fd.
MapFd(c_int),
/// When using `MapFd`, the start of the map is `uint` bytes from the start of the file.
MapOffset(uint)
MapOffset(uint),
/// On POSIX, this can be used to specify the default flags passed to `mmap`. By default it uses
/// `MAP_PRIVATE` and, if not using `MapFd`, `MAP_ANON`. This will override both of those. This
/// is platform-specific (the exact values used) and unused on Windows.
MapNonStandardFlags(c_int),
}
/// Possible errors when creating a map.
@ -938,6 +942,7 @@ impl MemoryMap {
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.iter() {
@ -953,10 +958,11 @@ impl MemoryMap {
flags |= libc::MAP_FILE;
fd = fd_;
},
MapOffset(offset_) => { offset = offset_ as off_t; }
MapOffset(offset_) => { offset = offset_ as off_t; },
MapNonStandardFlags(f) => { custom_flags = true; flags = f },
}
}
if fd == -1 { flags |= libc::MAP_ANON; }
if fd == -1 && !custom_flags { flags |= libc::MAP_ANON; }
let r = unsafe {
libc::mmap(addr as *c_void, len as size_t, prot, flags, fd, offset)
@ -1027,7 +1033,9 @@ impl MemoryMap {
MapExecutable => { executable = true; }
MapAddr(addr_) => { lpAddress = addr_ as LPVOID; },
MapFd(fd_) => { fd = fd_; },
MapOffset(offset_) => { offset = offset_; }
MapOffset(offset_) => { offset = offset_; },
MapNonStandardFlags(f) => info!("MemoryMap::new: MapNonStandardFlags used on \
Windows: {}", f),
}
}