Skip to content

Commit

Permalink
Preserve backward-compatibility
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jarrett <[email protected]>
  • Loading branch information
EmeraldShift committed Dec 12, 2020
1 parent aa4fa1b commit e043095
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
25 changes: 15 additions & 10 deletions src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,19 @@ impl GuestMemoryMmap {
///
/// Valid memory regions are specified as a sequence of (Address, Size, Option<FileOffset>)
/// tuples sorted by Address.
pub fn from_ranges_with_files<A, T>(
ranges: &[(GuestAddress, usize, Option<FileOffset>)],
) -> result::Result<Self, Error> {
Self::from_ranges_with_options(
ranges
.iter()
.map(|r| (r.0, r.1, PageSizePolicy::BasePages, r.2.clone())),
)
pub fn from_ranges_with_files<A, T>(ranges: T) -> result::Result<Self, Error>
where
A: Borrow<(GuestAddress, usize, Option<FileOffset>)>,
T: IntoIterator<Item = A>,
{
Self::from_ranges_with_options(ranges.into_iter().map(|r| {
(
r.borrow().0,
r.borrow().1,
PageSizePolicy::BasePages,
r.borrow().2.clone(),
)
}))
}

/// Creates a container and allocates anonymous memory for guest memory regions.
Expand All @@ -486,9 +491,9 @@ impl GuestMemoryMmap {
let policy = x.borrow().2;

if let Some(ref f_off) = x.borrow().3 {
MmapRegion::from_file(f_off.clone(), size, policy)
MmapRegion::from_file_with_policy(f_off.clone(), size, policy)
} else {
MmapRegion::new(size, policy)
MmapRegion::with_policy(size, policy)
}
.map_err(Error::MmapRegion)
.and_then(|r| GuestRegionMmap::new(r, guest_base))
Expand Down
33 changes: 31 additions & 2 deletions src/mmap_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,16 @@ impl MmapRegion {
///
/// # Arguments
/// * `size` - The size of the memory region in bytes.
pub fn new(size: usize, policy: PageSizePolicy) -> Result<Self> {
pub fn new(size: usize) -> Result<Self> {
Self::with_policy(size, PageSizePolicy::BasePages)
}

/// Creates a shared anonymous mapping of `size` bytes.
///
/// # Arguments
/// * `size` - The size of the memory region in bytes.
/// * `policy` - The page size policy of the memory region.
pub fn with_policy(size: usize, policy: PageSizePolicy) -> Result<Self> {
Self::build(
None,
size,
Expand All @@ -121,7 +130,22 @@ impl MmapRegion {
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
/// referred to by `file_offset.file`.
/// * `size` - The size of the memory region in bytes.
pub fn from_file(file_offset: FileOffset, size: usize, policy: PageSizePolicy) -> Result<Self> {
pub fn from_file(file_offset: FileOffset, size: usize) -> Result<Self> {
Self::from_file_with_policy(file_offset, size, PageSizePolicy::BasePages)
}

/// Creates a shared file mapping of `size` bytes.
///
/// # Arguments
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
/// referred to by `file_offset.file`.
/// * `size` - The size of the memory region in bytes.
/// * `policy` - The page size policy of the memory region.
pub fn from_file_with_policy(
file_offset: FileOffset,
size: usize,
policy: PageSizePolicy,
) -> Result<Self> {
Self::build(
Some(file_offset),
size,
Expand Down Expand Up @@ -412,6 +436,7 @@ mod tests {
size,
prot,
flags,
PageSizePolicy::BasePages,
);
assert_eq!(format!("{:?}", r.unwrap_err()), "InvalidOffsetLength");

Expand All @@ -421,6 +446,7 @@ mod tests {
size,
prot,
flags,
PageSizePolicy::BasePages,
);
assert_eq!(format!("{:?}", r.unwrap_err()), "MappingPastEof");

Expand All @@ -430,6 +456,7 @@ mod tests {
size,
prot,
flags | libc::MAP_FIXED,
PageSizePolicy::BasePages,
);
assert_eq!(format!("{:?}", r.unwrap_err()), "MapFixed");

Expand All @@ -442,6 +469,7 @@ mod tests {
size,
prot,
flags,
PageSizePolicy::BasePages,
);
assert_eq!(r.unwrap_err().raw_os_error(), libc::EINVAL);

Expand All @@ -451,6 +479,7 @@ mod tests {
size,
prot,
flags,
PageSizePolicy::BasePages,
)
.unwrap();

Expand Down
22 changes: 20 additions & 2 deletions src/mmap_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,20 @@ unsafe impl Send for MmapRegion {}
unsafe impl Sync for MmapRegion {}

impl MmapRegion {
/// Creates a shared anonymous mapping of `size` bytes.
///
/// # Arguments
/// * `size` - The size of the memory region in bytes.
pub fn new(size: usize) -> io::Result<Self> {
Self::with_policy(size, PageSizePolicy::BasePages)
}

/// Creates a shared anonymous mapping of `size` bytes.
///
/// # Arguments
/// * `size` - The size of the memory region in bytes.
/// * `policy` - Unimplemented on Windows platforms.
pub fn new(size: usize, _policy: PageSizePolicy) -> io::Result<Self> {
pub fn with_policy(size: usize, _policy: PageSizePolicy) -> io::Result<Self> {
if (size == 0) || (size > MM_HIGHEST_VAD_ADDRESS as usize) {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
Expand All @@ -106,14 +114,24 @@ impl MmapRegion {
})
}

/// Creates a shared file mapping of `size` bytes.
///
/// # Arguments
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
/// referred to by `file_offset.file`.
/// * `size` - The size of the memory region in bytes.
pub fn from_file(file_offset: FileOffset, size: usize) -> io::Result<Self> {
Self::from_file_with_policy(file_offset, size, PageSizePolicy::BasePages)
}

/// Creates a shared file mapping of `size` bytes.
///
/// # Arguments
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
/// referred to by `file_offset.file`.
/// * `size` - The size of the memory region in bytes.
/// * `policy` - Unimplemented on Windows platforms.
pub fn from_file(
pub fn from_file_with_policy(
file_offset: FileOffset,
size: usize,
_policy: PageSizePolicy,
Expand Down

0 comments on commit e043095

Please sign in to comment.