Skip to content

Commit 462f09e

Browse files
emberianalexcrichton
authored andcommitted
Add support for arbitrary flags to MemoryMap.
This also fixes up the documentation a bit, it was subtly incorrect.
1 parent de57a22 commit 462f09e

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/libstd/os.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,11 @@ pub struct MemoryMap {
847847

848848
/// Type of memory map
849849
pub enum MemoryMapKind {
850-
/// Memory-mapped file. On Windows, the inner pointer is a handle to the mapping, and
851-
/// corresponds to `CreateFileMapping`. Elsewhere, it is null.
852-
MapFile(*u8),
853850
/// Virtual memory map. Usually used to change the permissions of a given chunk of memory.
854851
/// Corresponds to `VirtualAlloc` on Windows.
852+
MapFile(*u8),
853+
/// Virtual memory map. Usually used to change the permissions of a given chunk of memory, or
854+
/// for allocation. Corresponds to `VirtualAlloc` on Windows.
855855
MapVirtual
856856
}
857857

@@ -868,7 +868,11 @@ pub enum MapOption {
868868
/// Create a memory mapping for a file with a given fd.
869869
MapFd(c_int),
870870
/// When using `MapFd`, the start of the map is `uint` bytes from the start of the file.
871-
MapOffset(uint)
871+
MapOffset(uint),
872+
/// On POSIX, this can be used to specify the default flags passed to `mmap`. By default it uses
873+
/// `MAP_PRIVATE` and, if not using `MapFd`, `MAP_ANON`. This will override both of those. This
874+
/// is platform-specific (the exact values used) and unused on Windows.
875+
MapNonStandardFlags(c_int),
872876
}
873877

874878
/// Possible errors when creating a map.
@@ -938,6 +942,7 @@ impl MemoryMap {
938942
let mut flags = libc::MAP_PRIVATE;
939943
let mut fd = -1;
940944
let mut offset = 0;
945+
let mut custom_flags = false;
941946
let len = round_up(min_len, page_size());
942947

943948
for &o in options.iter() {
@@ -953,10 +958,11 @@ impl MemoryMap {
953958
flags |= libc::MAP_FILE;
954959
fd = fd_;
955960
},
956-
MapOffset(offset_) => { offset = offset_ as off_t; }
961+
MapOffset(offset_) => { offset = offset_ as off_t; },
962+
MapNonStandardFlags(f) => { custom_flags = true; flags = f },
957963
}
958964
}
959-
if fd == -1 { flags |= libc::MAP_ANON; }
965+
if fd == -1 && !custom_flags { flags |= libc::MAP_ANON; }
960966

961967
let r = unsafe {
962968
libc::mmap(addr as *c_void, len as size_t, prot, flags, fd, offset)
@@ -1027,7 +1033,9 @@ impl MemoryMap {
10271033
MapExecutable => { executable = true; }
10281034
MapAddr(addr_) => { lpAddress = addr_ as LPVOID; },
10291035
MapFd(fd_) => { fd = fd_; },
1030-
MapOffset(offset_) => { offset = offset_; }
1036+
MapOffset(offset_) => { offset = offset_; },
1037+
MapNonStandardFlags(f) => info!("MemoryMap::new: MapNonStandardFlags used on \
1038+
Windows: {}", f),
10311039
}
10321040
}
10331041

0 commit comments

Comments
 (0)