Skip to content

Commit 5ded635

Browse files
committed
refactor: scatter mmap.rs contents to unix/windows modules
This is some duplication, but it once and for all eliminates all cfg fuckery and code that expects precisely one module to be defined. Admittedly, for windows/unix we could have stuck with the cfg stuff, but I wanted a clean slate for now. The windows parts are compile-tested only, as I do not have a windows system available. The handling of various from_range functions is based on the assumption that these were only used in test code and nowhere else (hence all the preceding refactors of test code). In unit tests, their use has been eliminated, and doc tests now got a cfg(target_family="unix", not(feature="xen")) so that they only run when mmap_unix is available. This is fine, because they are examples of how to use specific functions, and not any serious tests (e.g. dont need to run for all supported backends). Signed-off-by: Patrick Roy <[email protected]>
1 parent 217b29e commit 5ded635

File tree

11 files changed

+308
-230
lines changed

11 files changed

+308
-230
lines changed

benches/guest_memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (C) 2020 Alibaba Cloud Computing. All rights reserved.
22
//
33
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
4-
#![cfg(feature = "backend-mmap")]
4+
#![cfg(all(feature = "backend-mmap", not(feature = "xen")))]
55

66
use core::hint::black_box;
77
pub use criterion::Criterion;

benches/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
extern crate criterion;
66

77
pub use criterion::{criterion_group, criterion_main, Criterion};
8-
#[cfg(feature = "backend-mmap")]
8+
#[cfg(all(feature = "backend-mmap", not(feature = "xen")))]
99
use vm_memory::{GuestAddress, GuestMemoryMmap};
1010

1111
mod guest_memory;
@@ -14,7 +14,7 @@ mod volatile;
1414

1515
use volatile::benchmark_for_volatile;
1616

17-
#[cfg(feature = "backend-mmap")]
17+
#[cfg(all(feature = "backend-mmap", not(feature = "xen")))]
1818
// Use this function with caution. It does not check against overflows
1919
// and `GuestMemoryMmap::from_ranges` errors.
2020
fn create_guest_memory_mmap(size: usize, count: u64) -> GuestMemoryMmap<()> {
@@ -27,12 +27,12 @@ fn create_guest_memory_mmap(size: usize, count: u64) -> GuestMemoryMmap<()> {
2727
}
2828

2929
pub fn criterion_benchmark(_c: &mut Criterion) {
30-
#[cfg(feature = "backend-mmap")]
30+
#[cfg(all(feature = "backend-mmap", not(feature = "xen")))]
3131
mmap::benchmark_for_mmap(_c);
3232
}
3333

3434
pub fn benchmark_guest_memory(_c: &mut Criterion) {
35-
#[cfg(feature = "backend-mmap")]
35+
#[cfg(all(feature = "backend-mmap", not(feature = "xen")))]
3636
guest_memory::benchmark_for_guest_memory(_c)
3737
}
3838

benches/mmap/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
//
33
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
4-
#![cfg(feature = "backend-mmap")]
4+
#![cfg(all(feature = "backend-mmap", not(feature = "xen")))]
55
#![allow(clippy::undocumented_unsafe_blocks)]
66

77
extern crate criterion;

src/bitmap/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub(crate) mod tests {
125125
use std::sync::atomic::Ordering;
126126

127127
use crate::{Bytes, VolatileMemory};
128-
#[cfg(feature = "backend-mmap")]
128+
#[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
129129
use crate::{GuestAddress, MemoryRegionAddress};
130130

131131
// Helper method to check whether a specified range is clean.
@@ -219,7 +219,7 @@ pub(crate) mod tests {
219219
// them to test the mmap-based backend implementations for now. Going forward, the generic
220220
// test functions defined here can be placed in a separate module (i.e. `test_utilities`)
221221
// which is gated by a feature and can be used for testing purposes by other crates as well.
222-
#[cfg(feature = "backend-mmap")]
222+
#[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
223223
fn test_guest_memory_region<R: GuestMemoryRegion>(region: &R) {
224224
let dirty_addr = MemoryRegionAddress(0x0);
225225
let val = 123u64;
@@ -253,7 +253,7 @@ pub(crate) mod tests {
253253
);
254254
}
255255

256-
#[cfg(feature = "backend-mmap")]
256+
#[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
257257
// Assumptions about M generated by f ...
258258
pub fn test_guest_memory_and_region<M, F>(f: F)
259259
where

src/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ pub trait Bytes<A> {
331331
/// * Read bytes from /dev/urandom (uses the `backend-mmap` feature)
332332
///
333333
/// ```
334-
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd"))]
334+
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd", target_family = "unix", not(feature = "xen")))]
335335
/// # {
336336
/// # use vm_memory::{Address, GuestMemory, Bytes, GuestAddress, GuestMemoryMmap};
337337
/// # use std::fs::File;

src/guest_memory.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl FileOffset {
176176
/// # Examples (uses the `backend-mmap` and `backend-atomic` features)
177177
///
178178
/// ```
179-
/// # #[cfg(feature = "backend-mmap")]
179+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
180180
/// # {
181181
/// # use std::sync::Arc;
182182
/// # use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryMmap};
@@ -294,7 +294,7 @@ pub trait GuestMemory {
294294
/// `backend-mmap` feature)
295295
///
296296
/// ```
297-
/// # #[cfg(feature = "backend-mmap")]
297+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
298298
/// # {
299299
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryRegion, GuestMemoryMmap};
300300
/// #
@@ -318,7 +318,7 @@ pub trait GuestMemory {
318318
/// # Examples (uses the `backend-mmap` feature)
319319
///
320320
/// ```
321-
/// # #[cfg(feature = "backend-mmap")]
321+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
322322
/// # {
323323
/// # use vm_memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap};
324324
/// #
@@ -433,7 +433,7 @@ pub trait GuestMemory {
433433
/// # Examples (uses the `backend-mmap` feature)
434434
///
435435
/// ```
436-
/// # #[cfg(feature = "backend-mmap")]
436+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
437437
/// # {
438438
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
439439
/// #
@@ -592,7 +592,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
592592
/// * Write a slice at guestaddress 0x1000. (uses the `backend-mmap` feature)
593593
///
594594
/// ```
595-
/// # #[cfg(feature = "backend-mmap")]
595+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
596596
/// # {
597597
/// # use vm_memory::{Bytes, GuestAddress, mmap::GuestMemoryMmap};
598598
/// #
@@ -620,7 +620,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
620620
/// * Read a slice of length 16 at guestaddress 0x1000. (uses the `backend-mmap` feature)
621621
///
622622
/// ```
623-
/// # #[cfg(feature = "backend-mmap")]
623+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
624624
/// # {
625625
/// # use vm_memory::{Bytes, GuestAddress, mmap::GuestMemoryMmap};
626626
/// #

src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,22 @@ pub use io::{ReadVolatile, WriteVolatile};
6767
#[cfg(feature = "backend-mmap")]
6868
pub mod mmap;
6969

70-
#[cfg(feature = "backend-mmap")]
71-
pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapRegion};
7270
#[cfg(all(feature = "backend-mmap", feature = "xen", target_family = "unix"))]
7371
pub use mmap::{MmapRange, MmapXenFlags};
7472

75-
#[cfg(all(feature = "xen", unix))]
73+
#[cfg(all(feature = "xen", target_family = "unix"))]
7674
pub use mmap::xen::{GuestMemoryXen, MmapRegion as MmapRegionXen};
7775

76+
#[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
77+
pub use mmap::unix::{Error as MmapRegionError, GuestMemoryMmap, GuestRegionMmap, MmapRegion};
78+
79+
#[cfg(windows)]
80+
pub use crate::mmap::windows::{
81+
GuestMemoryWindows as GuestMemoryMmap, GuestRegionWindows as GuestRegionMmap, MmapRegion,
82+
}; // rename for backwards compat
83+
#[cfg(windows)]
84+
pub use std::io::Error as MmapRegionError;
85+
7886
pub mod volatile_memory;
7987
pub use volatile_memory::{
8088
Error as VolatileMemoryError, Result as VolatileMemoryResult, VolatileArrayRef, VolatileMemory,

0 commit comments

Comments
 (0)