seekable_async_file/
mmap.rs1use crate::common::ISyncIO;
2use off64::usz;
3use std::path::Path;
4use std::sync::Arc;
5use tokio::fs::OpenOptions;
6
7pub struct MMapIO {
9 mmap: Arc<memmap2::MmapRaw>,
10 mmap_len: usize,
11}
12
13impl MMapIO {
14 pub async fn open(path: &Path, size: u64, flags: i32) -> Self {
22 let async_fd = OpenOptions::new()
23 .read(true)
24 .write(true)
25 .custom_flags(flags)
26 .open(path)
27 .await
28 .unwrap();
29
30 let fd = async_fd.into_std().await;
31
32 Self {
33 mmap: Arc::new(memmap2::MmapRaw::map_raw(&fd).unwrap()),
34 mmap_len: usz!(size),
35 }
36 }
37
38 pub unsafe fn get_mmap_raw_ptr(&self, offset: u64) -> *const u8 {
39 self.mmap.as_ptr().add(usz!(offset))
40 }
41
42 pub unsafe fn get_mmap_raw_mut_ptr(&self, offset: u64) -> *mut u8 {
43 self.mmap.as_mut_ptr().add(usz!(offset))
44 }
45}
46
47impl ISyncIO for MMapIO {
48 fn read_at_sync(&self, offset: u64, len: u64) -> Vec<u8> {
49 let offset = usz!(offset);
50 let len = usz!(len);
51 let memory = unsafe { std::slice::from_raw_parts(self.mmap.as_ptr(), self.mmap_len) };
52 memory[offset..offset + len].to_vec()
53 }
54
55 fn write_at_sync(&self, offset: u64, data: &[u8]) -> () {
56 let offset = usz!(offset);
57 let len = data.as_ref().len();
58
59 let memory = unsafe { std::slice::from_raw_parts_mut(self.mmap.as_mut_ptr(), self.mmap_len) };
60 memory[offset..offset + len].copy_from_slice(data.as_ref());
61 }
62
63 fn sync_data_sync(&self) -> () {
64 self.mmap.flush().unwrap();
65 }
66}