about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorCorey Richardson <corey@octayn.net>2013-11-13 05:20:47 -0500
committerAlex Crichton <alex@alexcrichton.com>2014-01-24 22:30:00 -0800
commit462f09e9494481456b22630cb42a3c0544a08625 (patch)
treebdec144db010ba71ef7381ea1a87ac3a0a82660f /src/libstd
parentde57a22b9a8c8416cace31c9bd3ec4c9a6888017 (diff)
downloadrust-462f09e9494481456b22630cb42a3c0544a08625.tar.gz
rust-462f09e9494481456b22630cb42a3c0544a08625.zip
Add support for arbitrary flags to MemoryMap.
This also fixes up the documentation a bit, it was subtly incorrect.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/os.rs22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/libstd/os.rs b/src/libstd/os.rs
index 1b55427fc2d..cdf0c3b6442 100644
--- a/src/libstd/os.rs
+++ b/src/libstd/os.rs
@@ -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),
             }
         }