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
use std::sync::Arc;
use crate::cell::RootCell;
use crate::utils::*;
use crate::result::Result;
use crate::stm::Chaperon;
use crate::*;
use std::alloc::{alloc, dealloc, Layout};
use std::collections::HashMap;
use std::ops::Range;
use std::sync::Mutex;
use std::thread::ThreadId;
use crate::cell::LazyCell;
pub use crate::alloc::*;
#[derive(Clone,Copy,Default)]
pub struct Heap {}
static mut JOURNALS: Option<HashMap<ThreadId, (u64, i32)>> = None;
static mut CHAPERONS: Option<HashMap<ThreadId, Chaperon>> = None;
static mut MUTEX: Option<Mutex<bool>> = None;
static mut LOGS: LazyCell<Mutex<Ring<(u64, u64), 8>>> =
LazyCell::new(|| Mutex::new(Ring::new()));
unsafe impl MemPool for Heap {}
unsafe impl MemPoolTraits for Heap {
fn name() -> &'static str {
"heap"
}
#[inline]
fn rng() -> Range<u64> {
0..u64::MAX
}
unsafe fn pre_alloc(size: usize) -> (*mut u8, u64, usize, usize) {
Self::discard(0);
let x = alloc(Layout::from_size_align_unchecked(size, 1));
let off = x as u64;
log!(Self, Green, "", "PRE: {:<6} ({:>6x}:{:<6x}) = {:<6} POST = {:<6}",
0, off, off + size as u64 - 1, size, 0);
(x, off, size, 0)
}
unsafe fn pre_dealloc(ptr: *mut u8, size: usize) -> usize {
Self::discard(0);
let _start = ptr as u64;
let _end = _start + size as u64;
log!(Self, Red, "DEALLOC", "PRE: {:<6} ({:>6x}:{:<6x}) = {:<6} POST = {:<6}",
0, _start, _end, _end - _start + 1, 0);
dealloc(ptr, Layout::from_size_align_unchecked(size, 1));
0
}
fn allocated(off: u64, _len: usize) -> bool {
if off >= Self::end() {
false
} else {
Self::contains(off + Self::start())
}
}
unsafe fn log64(obj: u64, val: u64, _: usize) {
let mut logs = match LOGS.lock() {
Ok(g) => g,
Err(p) => p.into_inner()
};
logs.push((obj, val))
}
unsafe fn drop_on_failure(_off: u64, _len: usize, _: usize) {}
unsafe fn perform(_: usize) {
let mut logs = match LOGS.lock() {
Ok(g) => g,
Err(p) => p.into_inner()
};
logs.foreach(|(off, data)| {
*utils::read_addr(off) = data;
});
logs.clear();
}
unsafe fn discard(_: usize) {
let mut logs = match LOGS.lock() {
Ok(g) => g,
Err(p) => p.into_inner()
};
logs.clear()
}
fn open_no_root(_path: &str, _flags: u32) -> Result<PoolGuard<Self>> {
Ok(PoolGuard::<Self>::new())
}
#[track_caller]
fn open<'a, U: 'a + PSafe + RootObj<Self>>(
path: &str,
flags: u32,
) -> Result<RootCell<'a, U, Self>> {
let slf = Self::open_no_root(path, flags)?;
if std::mem::size_of::<U>() == 0 {
Err("root type cannot be a ZST".to_string())
} else {
unsafe {
let root_off = Self::transaction(move |j| {
let ptr = Self::new(U::init(j), j);
Self::off_unchecked(ptr)
})
.unwrap();
let ptr = Self::get_unchecked(root_off);
Ok(RootCell::new(ptr, Arc::new(slf)))
}
}
}
fn is_open() -> bool {
true
}
unsafe fn format(_path: &str) -> Result<()> {
Ok(())
}
fn size() -> usize {
usize::MAX - 1
}
fn available() -> usize {
usize::MAX - 1
}
unsafe fn recover() {}
unsafe fn drop_journal(journal: &mut Journal) {
let tid = std::thread::current().id();
JOURNALS.as_mut().unwrap().remove(&tid);
Self::free_nolog(journal);
}
unsafe fn journals<T, F: Fn(&mut HashMap<ThreadId, (u64, i32)>)->T>(f: F)->T{
if JOURNALS.is_none() {
JOURNALS = Some(HashMap::new());
}
f(JOURNALS.as_mut().unwrap())
}
unsafe fn journals_head() -> &'static u64 {
static mut HEAD: u64 = u64::MAX;
&HEAD
}
unsafe fn close() -> Result<()> {
Ok(())
}
}
pub type Pbox<T> = crate::boxed::Pbox<T, Heap>;
pub type Prc<T> = crate::prc::Prc<T, Heap>;
pub type Parc<T> = crate::sync::Parc<T, Heap>;
pub type PMutex<T> = crate::sync::PMutex<T, Heap>;
pub type PCell<T> = crate::cell::PCell<T, Heap>;
pub type PNonNull<T> = crate::ptr::LogNonNull<T, Heap>;
pub type PRefCell<T> = crate::cell::PRefCell<T, Heap>;
pub type PRef<'b, T> = crate::cell::Ref<'b, T, Heap>;
pub type PRefMut<'b, T> = crate::cell::RefMut<'b, T, Heap>;
pub type VCell<T> = crate::cell::VCell<T, Heap>;
pub type PVec<T> = crate::vec::Vec<T, Heap>;
pub type PString = crate::str::String<Heap>;
pub type Journal = crate::stm::Journal<Heap>;
pub mod prc {
pub type PWeak<T> = crate::prc::Weak<T, super::Heap>;
pub type VWeak<T> = crate::prc::VWeak<T, super::Heap>;
}
pub mod parc {
pub type PWeak<T> = crate::sync::Weak<T, super::Heap>;
pub type VWeak<T> = crate::sync::VWeak<T, super::Heap>;
}