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
use crate::base::Base;
use crate::{internal, RawMem};
use memmap2::{MmapMut, MmapOptions};
use std::{
    cmp::max,
    error::Error,
    fs::File,
    io,
    mem::{size_of, ManuallyDrop},
    ptr,
    ptr::NonNull,
};

pub struct FileMappedMem<T> {
    base: Base<T>,
    allocated: usize,
    pub(crate) file: File,
    mapping: ManuallyDrop<MmapMut>, // TODO: `MaybeUninit`
}

impl<T: Default> FileMappedMem<T> {
    pub fn new(file: File) -> io::Result<Self> {
        let capacity = Base::<T>::MINIMUM_CAPACITY / size_of::<T>();
        let mapping = unsafe { MmapOptions::new().map_mut(&file)? };

        let mut new = Self {
            allocated: 0,
            base: Base::new(NonNull::slice_from_raw_parts(NonNull::dangling(), 0)),
            mapping: ManuallyDrop::new(mapping),
            file,
        };

        new.alloc_impl(capacity).map(|_| new)
    }

    unsafe fn map(&mut self, capacity: usize) -> io::Result<&mut [u8]> {
        let mapping = MmapOptions::new().len(capacity).map_mut(&self.file)?;
        self.mapping = ManuallyDrop::new(mapping);
        Ok(self.mapping.as_mut())
    }

    unsafe fn unmap(&mut self) {
        ManuallyDrop::drop(&mut self.mapping)
    }

    fn alloc_impl(&mut self, capacity: usize) -> io::Result<()> {
        self.allocated = capacity;
        let alloc_cap = capacity * size_of::<T>();

        // SAFETY: `self.mapping` is initialized
        unsafe {
            self.unmap();
        }
        let file_len = self.file.metadata()?.len();
        self.file.set_len(max(file_len, alloc_cap as u64))?;

        let bytes = unsafe { self.map(alloc_cap) }?;
        self.base.ptr = NonNull::from(internal::guaranteed_align_slice(bytes));
        Ok(())
    }
}

impl<T: Default> RawMem<T> for FileMappedMem<T> {
    fn alloc(&mut self, capacity: usize) -> io::Result<&mut [T]> {
        self.alloc_impl(capacity)?;

        // SAFETY: `ptr` is valid slice
        unsafe { Ok(self.base.ptr.as_mut()) }
    }

    fn allocated(&self) -> usize {
        self.allocated
    }

    fn occupy(&mut self, capacity: usize) -> io::Result<()> {
        self.base.occupy(capacity)
    }

    fn occupied(&self) -> usize {
        self.base.occupied
    }
}

impl<T> Drop for FileMappedMem<T> {
    fn drop(&mut self) {
        // SAFETY: `slice` is valid file piece
        // SAFETY: items is friendly to drop
        unsafe {
            let ptr = self.base.ptr;
            let mut ptr = NonNull::slice_from_raw_parts(ptr.as_non_null_ptr(), self.allocated);
            let slice = ptr.as_mut();
            for item in slice {
                ptr::drop_in_place(item);
            }
        }

        // SAFETY: `self.mapping` is initialized
        unsafe {
            ManuallyDrop::drop(&mut self.mapping);
        }

        let _: Result<_, Box<dyn Error>> = try {
            self.file.sync_all()?;
        };
    }
}