1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
use crate::mem::MemoryMap; use crate::types::Address; use crate::{Error, Result}; use memmap::{Mmap, MmapMut, MmapOptions}; use std::fs::File; use std::sync::Arc; use super::mmap::MappedPhysicalMemory; #[derive(Clone)] pub struct MMAPInfo<'a> { mem_map: MemoryMap<&'a [u8]>, _buf: Arc<Mmap>, } impl<'a> AsRef<MemoryMap<&'a [u8]>> for MMAPInfo<'a> { fn as_ref(&self) -> &MemoryMap<&'a [u8]> { &self.mem_map } } impl<'a> MMAPInfo<'a> { pub fn try_with_filemap(file: File, map: MemoryMap<(Address, usize)>) -> Result<Self> { let file_map = unsafe { MmapOptions::new() .map(&file) .map_err(|_| Error::Connector("unable to map file"))? }; Self::try_with_bufmap(file_map, map) } pub fn try_with_bufmap(buf: Mmap, map: MemoryMap<(Address, usize)>) -> Result<Self> { let mut new_map = MemoryMap::new(); let buf_len = buf.as_ref().len(); let buf_ptr = buf.as_ref().as_ptr(); for (base, (output_base, size)) in map.into_iter() { if output_base.as_usize() >= buf_len { return Err(Error::Connector("Memory map is out of range")); } let output_end = std::cmp::min(output_base.as_usize() + size, buf_len); new_map.push(base, unsafe { std::slice::from_raw_parts( buf_ptr.add(output_base.as_usize()), output_end - output_base.as_usize(), ) }); } Ok(Self { mem_map: new_map, _buf: Arc::new(buf), }) } pub fn into_connector(self) -> ReadMappedFilePhysicalMemory<'a> { MappedPhysicalMemory::with_info(self) } } pub type ReadMappedFilePhysicalMemory<'a> = MappedPhysicalMemory<&'a [u8], MMAPInfo<'a>>; pub struct MMAPInfoMut<'a> { mem_map: MemoryMap<&'a mut [u8]>, _buf: MmapMut, } impl<'a> AsRef<MemoryMap<&'a mut [u8]>> for MMAPInfoMut<'a> { fn as_ref(&self) -> &MemoryMap<&'a mut [u8]> { &self.mem_map } } impl<'a> MMAPInfoMut<'a> { pub fn try_with_filemap_mut(file: File, map: MemoryMap<(Address, usize)>) -> Result<Self> { let file_map = unsafe { MmapOptions::new() .map_mut(&file) .map_err(|_| Error::Connector("unable to map file"))? }; Self::try_with_bufmap_mut(file_map, map) } pub fn try_with_bufmap_mut(mut buf: MmapMut, map: MemoryMap<(Address, usize)>) -> Result<Self> { let mut new_map = MemoryMap::new(); let buf_len = buf.as_ref().len(); let buf_ptr = buf.as_mut().as_mut_ptr(); for (base, (output_base, size)) in map.into_iter() { if output_base.as_usize() >= buf_len { return Err(Error::Connector("Memory map is out of range")); } let output_end = std::cmp::min(output_base.as_usize() + size, buf_len); new_map.push(base, unsafe { std::slice::from_raw_parts_mut( buf_ptr.add(output_base.as_usize()), output_end - output_base.as_usize(), ) }); } Ok(Self { mem_map: new_map, _buf: buf, }) } pub fn into_connector(self) -> WriteMappedFilePhysicalMemory<'a> { MappedPhysicalMemory::with_info(self) } } pub type WriteMappedFilePhysicalMemory<'a> = MappedPhysicalMemory<&'a mut [u8], MMAPInfoMut<'a>>;