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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use super::{SlHead, HEAD_LEN, SYMBOL_LEN};
use std::alloc;
use std::mem;
use std::ptr;
static mut REGION_TABLE: RegionTable = RegionTable::new();
struct RegionTable {
low_array: *mut usize,
high_array: *mut usize,
zone_array: *mut *mut Zone,
region_array: *mut *mut Region,
len: usize,
cap: usize,
lock: u8,
}
impl RegionTable {
const fn new() -> Self {
Self {
low_array: ptr::null_mut(),
high_array: ptr::null_mut(),
zone_array: ptr::null_mut(),
region_array: ptr::null_mut(),
len: 0,
cap: 0,
lock: false as u8,
}
}
unsafe fn setup(&mut self, cap: usize) {
let layout = alloc::Layout::from_size_align_unchecked(cap * 8, 8);
self.low_array = alloc::alloc(layout) as *mut usize;
self.high_array = alloc::alloc(layout) as *mut usize;
self.zone_array = alloc::alloc(layout) as *mut *mut Zone;
self.region_array = alloc::alloc(layout) as *mut *mut Region;
self.cap = cap;
}
unsafe fn resize(&mut self, cap: usize) {
let lock: *mut u8 = &mut self.lock;
while !std::intrinsics::atomic_cxchg_acq(lock, false as u8, true as u8).1 {
std::hint::spin_loop();
}
let current_layout = alloc::Layout::from_size_align_unchecked(self.cap * 8, 8);
self.low_array =
alloc::realloc(self.low_array as *mut u8, current_layout, cap * 8) as *mut usize;
self.high_array =
alloc::realloc(self.high_array as *mut u8, current_layout, cap * 8) as *mut usize;
self.zone_array =
alloc::realloc(self.zone_array as *mut u8, current_layout, cap * 8) as *mut *mut Zone;
self.region_array = alloc::realloc(self.region_array as *mut u8, current_layout, cap * 8)
as *mut *mut Region;
self.cap = cap;
std::intrinsics::atomic_store_rel(lock, false as u8);
}
unsafe fn append(&mut self, start: usize, end: usize, zone: *mut Zone, region: *mut Region) {
while self.lock == true as u8 {
std::hint::spin_loop();
}
std::intrinsics::atomic_fence_acqrel();
if self.len >= self.cap {
self.resize(self.cap * 2);
}
let len_ptr: *mut usize = &mut self.len;
let mut old_len = *len_ptr;
while !std::intrinsics::atomic_cxchg_acqrel(len_ptr, old_len, old_len + 1).1 {
old_len = *len_ptr;
}
ptr::write(self.low_array.add(old_len), start);
ptr::write(self.high_array.add(old_len), end);
ptr::write(self.zone_array.add(old_len), zone);
ptr::write(self.region_array.add(old_len), region);
}
unsafe fn index(&mut self, idx: usize) -> (usize, usize, *mut Zone, *mut Region) {
assert!(idx < self.len);
while self.lock == true as u8 {
std::hint::spin_loop();
}
std::intrinsics::atomic_fence_acqrel();
let start = ptr::read(self.low_array.add(idx));
let end = ptr::read(self.high_array.add(idx));
let zone = ptr::read(self.zone_array.add(idx));
let region = ptr::read(self.region_array.add(idx));
(start, end, zone, region)
}
}
pub unsafe fn alloc(region: *mut Region, size: usize, cfg: u8) -> *mut SlHead {
assert_ne!(region, ptr::null_mut());
let ptr = {
let size_type = cfg >> 5 == 7 || (cfg & 0b00011100) >> 2 == 7;
let pred_type = cfg & 0b00000001 != 0;
let length = if size_type && pred_type {
HEAD_LEN + SYMBOL_LEN + SYMBOL_LEN
} else if size_type || pred_type {
HEAD_LEN + SYMBOL_LEN
} else {
HEAD_LEN
} as usize
+ size;
if cfg!(feature = "memdbg") {
log::debug!("Allocating {} bytes with cfg: {:#010b}", length, cfg);
}
let region_ref = region.as_mut().unwrap();
let mut zone_ref = region_ref.head.as_mut().unwrap();
while zone_ref.used + length >= region_ref.zone_size {
zone_ref = match zone_ref.next.as_mut() {
Some(refer) => refer,
None => {
new_mem_zone(region);
region_ref.head.as_mut().unwrap()
}
};
}
let lock: *mut u8 = &mut zone_ref.lock;
while !std::intrinsics::atomic_cxchg_acq(lock, false as u8, true as u8).1 {
std::hint::spin_loop();
}
let out = zone_ref.top;
zone_ref.used += length;
zone_ref.top = out.add(length);
std::intrinsics::atomic_store_rel(lock, false as u8);
out as *mut SlHead
};
ptr::write_unaligned(ptr as *mut usize, 0);
let head = SlHead { cfg, rc: 1 };
ptr::write_unaligned(ptr, head);
ptr
}
#[derive(Debug)]
#[repr(C)]
pub struct Region {
zone_size: usize,
head: *mut Zone,
}
#[derive(Debug)]
#[repr(C)]
struct Zone {
used: usize,
top: *mut u8,
free: *mut FreeBlock,
next: *mut Zone,
lock: u8,
}
#[repr(C)]
struct FreeBlock {
field: usize,
}
const MEM_REGION_HEAD_SIZE: usize = mem::size_of::<Region>();
const MEM_ZONE_HEAD_SIZE: usize = mem::size_of::<Zone>();
pub unsafe fn acquire_mem_region(zone_size: usize) -> *mut Region {
if cfg!(feature = "memdbg") {
log::debug!("Creating mem region");
}
if REGION_TABLE.cap == 0 {
REGION_TABLE.setup(64);
}
let out = Box::into_raw(Box::from(Region {
zone_size,
head: ptr::null_mut(),
}));
new_mem_zone(out);
out
}
unsafe fn which_mem_area(ptr: *mut SlHead) -> (*mut Region, *mut Zone) {
assert_ne!(ptr, ptr::null_mut());
let pos = ptr as usize;
for idx in 0..REGION_TABLE.len {
let entry = REGION_TABLE.index(idx);
if pos >= entry.0 && pos <= entry.1 {
return (entry.3, entry.2);
}
}
panic!("invalid memory area")
}
unsafe fn new_mem_zone(region: *mut Region) {
assert_ne!(region, ptr::null_mut());
if cfg!(feature = "memdbg") {
log::debug!("Creating mem zone");
}
let region_head = region.as_mut().unwrap();
let size = region_head.zone_size;
let cur_head = region_head.head;
let ptr = {
let layout = alloc::Layout::from_size_align_unchecked(size + MEM_ZONE_HEAD_SIZE, 1);
alloc::alloc(layout) as *mut Zone
};
let start = (ptr as *mut u8).offset(MEM_ZONE_HEAD_SIZE as isize);
let end = start.offset(size as isize);
let new_head = Zone {
used: 0,
free: ptr::null_mut(),
next: cur_head,
top: start,
lock: false as u8,
};
ptr::write_unaligned(ptr, new_head);
REGION_TABLE.append(start as usize, end as usize, ptr, region);
ptr::write_unaligned((region as *mut usize).offset(1) as *mut *mut Zone, ptr);
}