From 462f09e9494481456b22630cb42a3c0544a08625 Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Wed, 13 Nov 2013 05:20:47 -0500 Subject: [PATCH] Add support for arbitrary flags to MemoryMap. This also fixes up the documentation a bit, it was subtly incorrect. --- src/libstd/os.rs | 22 +++++++++++++++------- 1 file 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), } }