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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use core::cmp;
use std::io;
pub struct Storage {
inner: memmap::MmapMut,
size: usize,
end: usize,
}
impl Storage {
pub fn new(size: usize) -> io::Result<Self> {
let inner = memmap::MmapOptions::new().len(size).map_anon()?;
Ok(Self {
inner,
end: 0,
size
})
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
&self.inner[..self.end]
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
&mut self.inner[..self.end]
}
#[inline]
pub fn capacity(&self) -> usize {
self.size
}
#[inline]
pub fn flush_async(&self) -> io::Result<()> {
self.inner.flush_async()
}
#[inline]
pub fn flush_sync(&self) -> io::Result<()> {
self.inner.flush()
}
fn realloc(&mut self, new_size: usize) -> io::Result<()> {
let new_size = cmp::max(self.size + self.size, new_size);
let mut new_mmap = Self::new(new_size)?;
new_mmap.copy_from_slice(self.as_slice());
*self = new_mmap;
Ok(())
}
pub fn extend_from_slice(&mut self, data: &[u8]) -> io::Result<()> {
if (self.size - self.end) < data.len() {
let new_size = self.end + data.len();
self.realloc(new_size)?;
}
self.inner[self.end..self.end+data.len()].copy_from_slice(data);
self.end += data.len();
Ok(())
}
pub fn copy_from_slice(&mut self, data: &[u8]) {
assert!(self.size >= data.len());
self.end = data.len();
self.as_mut_slice().copy_from_slice(data);
}
pub fn resize(&mut self, new_size: usize) -> io::Result<()> {
let mut new_mmap = Self::new(new_size)?;
new_mmap.copy_from_slice(self.as_slice());
*self = new_mmap;
Ok(())
}
pub fn clear(&mut self) {
self.end = 0;
}
pub fn to_vec(&self) -> Vec<u8> {
let mut result = Vec::with_capacity(self.end);
result.extend_from_slice(self.as_slice());
result
}
}
impl Default for Storage {
fn default() -> Self {
Self::new(1024).expect("To create default mmap storage")
}
}
impl io::Write for Storage {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.extend_from_slice(buf)?;
Ok(buf.len())
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.extend_from_slice(buf)
}
#[inline]
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}