Skip to content

std: os: Document MemoryMap #10358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,41 +913,81 @@ pub fn page_size() -> uint {
}
}

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

/// 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(*c_void),
/// Virtual memory map. Usually used to change the permissions of a given chunk of memory.
/// Corresponds to `VirtualAlloc` on Windows.
MapVirtual
}

/// Options the memory map is created with
pub enum MapOption {
/// The memory should be readable
MapReadable,
/// The memory should be writable
MapWritable,
/// The memory should be executable
MapExecutable,
/// Create a map for a specific address range. Corresponds to `MAP_FIXED` on POSIX.
MapAddr(*c_void),
/// 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)
}

/// Possible errors when creating a map.
pub enum MapError {
// Linux-specific errors
/// ## The following are POSIX-specific
///
/// fd was not open for reading or, if using `MapWritable`, was not open for writing.
ErrFdNotAvail,
/// fd was not valid
ErrInvalidFd,
/// Either the address given by `MapAddr` or offset given by `MapOffset` was not a multiple of
/// `MemoryMap::granularity` (unaligned to page size).
ErrUnaligned,
/// With `MapFd`, the fd does not support mapping.
ErrNoMapSupport,
/// If using `MapAddr`, the address + `min_len` was outside of the process's address space. If
/// using `MapFd`, the target of the fd didn't have enough resources to fulfill the request.
ErrNoMem,
/// Unrecognized error. The inner value is the unrecognized errno.
ErrUnknown(libc::c_int),

// Windows-specific errors
/// ## The following are win32-specific
///
/// Unsupported combination of protection flags (`MapReadable`/`MapWritable`/`MapExecutable`).
ErrUnsupProt,
/// When using `MapFd`, `MapOffset` was given (Windows does not support this at all)
ErrUnsupOffset,
/// When using `MapFd`, there was already a mapping to the file.
ErrAlreadyExists,
/// Unrecognized error from `VirtualAlloc`. The inner value is the return value of GetLastError.
ErrVirtualAlloc(uint),
/// Unrecognized error from `CreateFileMapping`. The inner value is the return value of
/// `GetLastError`.
ErrCreateFileMappingW(uint),
/// Unrecognized error from `MapViewOfFile`. The inner value is the return value of
/// `GetLastError`.
ErrMapViewOfFile(uint)
}

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

#[cfg(unix)]
impl MemoryMap {
/// Create a new mapping with the given `options`, at least `min_len` bytes long.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add to this comment saying that the region is unmapped when the MemoryMap object goes out of scope?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

On Sat, Nov 9, 2013 at 12:36 AM, Alex Crichton notifications@github.comwrote:

In src/libstd/os.rs:

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

#[cfg(unix)]
impl MemoryMap {

  • /// Create a new mapping with the given options, at least min_len bytes long.

Could you add to this comment saying that the region is unmapped when the
MemoryMap object goes out of scope?


Reply to this email directly or view it on GitHubhttps://github.com//pull/10358/files#r7544673
.

pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
#[fixed_stack_segment]; #[inline(never)];

Expand Down Expand Up @@ -1028,13 +1069,15 @@ impl MemoryMap {
}
}

/// Granularity that the offset or address must be for `MapOffset` and `MapAddr` respectively.
pub fn granularity() -> uint {
page_size()
}
}

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

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

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

Expand Down Expand Up @@ -1161,6 +1205,8 @@ impl MemoryMap {

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

Expand Down