Skip to content

Commit 785f9b8

Browse files
committed
auto merge of #10358 : cmr/rust/mmap++, r=alexcrichton
2 parents d14a647 + 658e20e commit 785f9b8

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

src/libstd/os.rs

+49-3
Original file line numberDiff line numberDiff line change
@@ -913,41 +913,81 @@ pub fn page_size() -> uint {
913913
}
914914
}
915915

916+
/// A memory mapped file or chunk of memory. This is a very system-specific interface to the OS's
917+
/// memory mapping facilities (`mmap` on POSIX, `VirtualAlloc`/`CreateFileMapping` on win32). It
918+
/// makes no attempt at abstracting platform differences, besides in error values returned. Consider
919+
/// yourself warned.
920+
///
921+
/// The memory map is released (unmapped) when the destructor is run, so don't let it leave scope by
922+
/// accident if you want it to stick around.
916923
pub struct MemoryMap {
924+
/// Pointer to the memory created or modified by this map.
917925
data: *mut u8,
926+
/// Number of bytes this map applies to
918927
len: size_t,
928+
/// Type of mapping
919929
kind: MemoryMapKind
920930
}
921931

932+
/// Type of memory map
922933
pub enum MemoryMapKind {
934+
/// Memory-mapped file. On Windows, the inner pointer is a handle to the mapping, and
935+
/// corresponds to `CreateFileMapping`. Elsewhere, it is null.
923936
MapFile(*c_void),
937+
/// Virtual memory map. Usually used to change the permissions of a given chunk of memory.
938+
/// Corresponds to `VirtualAlloc` on Windows.
924939
MapVirtual
925940
}
926941

942+
/// Options the memory map is created with
927943
pub enum MapOption {
944+
/// The memory should be readable
928945
MapReadable,
946+
/// The memory should be writable
929947
MapWritable,
948+
/// The memory should be executable
930949
MapExecutable,
950+
/// Create a map for a specific address range. Corresponds to `MAP_FIXED` on POSIX.
931951
MapAddr(*c_void),
952+
/// Create a memory mapping for a file with a given fd.
932953
MapFd(c_int),
954+
/// When using `MapFd`, the start of the map is `uint` bytes from the start of the file.
933955
MapOffset(uint)
934956
}
935957

958+
/// Possible errors when creating a map.
936959
pub enum MapError {
937-
// Linux-specific errors
960+
/// ## The following are POSIX-specific
961+
///
962+
/// fd was not open for reading or, if using `MapWritable`, was not open for writing.
938963
ErrFdNotAvail,
964+
/// fd was not valid
939965
ErrInvalidFd,
966+
/// Either the address given by `MapAddr` or offset given by `MapOffset` was not a multiple of
967+
/// `MemoryMap::granularity` (unaligned to page size).
940968
ErrUnaligned,
969+
/// With `MapFd`, the fd does not support mapping.
941970
ErrNoMapSupport,
971+
/// If using `MapAddr`, the address + `min_len` was outside of the process's address space. If
972+
/// using `MapFd`, the target of the fd didn't have enough resources to fulfill the request.
942973
ErrNoMem,
974+
/// Unrecognized error. The inner value is the unrecognized errno.
943975
ErrUnknown(libc::c_int),
944-
945-
// Windows-specific errors
976+
/// ## The following are win32-specific
977+
///
978+
/// Unsupported combination of protection flags (`MapReadable`/`MapWritable`/`MapExecutable`).
946979
ErrUnsupProt,
980+
/// When using `MapFd`, `MapOffset` was given (Windows does not support this at all)
947981
ErrUnsupOffset,
982+
/// When using `MapFd`, there was already a mapping to the file.
948983
ErrAlreadyExists,
984+
/// Unrecognized error from `VirtualAlloc`. The inner value is the return value of GetLastError.
949985
ErrVirtualAlloc(uint),
986+
/// Unrecognized error from `CreateFileMapping`. The inner value is the return value of
987+
/// `GetLastError`.
950988
ErrCreateFileMappingW(uint),
989+
/// Unrecognized error from `MapViewOfFile`. The inner value is the return value of
990+
/// `GetLastError`.
951991
ErrMapViewOfFile(uint)
952992
}
953993

@@ -973,6 +1013,7 @@ impl to_str::ToStr for MapError {
9731013

9741014
#[cfg(unix)]
9751015
impl MemoryMap {
1016+
/// Create a new mapping with the given `options`, at least `min_len` bytes long.
9761017
pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
9771018
#[fixed_stack_segment]; #[inline(never)];
9781019

@@ -1028,13 +1069,15 @@ impl MemoryMap {
10281069
}
10291070
}
10301071

1072+
/// Granularity that the offset or address must be for `MapOffset` and `MapAddr` respectively.
10311073
pub fn granularity() -> uint {
10321074
page_size()
10331075
}
10341076
}
10351077

10361078
#[cfg(unix)]
10371079
impl Drop for MemoryMap {
1080+
/// Unmap the mapping. Fails the task if `munmap` fails.
10381081
fn drop(&mut self) {
10391082
#[fixed_stack_segment]; #[inline(never)];
10401083

@@ -1053,6 +1096,7 @@ impl Drop for MemoryMap {
10531096

10541097
#[cfg(windows)]
10551098
impl MemoryMap {
1099+
/// Create a new mapping with the given `options`, at least `min_len` bytes long.
10561100
pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
10571101
#[fixed_stack_segment]; #[inline(never)];
10581102

@@ -1161,6 +1205,8 @@ impl MemoryMap {
11611205

11621206
#[cfg(windows)]
11631207
impl Drop for MemoryMap {
1208+
/// Unmap the mapping. Fails the task if any of `VirtualFree`, `UnmapViewOfFile`, or
1209+
/// `CloseHandle` fail.
11641210
fn drop(&mut self) {
11651211
#[fixed_stack_segment]; #[inline(never)];
11661212

0 commit comments

Comments
 (0)