MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / mmap

Method mmap

pci/src/mmap.rs:52–88  ·  view source on GitHub ↗

Create an [`MmapRegion`] using `mmap` of a file descriptor.

(
        len: u64,
        prot: c_int,
        fd: BorrowedFd,
        offset1: u64,
        offset2: u64,
    )

Source from the content-addressed store, hash-verified

50
51 /// Create an [`MmapRegion`] using `mmap` of a file descriptor.
52 pub fn mmap(
53 len: u64,
54 prot: c_int,
55 fd: BorrowedFd,
56 offset1: u64,
57 offset2: u64,
58 ) -> std::io::Result<Self> {
59 const BAD_LENGTH: &str = "Offsets must fit in libc::off_t";
60 const BAD_OFFSET: &str = "Mapping length must fit \
61in both isize and libc::size_t";
62 let Some(offset) = offset1.checked_add(offset2) else {
63 return Err(Error::new(ErrorKind::InvalidInput, BAD_OFFSET));
64 };
65 let Ok(offset) = libc::off_t::try_from(offset) else {
66 return Err(Error::new(ErrorKind::InvalidInput, BAD_OFFSET));
67 };
68 if isize::try_from(len).is_err() {
69 return Err(Error::new(ErrorKind::InvalidInput, BAD_LENGTH));
70 }
71 let Ok(len) = libc::size_t::try_from(len) else {
72 return Err(Error::new(ErrorKind::InvalidInput, BAD_LENGTH));
73 };
74
75 assert!(
76 (prot & !(libc::PROT_READ | libc::PROT_WRITE | libc::PROT_EXEC)) == 0,
77 "bad protection"
78 );
79 let flags = libc::MAP_SHARED;
80 // SAFETY: FFI call with correct parameters.
81 let addr = unsafe { libc::mmap(null_mut(), len, prot, flags, fd.as_raw_fd(), offset) };
82 if addr == libc::MAP_FAILED {
83 Err(Error::last_os_error())
84 } else {
85 let addr = addr.cast();
86 Ok(Self { addr, len })
87 }
88 }
89}

Callers

nothing calls this directly

Calls 2

newFunction · 0.85
as_raw_fdMethod · 0.45

Tested by

no test coverage detected