Skip to content

Commit 4a49912

Browse files
committed
rollup merge of rust-lang#19620: retep998/memorymap
2 parents a2e9c99 + 58f1274 commit 4a49912

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

src/libstd/os.rs

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub fn getcwd() -> IoResult<Path> {
160160
}
161161

162162
#[cfg(windows)]
163-
pub mod windows {
163+
pub mod windoze {
164164
use libc::types::os::arch::extra::DWORD;
165165
use libc;
166166
use option::Option;
@@ -386,7 +386,7 @@ pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
386386
pub fn getenv(n: &str) -> Option<String> {
387387
unsafe {
388388
with_env_lock(|| {
389-
use os::windows::{fill_utf16_buf_and_decode};
389+
use os::windoze::{fill_utf16_buf_and_decode};
390390
let mut n: Vec<u16> = n.utf16_units().collect();
391391
n.push(0);
392392
fill_utf16_buf_and_decode(|buf, sz| {
@@ -715,7 +715,7 @@ pub fn self_exe_name() -> Option<Path> {
715715
#[cfg(windows)]
716716
fn load_self() -> Option<Vec<u8>> {
717717
unsafe {
718-
use os::windows::fill_utf16_buf_and_decode;
718+
use os::windoze::fill_utf16_buf_and_decode;
719719
fill_utf16_buf_and_decode(|buf, sz| {
720720
libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
721721
}).map(|s| s.into_string().into_bytes())
@@ -1215,7 +1215,11 @@ pub enum MapOption {
12151215
/// Create a map for a specific address range. Corresponds to `MAP_FIXED` on
12161216
/// POSIX.
12171217
MapAddr(*const u8),
1218+
/// Create a memory mapping for a file with a given HANDLE.
1219+
#[cfg(windows)]
1220+
MapFd(libc::HANDLE),
12181221
/// Create a memory mapping for a file with a given fd.
1222+
#[cfg(not(windows))]
12191223
MapFd(c_int),
12201224
/// When using `MapFd`, the start of the map is `uint` bytes from the start
12211225
/// of the file.
@@ -1413,7 +1417,7 @@ impl MemoryMap {
14131417
let mut readable = false;
14141418
let mut writable = false;
14151419
let mut executable = false;
1416-
let mut fd: c_int = -1;
1420+
let mut handle: HANDLE = libc::INVALID_HANDLE_VALUE;
14171421
let mut offset: uint = 0;
14181422
let len = round_up(min_len, page_size());
14191423

@@ -1423,23 +1427,23 @@ impl MemoryMap {
14231427
MapWritable => { writable = true; },
14241428
MapExecutable => { executable = true; }
14251429
MapAddr(addr_) => { lpAddress = addr_ as LPVOID; },
1426-
MapFd(fd_) => { fd = fd_; },
1430+
MapFd(handle_) => { handle = handle_; },
14271431
MapOffset(offset_) => { offset = offset_; },
14281432
MapNonStandardFlags(..) => {}
14291433
}
14301434
}
14311435

14321436
let flProtect = match (executable, readable, writable) {
1433-
(false, false, false) if fd == -1 => libc::PAGE_NOACCESS,
1437+
(false, false, false) if handle == libc::INVALID_HANDLE_VALUE => libc::PAGE_NOACCESS,
14341438
(false, true, false) => libc::PAGE_READONLY,
14351439
(false, true, true) => libc::PAGE_READWRITE,
1436-
(true, false, false) if fd == -1 => libc::PAGE_EXECUTE,
1440+
(true, false, false) if handle == libc::INVALID_HANDLE_VALUE => libc::PAGE_EXECUTE,
14371441
(true, true, false) => libc::PAGE_EXECUTE_READ,
14381442
(true, true, true) => libc::PAGE_EXECUTE_READWRITE,
14391443
_ => return Err(ErrUnsupProt)
14401444
};
14411445

1442-
if fd == -1 {
1446+
if handle == libc::INVALID_HANDLE_VALUE {
14431447
if offset != 0 {
14441448
return Err(ErrUnsupOffset);
14451449
}
@@ -1467,7 +1471,7 @@ impl MemoryMap {
14671471
// we should never get here.
14681472
};
14691473
unsafe {
1470-
let hFile = libc::get_osfhandle(fd) as HANDLE;
1474+
let hFile = handle;
14711475
let mapping = libc::CreateFileMappingW(hFile,
14721476
ptr::null_mut(),
14731477
flProtect,
@@ -1991,55 +1995,47 @@ mod tests {
19911995

19921996
#[test]
19931997
fn memory_map_file() {
1994-
use result::Result::{Ok, Err};
19951998
use os::*;
1996-
use libc::*;
1997-
use io::fs;
1998-
1999-
#[cfg(unix)]
2000-
fn lseek_(fd: c_int, size: uint) {
2001-
unsafe {
2002-
assert!(lseek(fd, size as off_t, SEEK_SET) == size as off_t);
2003-
}
1999+
use io::fs::{File, unlink};
2000+
use io::SeekStyle::SeekSet;
2001+
use io::FileMode::Open;
2002+
use io::FileAccess::ReadWrite;
2003+
use libc::HANDLE;
2004+
2005+
#[cfg(not(windows))]
2006+
fn get_fd(file: &File) -> c_int {
2007+
use os::unix::AsRawFd;
2008+
file.as_raw_fd()
20042009
}
2010+
20052011
#[cfg(windows)]
2006-
fn lseek_(fd: c_int, size: uint) {
2007-
unsafe {
2008-
assert!(lseek(fd, size as c_long, SEEK_SET) == size as c_long);
2009-
}
2012+
fn get_fd(file: &File) -> HANDLE {
2013+
use os::windows::AsRawHandle;
2014+
file.as_raw_handle()
20102015
}
20112016

20122017
let mut path = tmpdir();
20132018
path.push("mmap_file.tmp");
20142019
let size = MemoryMap::granularity() * 2;
2020+
let mut file = File::open_mode(&path, Open, ReadWrite).unwrap();
2021+
file.seek(size as i64, SeekSet);
2022+
file.write_u8(0);
20152023

2016-
let fd = unsafe {
2017-
let fd = path.with_c_str(|path| {
2018-
open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)
2019-
});
2020-
lseek_(fd, size);
2021-
"x".with_c_str(|x| assert!(write(fd, x as *const c_void, 1) == 1));
2022-
fd
2023-
};
2024-
let chunk = match MemoryMap::new(size / 2, &[
2024+
let chunk = MemoryMap::new(size / 2, &[
20252025
MapReadable,
20262026
MapWritable,
2027-
MapFd(fd),
2027+
MapFd(get_fd(&file)),
20282028
MapOffset(size / 2)
2029-
]) {
2030-
Ok(chunk) => chunk,
2031-
Err(msg) => panic!("{}", msg)
2032-
};
2029+
]).unwrap();
20332030
assert!(chunk.len > 0);
20342031

20352032
unsafe {
20362033
*chunk.data = 0xbe;
20372034
assert!(*chunk.data == 0xbe);
2038-
close(fd);
20392035
}
20402036
drop(chunk);
20412037

2042-
fs::unlink(&path).unwrap();
2038+
unlink(&path).unwrap();
20432039
}
20442040

20452041
#[test]

src/libstd/sys/windows/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use libc::{mod, c_int};
1515

1616
use c_str::CString;
1717
use mem;
18-
use os::windows::fill_utf16_buf_and_decode;
18+
use os::windoze::fill_utf16_buf_and_decode;
1919
use path;
2020
use ptr;
2121
use str;

0 commit comments

Comments
 (0)