Skip to content

Commit 66df193

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 1f01521 commit 66df193

File tree

9 files changed

+307
-227
lines changed

9 files changed

+307
-227
lines changed

src/bitmap/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ pub(crate) mod tests {
285285
// them to test the mmap-based backend implementations for now. Going forward, the generic
286286
// test functions defined here can be placed in a separate module (i.e. `test_utilities`)
287287
// which is gated by a feature and can be used for testing purposes by other crates as well.
288-
#[cfg(feature = "backend-mmap")]
288+
#[cfg(all(feature = "backend-mmap", target_family = "unix"))]
289289
fn test_guest_memory_region<R: GuestMemoryRegion>(region: &R) {
290290
let dirty_addr = MemoryRegionAddress(0x0);
291291
let val = 123u64;
@@ -319,7 +319,7 @@ pub(crate) mod tests {
319319
);
320320
}
321321

322-
#[cfg(feature = "backend-mmap")]
322+
#[cfg(all(feature = "backend-mmap", target_family = "unix"))]
323323
// Assumptions about M generated by f ...
324324
pub fn test_guest_memory_and_region<M, F>(f: F)
325325
where

src/bytes.rs

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

src/guest_memory.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl FileOffset {
174174
/// # Examples (uses the `backend-mmap` and `backend-atomic` features)
175175
///
176176
/// ```
177-
/// # #[cfg(feature = "backend-mmap")]
177+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
178178
/// # {
179179
/// # use std::sync::Arc;
180180
/// # use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryMmap};
@@ -292,7 +292,7 @@ pub trait GuestMemory {
292292
/// `backend-mmap` feature)
293293
///
294294
/// ```
295-
/// # #[cfg(feature = "backend-mmap")]
295+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
296296
/// # {
297297
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryRegion, GuestMemoryMmap};
298298
/// #
@@ -316,7 +316,7 @@ pub trait GuestMemory {
316316
/// # Examples (uses the `backend-mmap` feature)
317317
///
318318
/// ```
319-
/// # #[cfg(feature = "backend-mmap")]
319+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
320320
/// # {
321321
/// # use vm_memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap};
322322
/// #
@@ -428,7 +428,7 @@ pub trait GuestMemory {
428428
/// # Examples (uses the `backend-mmap` feature)
429429
///
430430
/// ```
431-
/// # #[cfg(feature = "backend-mmap")]
431+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
432432
/// # {
433433
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
434434
/// #
@@ -485,7 +485,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
485485
/// * Write a slice at guestaddress 0x1000. (uses the `backend-mmap` feature)
486486
///
487487
/// ```
488-
/// # #[cfg(feature = "backend-mmap")]
488+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
489489
/// # {
490490
/// # use vm_memory::{Bytes, GuestAddress, mmap::GuestMemoryMmap};
491491
/// #
@@ -513,7 +513,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
513513
/// * Read a slice of length 16 at guestaddress 0x1000. (uses the `backend-mmap` feature)
514514
///
515515
/// ```
516-
/// # #[cfg(feature = "backend-mmap")]
516+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
517517
/// # {
518518
/// # use vm_memory::{Bytes, GuestAddress, mmap::GuestMemoryMmap};
519519
/// #

src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ pub use io::{ReadVolatile, WriteVolatile};
6161
#[cfg(feature = "backend-mmap")]
6262
pub mod mmap;
6363

64-
#[cfg(feature = "backend-mmap")]
65-
pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapRegion};
6664
#[cfg(all(feature = "backend-mmap", feature = "xen", target_family = "unix"))]
6765
pub use mmap::{MmapRange, MmapXenFlags};
6866

69-
#[cfg(all(feature = "xen", unix))]
70-
pub use mmap_xen::{GuestMemoryXen, MmapRegion as MmapRegionXen};
67+
#[cfg(all(feature = "xen", target_family = "unix"))]
68+
pub use mmap::xen::{GuestMemoryXen, MmapRegion as MmapRegionXen};
69+
70+
#[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
71+
pub use mmap::unix::{Error as MmapRegionError, GuestMemoryMmap, GuestRegionMmap, MmapRegion};
72+
73+
#[cfg(windows)]
74+
pub use crate::mmap::windows::{
75+
GuestMemoryWindows as GuestMemoryMmap, GuestRegionWindows as GuestRegionMmap, MmapRegion,
76+
}; // rename for backwards compat
77+
#[cfg(windows)]
78+
pub use std::io::Error as MmapRegionError;
7179

7280
pub mod volatile_memory;
7381
pub use volatile_memory::{

0 commit comments

Comments
 (0)