Skip to content

Commit e043095

Browse files
committed
Preserve backward-compatibility
Signed-off-by: Michael Jarrett <[email protected]>
1 parent aa4fa1b commit e043095

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

src/mmap.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,19 @@ impl GuestMemoryMmap {
458458
///
459459
/// Valid memory regions are specified as a sequence of (Address, Size, Option<FileOffset>)
460460
/// tuples sorted by Address.
461-
pub fn from_ranges_with_files<A, T>(
462-
ranges: &[(GuestAddress, usize, Option<FileOffset>)],
463-
) -> result::Result<Self, Error> {
464-
Self::from_ranges_with_options(
465-
ranges
466-
.iter()
467-
.map(|r| (r.0, r.1, PageSizePolicy::BasePages, r.2.clone())),
468-
)
461+
pub fn from_ranges_with_files<A, T>(ranges: T) -> result::Result<Self, Error>
462+
where
463+
A: Borrow<(GuestAddress, usize, Option<FileOffset>)>,
464+
T: IntoIterator<Item = A>,
465+
{
466+
Self::from_ranges_with_options(ranges.into_iter().map(|r| {
467+
(
468+
r.borrow().0,
469+
r.borrow().1,
470+
PageSizePolicy::BasePages,
471+
r.borrow().2.clone(),
472+
)
473+
}))
469474
}
470475

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

488493
if let Some(ref f_off) = x.borrow().3 {
489-
MmapRegion::from_file(f_off.clone(), size, policy)
494+
MmapRegion::from_file_with_policy(f_off.clone(), size, policy)
490495
} else {
491-
MmapRegion::new(size, policy)
496+
MmapRegion::with_policy(size, policy)
492497
}
493498
.map_err(Error::MmapRegion)
494499
.and_then(|r| GuestRegionMmap::new(r, guest_base))

src/mmap_unix.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,16 @@ impl MmapRegion {
105105
///
106106
/// # Arguments
107107
/// * `size` - The size of the memory region in bytes.
108-
pub fn new(size: usize, policy: PageSizePolicy) -> Result<Self> {
108+
pub fn new(size: usize) -> Result<Self> {
109+
Self::with_policy(size, PageSizePolicy::BasePages)
110+
}
111+
112+
/// Creates a shared anonymous mapping of `size` bytes.
113+
///
114+
/// # Arguments
115+
/// * `size` - The size of the memory region in bytes.
116+
/// * `policy` - The page size policy of the memory region.
117+
pub fn with_policy(size: usize, policy: PageSizePolicy) -> Result<Self> {
109118
Self::build(
110119
None,
111120
size,
@@ -121,7 +130,22 @@ impl MmapRegion {
121130
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
122131
/// referred to by `file_offset.file`.
123132
/// * `size` - The size of the memory region in bytes.
124-
pub fn from_file(file_offset: FileOffset, size: usize, policy: PageSizePolicy) -> Result<Self> {
133+
pub fn from_file(file_offset: FileOffset, size: usize) -> Result<Self> {
134+
Self::from_file_with_policy(file_offset, size, PageSizePolicy::BasePages)
135+
}
136+
137+
/// Creates a shared file mapping of `size` bytes.
138+
///
139+
/// # Arguments
140+
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
141+
/// referred to by `file_offset.file`.
142+
/// * `size` - The size of the memory region in bytes.
143+
/// * `policy` - The page size policy of the memory region.
144+
pub fn from_file_with_policy(
145+
file_offset: FileOffset,
146+
size: usize,
147+
policy: PageSizePolicy,
148+
) -> Result<Self> {
125149
Self::build(
126150
Some(file_offset),
127151
size,
@@ -412,6 +436,7 @@ mod tests {
412436
size,
413437
prot,
414438
flags,
439+
PageSizePolicy::BasePages,
415440
);
416441
assert_eq!(format!("{:?}", r.unwrap_err()), "InvalidOffsetLength");
417442

@@ -421,6 +446,7 @@ mod tests {
421446
size,
422447
prot,
423448
flags,
449+
PageSizePolicy::BasePages,
424450
);
425451
assert_eq!(format!("{:?}", r.unwrap_err()), "MappingPastEof");
426452

@@ -430,6 +456,7 @@ mod tests {
430456
size,
431457
prot,
432458
flags | libc::MAP_FIXED,
459+
PageSizePolicy::BasePages,
433460
);
434461
assert_eq!(format!("{:?}", r.unwrap_err()), "MapFixed");
435462

@@ -442,6 +469,7 @@ mod tests {
442469
size,
443470
prot,
444471
flags,
472+
PageSizePolicy::BasePages,
445473
);
446474
assert_eq!(r.unwrap_err().raw_os_error(), libc::EINVAL);
447475

@@ -451,6 +479,7 @@ mod tests {
451479
size,
452480
prot,
453481
flags,
482+
PageSizePolicy::BasePages,
454483
)
455484
.unwrap();
456485

src/mmap_windows.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,20 @@ unsafe impl Send for MmapRegion {}
8484
unsafe impl Sync for MmapRegion {}
8585

8686
impl MmapRegion {
87+
/// Creates a shared anonymous mapping of `size` bytes.
88+
///
89+
/// # Arguments
90+
/// * `size` - The size of the memory region in bytes.
91+
pub fn new(size: usize) -> io::Result<Self> {
92+
Self::with_policy(size, PageSizePolicy::BasePages)
93+
}
94+
8795
/// Creates a shared anonymous mapping of `size` bytes.
8896
///
8997
/// # Arguments
9098
/// * `size` - The size of the memory region in bytes.
9199
/// * `policy` - Unimplemented on Windows platforms.
92-
pub fn new(size: usize, _policy: PageSizePolicy) -> io::Result<Self> {
100+
pub fn with_policy(size: usize, _policy: PageSizePolicy) -> io::Result<Self> {
93101
if (size == 0) || (size > MM_HIGHEST_VAD_ADDRESS as usize) {
94102
return Err(io::Error::from_raw_os_error(libc::EINVAL));
95103
}
@@ -106,14 +114,24 @@ impl MmapRegion {
106114
})
107115
}
108116

117+
/// Creates a shared file mapping of `size` bytes.
118+
///
119+
/// # Arguments
120+
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
121+
/// referred to by `file_offset.file`.
122+
/// * `size` - The size of the memory region in bytes.
123+
pub fn from_file(file_offset: FileOffset, size: usize) -> io::Result<Self> {
124+
Self::from_file_with_policy(file_offset, size, PageSizePolicy::BasePages)
125+
}
126+
109127
/// Creates a shared file mapping of `size` bytes.
110128
///
111129
/// # Arguments
112130
/// * `file_offset` - The mapping will be created at offset `file_offset.start` in the file
113131
/// referred to by `file_offset.file`.
114132
/// * `size` - The size of the memory region in bytes.
115133
/// * `policy` - Unimplemented on Windows platforms.
116-
pub fn from_file(
134+
pub fn from_file_with_policy(
117135
file_offset: FileOffset,
118136
size: usize,
119137
_policy: PageSizePolicy,

0 commit comments

Comments
 (0)