Skip to content

Commit 448291d

Browse files
Serban Iorgaalxiord
authored andcommitted
[mmap_windows.rs] documentation improvements and fixes
Signed-off-by: Serban Iorga <seriorga@amazon.com>
1 parent ee29b88 commit 448291d

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

src/mmap_windows.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5-
//! A default Windows implementation of the GuestMemory trait using VirtualAlloc() and MapViewOfFile().
6-
//!
7-
//! The main structs to access guest's memory are:
8-
//! - [MmapRegion](struct.MmapRegion.html): mmap a continuous region of guest's memory into the
9-
//! current process
10-
//! - [GuestRegionMmap](struct.GuestRegionMmap.html): tracks a mapping of memory in the current
11-
//! process and the corresponding base address. It relays guest memory access requests to the
12-
//! underline [MmapRegion](struct.MmapRegion.html) object.
13-
//! - [GuestMemoryMmap](struct.GuestMemoryMmap.html): provides methods to access a collection of
14-
//! GuestRegionMmap objects.
5+
//! Helper structure for working with mmaped memory regions in Windows.
156
167
use std;
178
use std::io;
@@ -70,11 +61,15 @@ pub const INVALID_HANDLE_VALUE: RawHandle = (-1isize) as RawHandle;
7061
#[allow(dead_code)]
7162
pub const ERROR_INVALID_PARAMETER: i32 = 87;
7263

73-
/// A backend driver to access guest's physical memory by mmapping guest's memory into the current
74-
/// process.
75-
/// For a combination of 32-bit hypervisor and 64-bit virtual machine, only partial of guest's
76-
/// physical memory may be mapped into current process due to limited process virtual address
77-
/// space size.
64+
/// Helper structure for working with mmaped memory regions in Unix.
65+
///
66+
/// The structure is used for accessing the guest's physical memory by mmapping it into
67+
/// the current process.
68+
///
69+
/// # Limitations
70+
/// When running a 64-bit virtual machine on a 32-bit hypervisor, only part of the guest's
71+
/// physical memory may be mapped into the current process due to the limited virtual address
72+
/// space size of the process.
7873
#[derive(Debug)]
7974
pub struct MmapRegion {
8075
addr: *mut u8,
@@ -90,10 +85,10 @@ unsafe impl Send for MmapRegion {}
9085
unsafe impl Sync for MmapRegion {}
9186

9287
impl MmapRegion {
93-
/// Creates an anonymous shared mapping of `size` bytes.
88+
/// Creates a shared anonymous mapping of `size` bytes.
9489
///
9590
/// # Arguments
96-
/// * `size` - Size of memory region in bytes.
91+
/// * `size` - The size of the memory region in bytes.
9792
pub fn new(size: usize) -> io::Result<Self> {
9893
if (size == 0) || (size > MM_HIGHEST_VAD_ADDRESS as usize) {
9994
return Err(io::Error::from_raw_os_error(libc::EINVAL));
@@ -111,12 +106,12 @@ impl MmapRegion {
111106
})
112107
}
113108

114-
/// Maps the `size` bytes starting at `offset` bytes of the given `fd`.
109+
/// Creates a shared file mapping of `size` bytes.
115110
///
116111
/// # Arguments
117-
/// * `file` - Raw handle to a file to map into the address space.
118-
/// * `size` - Size of memory region in bytes.
119-
/// * `offset` - Offset in bytes from the beginning of `file` to start the mapping.
112+
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
113+
/// referred to by `file_offset.file`.
114+
/// * `size` - The size of the memory region in bytes.
120115
pub fn from_file(file_offset: FileOffset, size: usize) -> io::Result<Self> {
121116
let handle = file_offset.file().as_raw_handle();
122117
if handle == INVALID_HANDLE_VALUE {
@@ -165,8 +160,9 @@ impl MmapRegion {
165160
})
166161
}
167162

168-
/// Returns a pointer to the beginning of the memory region. Should only be
169-
/// used for passing this region to ioctls for setting guest memory.
163+
/// Returns a pointer to the beginning of the memory region.
164+
///
165+
/// Should only be used for passing this region to ioctls for setting guest memory.
170166
pub fn as_ptr(&self) -> *mut u8 {
171167
self.addr
172168
}
@@ -183,15 +179,12 @@ impl MmapRegion {
183179
}
184180

185181
impl AsSlice for MmapRegion {
186-
// Returns the region as a slice
187-
// used to do crap
188182
unsafe fn as_slice(&self) -> &[u8] {
189183
// This is safe because we mapped the area at addr ourselves, so this slice will not
190184
// overflow. However, it is possible to alias.
191185
std::slice::from_raw_parts(self.addr, self.size)
192186
}
193187

194-
// safe because it's expected interior mutability
195188
#[allow(clippy::mut_from_ref)]
196189
unsafe fn as_mut_slice(&self) -> &mut [u8] {
197190
// This is safe because we mapped the area at addr ourselves, so this slice will not

0 commit comments

Comments
 (0)