pub fn create_host_mmaps(
    ranges: &[(u64, u64)],
    mem_config: &MachineMemConfig,
) -> Result<Vec<Arc<HostMemMapping>>> {
    let mut f_back: Option<FileBackend> = None;

    if let Some(path) = &mem_config.mem_path {
        let file_len = ranges.iter().fold(0, |acc, x| acc + x.1);
        f_back = Some(
            FileBackend::new(&path, file_len)
                .chain_err(|| "Failed to create file that backs memory")?,
        );
    } else if mem_config.mem_share {
        let file_len = ranges.iter().fold(0, |acc, x| acc + x.1);
        let anon_mem_name = String::from("stratovirt_anon_mem");

        let anon_fd =
            unsafe { libc::syscall(libc::SYS_memfd_create, anon_mem_name.as_ptr(), 0) } as RawFd;
        if anon_fd < 0 {
            return Err(std::io::Error::last_os_error()).chain_err(|| "Failed to create memfd");
        }

        let anon_file = unsafe { File::from_raw_fd(anon_fd) };
        anon_file
            .set_len(file_len)
            .chain_err(|| "Failed to set the length of anonymous file that backs memory")?;

        f_back = Some(FileBackend {
            file: Arc::new(anon_file),
            offset: 0,
            page_size: host_page_size(),
        });
    }

    let mut host_addr = do_mmap(
        false,
        mem_config.mem_size,
        &f_back,
        mem_config.mem_share,
        mem_config.dump_guest_core,
    )?;
    let mut mappings = Vec::new();
    for range in ranges.iter() {
        mappings.push(Arc::new(HostMemMapping::new(
            GuestAddress(range.0),
            Some(host_addr),
            range.1,
            f_back.clone(),
            mem_config.dump_guest_core,
            mem_config.mem_share,
            false,
        )?));
        host_addr += range.1;

        if let Some(mut fb) = f_back.as_mut() {
            fb.offset += range.1
        }
    }

    Ok(mappings)
}
/// Create HostMemMappings according to address ranges.
///
/// # Arguments
///
/// * `ranges` - The guest address range that will be mapped.
/// * `mem_config` - Machine memory config.
Create HostMemMappings according to address ranges.
Create HostMemMappings according to address ranges .
pub fn create_host_mmaps(
    ranges: &[(u64, u64)],
    mem_config: &MachineMemConfig,
) -> Result<Vec<Arc<HostMemMapping>>> {
    let mut f_back: Option<FileBackend> = None;
    if let Some(path) = &mem_config.mem_path {
        let file_len = ranges.iter().fold(0, |acc, x| acc + x.1);
        f_back = Some(
            FileBackend::new(&path, file_len)
                .chain_err(|| "Failed to create file that backs memory")?,
        );
    } else if mem_config.mem_share {
        let file_len = ranges.iter().fold(0, |acc, x| acc + x.1);
        let anon_mem_name = String::from("stratovirt_anon_mem");
        let anon_fd =
            unsafe { libc::syscall(libc::SYS_memfd_create, anon_mem_name.as_ptr(), 0) } as RawFd;
        if anon_fd < 0 {
            return Err(std::io::Error::last_os_error()).chain_err(|| "Failed to create memfd");
        }
        let anon_file = unsafe { File::from_raw_fd(anon_fd) };
        anon_file
            .set_len(file_len)
            .chain_err(|| "Failed to set the length of anonymous file that backs memory")?;
        f_back = Some(FileBackend {
            file: Arc::new(anon_file),
            offset: 0,
            page_size: host_page_size(),
        });
    }
    let mut host_addr = do_mmap(
        false,
        mem_config.mem_size,
        &f_back,
        mem_config.mem_share,
        mem_config.dump_guest_core,
    )?;
    let mut mappings = Vec::new();
    for range in ranges.iter() {
        mappings.push(Arc::new(HostMemMapping::new(
            GuestAddress(range.0),
            Some(host_addr),
            range.1,
            f_back.clone(),
            mem_config.dump_guest_core,
            mem_config.mem_share,
            false,
        )?));
        host_addr += range.1;
        if let Some(mut fb) = f_back.as_mut() {
            fb.offset += range.1
        }
    }
    Ok(mappings)
}
pub fn create_host_mmaps ( ranges : & [ ( u64 , u64 ) ] , mem_config : & MachineMemConfig , ) -> Result < Vec < Arc < HostMemMapping > > > { let mut f_back : Option < FileBackend > = None ; if let Some ( path ) = & mem_config . mem_path { let file_len = ranges . iter ( ) . fold ( 0 , | acc , x | acc + x . 1 ) ; f_back = Some ( FileBackend :: new ( & path , file_len ) . chain_err ( | | " " ) ? , ) ; } else if mem_config . mem_share { let file_len = ranges . iter ( ) . fold ( 0 , | acc , x | acc + x . 1 ) ; let anon_mem_name = String :: from ( " " ) ; let anon_fd = unsafe { libc :: syscall ( libc :: SYS_memfd_create , anon_mem_name . as_ptr ( ) , 0 ) } as RawFd ; if anon_fd < 0 { return Err ( std :: io :: Error :: last_os_error ( ) ) . chain_err ( | | " " ) ; } let anon_file = unsafe { File :: from_raw_fd ( anon_fd ) } ; anon_file . set_len ( file_len ) . chain_err ( | | " " ) ? ; f_back = Some ( FileBackend { file : Arc :: new ( anon_file ) , offset : 0 , page_size : host_page_size ( ) , } ) ; } let mut host_addr = do_mmap ( false , mem_config . mem_size , & f_back , mem_config . mem_share , mem_config . dump_guest_core , ) ? ; let mut mappings = Vec :: new ( ) ; for range in ranges . iter ( ) { mappings . push ( Arc :: new ( HostMemMapping :: new ( GuestAddress ( range . 0 ) , Some ( host_addr ) , range . 1 , f_back . clone ( ) , mem_config . dump_guest_core , mem_config . mem_share , false , ) ? ) ) ; host_addr += range . 1 ; if let Some ( mut fb ) = f_back . as_mut ( ) { fb . offset += range . 1 } } Ok ( mappings ) }
Create HostMemMappings according to address ranges.
Create HostMemMappings according to address ranges .
ranges
&[(u64, u64)]
mem_config
&MachineMemConfig
ranges
&[(u64, u64)]
mem_config
&MachineMemConfig