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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#![no_std]
pub mod btree;
pub const PAGE_SIZE: usize = 4096;
pub trait Storable: core::fmt::Debug {
fn compare<T: LoadPage>(&self, _txn: &T, _b: &Self) -> core::cmp::Ordering {
unimplemented!()
}
fn page_references(&self) -> Self::PageReferences;
type PageReferences: Iterator<Item = u64>;
}
#[macro_export]
macro_rules! direct_repr {
($t: ty) => {
impl Storable for $t {
type PageReferences = core::iter::Empty<u64>;
fn page_references(&self) -> Self::PageReferences {
core::iter::empty()
}
fn compare<T>(&self, _: &T, b: &Self) -> core::cmp::Ordering {
self.cmp(b)
}
}
impl UnsizedStorable for $t {
const ALIGN: usize = core::mem::align_of::<$t>();
fn size(&self) -> usize {
core::mem::size_of::<Self>()
}
unsafe fn onpage_size(_: *const u8) -> usize {
core::mem::size_of::<Self>()
}
unsafe fn write_to_page(&self, p: *mut u8) {
core::ptr::copy_nonoverlapping(self, p as *mut Self, 1)
}
unsafe fn from_raw_ptr<'a, T>(_: &T, p: *const u8) -> &'a Self {
&*(p as *const Self)
}
}
};
}
direct_repr!(());
direct_repr!(u8);
direct_repr!(i8);
direct_repr!(u16);
direct_repr!(i16);
direct_repr!(u32);
direct_repr!(i32);
direct_repr!(u64);
direct_repr!(i64);
direct_repr!([u8; 16]);
pub trait UnsizedStorable: Storable {
const ALIGN: usize;
fn size(&self) -> usize;
unsafe fn onpage_size(_: *const u8) -> usize;
unsafe fn write_to_page(&self, p: *mut u8);
unsafe fn from_raw_ptr<'a, T>(_: &T, p: *const u8) -> &'a Self;
}
impl Storable for [u8] {
type PageReferences = core::iter::Empty<u64>;
fn page_references(&self) -> Self::PageReferences {
core::iter::empty()
}
fn compare<T>(&self, _: &T, b: &Self) -> core::cmp::Ordering {
self.cmp(b)
}
}
impl UnsizedStorable for [u8] {
const ALIGN: usize = 2;
fn size(&self) -> usize {
2 + self.len()
}
unsafe fn from_raw_ptr<'a, T>(_: &T, p: *const u8) -> &'a Self {
let len = u16::from_le(*(p as *const u16));
assert_ne!(len, 0);
assert_eq!(len & 0xf000, 0);
core::slice::from_raw_parts(p.add(2), len as usize)
}
unsafe fn onpage_size(p: *const u8) -> usize {
let len = u16::from_le(*(p as *const u16));
2 + len as usize
}
unsafe fn write_to_page(&self, p: *mut u8) {
assert!(self.len() <= 510);
*(p as *mut u16) = (self.len() as u16).to_le();
core::ptr::copy_nonoverlapping(self.as_ptr(), p.add(2), self.len())
}
}
unsafe fn read<T: LoadPage, K: UnsizedStorable + ?Sized, V: UnsizedStorable + ?Sized>(
_txn: &T,
k: *const u8,
) -> (*const u8, *const u8) {
let s = K::onpage_size(k);
let v = k.add(s);
let al = v.align_offset(V::ALIGN);
let v = v.add(al);
(k, v)
}
unsafe fn entry_size<K: UnsizedStorable + ?Sized, V: UnsizedStorable + ?Sized>(
k: *const u8,
) -> usize {
let ks = K::onpage_size(k);
let va = V::ALIGN;
let v_off = (ks + va - 1) & !(va - 1);
let v_ptr = k.add(v_off);
let vs = V::onpage_size(v_ptr);
let ka = K::ALIGN.max(V::ALIGN);
let size = v_off + vs;
(size + ka - 1) & !(ka - 1)
}
#[derive(Debug)]
#[repr(C)]
pub struct CowPage {
pub data: *mut u8,
pub offset: u64,
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct Page<'a> {
pub data: &'a [u8; PAGE_SIZE],
pub offset: u64,
}
impl CowPage {
pub fn as_page(&self) -> Page {
Page {
data: unsafe { &*(self.data as *const [u8; PAGE_SIZE]) },
offset: self.offset,
}
}
#[cfg(feature = "crc32")]
pub fn crc(&self, hasher: &crc32fast::Hasher) -> u32 {
let mut hasher = hasher.clone();
hasher.reset();
unsafe {
hasher.update(core::slice::from_raw_parts(self.data, 4));
hasher.update(core::slice::from_raw_parts(
self.data.offset(8),
PAGE_SIZE - 8,
));
}
hasher.finalize()
}
#[cfg(feature = "crc32")]
pub fn crc_check(&self, hasher: &crc32fast::Hasher) -> bool {
let crc = unsafe { u32::from_le(*(self.data as *const u32)) };
self.crc(hasher) == crc
}
}
#[derive(Debug)]
pub struct MutPage(pub CowPage);
impl MutPage {
#[cfg(not(feature = "crc32"))]
pub fn clear_dirty(&mut self) {
unsafe { *self.0.data &= 0xfe }
}
#[cfg(feature = "crc32")]
pub fn clear_dirty(&mut self, hasher: &crc32fast::Hasher) {
unsafe {
*self.0.data &= 0xfe;
let crc = (self.0.data as *mut u32).offset(1);
*crc = self.0.crc(hasher)
}
}
}
impl CowPage {
pub fn is_dirty(&self) -> bool {
unsafe { (*self.data) & 1 != 0 }
}
}
pub trait LoadPage {
type Error;
fn load_page(&self, off: u64) -> Result<CowPage, Self::Error>;
fn rc(&self, _off: u64) -> Result<u64, Self::Error> {
Ok(0)
}
}
pub trait AllocPage: LoadPage {
fn alloc_page(&mut self) -> Result<MutPage, Self::Error>;
fn incr_rc(&mut self, off: u64) -> Result<usize, Self::Error>;
fn decr_rc(&mut self, off: u64) -> Result<usize, Self::Error>;
fn decr_rc_owned(&mut self, off: u64) -> Result<usize, Self::Error>;
}