Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/guest_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

use std::convert::From;
use std::fmt::{self, Display};
use std::fs::File;
use std::io::{self, Read, Write};
use std::ops::{BitAnd, BitOr};
use std::sync::Arc;

use address::{Address, AddressValue};
use bytes::Bytes;
Expand Down Expand Up @@ -115,6 +117,40 @@ impl_address_ops!(MemoryRegionAddress, u64);
/// Type of the raw value stored in a GuestAddress object.
pub type GuestUsize = <GuestAddress as AddressValue>::V;

/// Represents the start point within a `File` that backs a `GuestMemoryRegion`.
#[derive(Clone, Debug)]
pub struct FileOffset {
file: Arc<File>,
start: u64,
}

impl FileOffset {
/// Creates a new `FileOffset` object.
pub fn new(file: File, start: u64) -> Self {
FileOffset::from_arc(Arc::new(file), start)
}

/// Creates a new `FileOffset` object based on an exiting `Arc<File>`.
pub fn from_arc(file: Arc<File>, start: u64) -> Self {
FileOffset { file, start }
}

/// Returns a reference to the inner `File` object.
pub fn file(&self) -> &File {
&self.file.as_ref()
}

/// Return a reference to the inner `Arc<File>` object.
pub fn arc(&self) -> &Arc<File> {
&self.file
}

/// Returns the start offset within the file.
pub fn start(&self) -> u64 {
self.start
}
}

/// Represents a continuous region of guest physical memory.
#[allow(clippy::len_without_is_empty)]
pub trait GuestMemoryRegion: Bytes<MemoryRegionAddress, E = Error> {
Expand Down Expand Up @@ -164,6 +200,9 @@ pub trait GuestMemoryRegion: Bytes<MemoryRegionAddress, E = Error> {
.and_then(|offset| self.check_address(MemoryRegionAddress(offset)))
}

/// Returns information regarding the file and offset backing this memory region.
fn file_offset(&self) -> Option<&FileOffset>;

/// Return a slice corresponding to the data in the region; unsafe because of
/// possible aliasing. Return None if the region does not support slice-based
/// access.
Expand Down Expand Up @@ -514,4 +553,16 @@ mod tests {
assert_eq!(Some(GuestAddress(0x0f)), a.checked_sub(0xf0));
assert!(a.checked_sub(0xffff).is_none());
}

#[test]
fn test_file_offset() {
let file = tempfile::tempfile().unwrap();
let start = 1234;
let file_offset = FileOffset::new(file, start);
assert_eq!(file_offset.start(), start);
assert_eq!(
file_offset.file() as *const File,
file_offset.arc().as_ref() as *const File
);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mod mmap_windows;
#[cfg(feature = "backend-mmap")]
pub mod mmap;
#[cfg(feature = "backend-mmap")]
pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapError, MmapRegion};
pub use mmap::{Error, GuestMemoryMmap, GuestRegionMmap, MmapRegion};

pub mod volatile_memory;
pub use volatile_memory::{
Expand Down
Loading