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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use core::cmp;
use core::marker::PhantomPinned;
use core::mem;
use core::ptr;
use core::sync::atomic::AtomicU32;
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
use core::u32;
use swim_core::reify::{Reified, Reify};
use crate::block::{Block, Layout};
use crate::alloc::{AllocTag, Hold, HoldError};
pub(crate) struct PackBase<'a> {
base: Reified<Hold<'a>>,
size: u32,
mark: AtomicU32,
#[allow(dead_code)]
pinned: PhantomPinned,
}
impl<'a> PackBase<'a> {
pub(crate) fn from_block(block: Block<'a>, header_size: usize) -> *mut PackBase<'a> {
let tag_align = mem::align_of::<AllocTag>();
let header_size = header_size.wrapping_add(tag_align).wrapping_sub(1) & !tag_align.wrapping_sub(1);
let block_size = block.size();
if block_size < header_size {
panic!("block too small");
}
if block_size > u32::MAX as usize {
panic!("block too large");
}
let pack_ptr = block.as_ptr() as *mut PackBase<'a>;
unsafe {
ptr::write(pack_ptr, PackBase {
base: Reified::uninitialized(),
size: block_size as u32,
mark: AtomicU32::new(header_size as u32),
pinned: PhantomPinned,
});
pack_ptr
}
}
#[inline]
pub(crate) fn size(&self) -> usize {
self.size as usize
}
#[inline]
pub(crate) fn free(&self) -> usize {
self.size.wrapping_sub(self.mark.load(Relaxed)) as usize
}
#[inline]
pub(crate) unsafe fn as_block(&mut self) -> Block<'a> {
let data = self as *mut PackBase<'a> as *mut u8;
let size = self.size as usize;
Block::from_raw_parts(data, size)
}
#[inline]
pub(crate) unsafe fn alloc(&self, layout: Layout) -> Result<Block<'a>, HoldError> {
let tag_align = mem::align_of::<AllocTag>();
let tag_size = mem::size_of::<AllocTag>();
let align = cmp::max(layout.align(), tag_align);
let size = layout.size().wrapping_add(tag_align).wrapping_sub(1) & !tag_align.wrapping_sub(1);
let base_addr = self as *const PackBase<'a> as usize;
let mut old_mark = self.mark.load(Relaxed);
loop {
let start_addr = base_addr.wrapping_add(old_mark as usize);
let block_addr = start_addr.wrapping_add(tag_size);
let block_addr = block_addr.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
let end_addr = match block_addr.checked_add(size) {
Some(addr) => addr,
None => return Err(HoldError::OutOfMemory),
};
let new_mark = end_addr.wrapping_sub(base_addr);
if new_mark > self.size as usize {
return Err(HoldError::OutOfMemory);
}
let commit = self.mark.compare_exchange_weak(old_mark, new_mark as u32, Acquire, Relaxed);
if let Err(mark) = commit {
old_mark = mark;
continue;
}
let tag_addr = block_addr.wrapping_sub(mem::size_of::<AllocTag>()) as *mut AllocTag<'a>;
ptr::write(tag_addr, AllocTag::new(&self.base));
return Ok(Block::from_raw_parts(block_addr as *mut u8, size))
}
}
#[inline]
pub(crate) unsafe fn dealloc(&self, block: Block<'a>) -> usize {
let tag_align = mem::align_of::<AllocTag>();
let tag_size = mem::size_of::<AllocTag>();
let size = block.size().wrapping_add(tag_align).wrapping_sub(1) & !tag_align.wrapping_sub(1);
if size != 0 {
let base_addr = self as *const PackBase<'a> as usize;
let end_addr = (block.as_ptr() as usize).wrapping_add(size);
let header_addr = (block.as_ptr() as usize).wrapping_sub(tag_size);
let old_mark = end_addr.wrapping_sub(base_addr) as u32;
let new_mark = header_addr.wrapping_sub(base_addr) as u32;
let _ = self.mark.compare_exchange(old_mark, new_mark, Release, Relaxed);
}
size
}
#[inline]
pub(crate) unsafe fn resize(&self, block: Block<'a>, layout: Layout) -> Result<Block<'a>, HoldError> {
let tag_align = mem::align_of::<AllocTag>();
let block_addr = block.as_ptr() as usize;
if block_addr % layout.align() != 0 {
return Err(HoldError::Misaligned);
}
let old_size = block.size().wrapping_add(tag_align).wrapping_sub(1) & !tag_align.wrapping_sub(1);
if old_size == 0 {
return Err(HoldError::Unsupported("resize from zero"));
}
let new_size = layout.size().wrapping_add(tag_align).wrapping_sub(1) & !tag_align.wrapping_sub(1);
if new_size == 0 {
return Err(HoldError::Unsupported("resize to zero"));
}
let size_diff = (new_size as isize).wrapping_sub(old_size as isize);
let base_addr = self as *const PackBase<'a> as usize;
let old_end_addr = block_addr.wrapping_add(old_size);
let old_end_mark = old_end_addr.wrapping_sub(base_addr) as u32;
let new_end_addr = if size_diff != 0 {
match block_addr.checked_add(new_size) {
Some(addr) => addr,
None => return Err(HoldError::Oversized),
}
} else {
return Ok(block);
};
let new_end_mark = new_end_addr.wrapping_sub(base_addr) as u32;
let commit = self.mark.compare_exchange(old_end_mark, new_end_mark, SeqCst, Relaxed);
if let Err(_) = commit {
return Err(HoldError::Oversized);
}
return Ok(Block::from_raw_parts(block.as_ptr(), new_size))
}
}
impl<'a> Reify<'a, Hold<'a> + 'a> for PackBase<'a> {
#[inline]
unsafe fn deify(object: &mut (Hold<'a> + 'a)) {
Reified::<Hold<'a>>::deify(mem::transmute(object));
}
#[inline]
unsafe fn reify(base: &'a Reified<Hold<'a> + 'a>) -> &'a (Hold<'a> + 'a) {
mem::transmute(base.reify())
}
}
pub struct Pack<'a> {
base: PackBase<'a>,
live: AtomicU32,
used: AtomicU32,
zero: AllocTag<'a>,
}
impl<'a> Pack<'a> {
pub fn new(block: Block<'a>) -> &'a Pack<'a> {
unsafe { &*Pack::from_block(block, mem::size_of::<Pack<'a>>()) }
}
pub fn from_block(block: Block<'a>, header_size: usize) -> *mut Pack<'a> {
let pack = PackBase::from_block(block, header_size) as *mut Pack<'a>;
unsafe {
ptr::write(&mut (*pack).live, AtomicU32::new(0));
ptr::write(&mut (*pack).used, AtomicU32::new(0));
ptr::write(&mut (*pack).zero, AllocTag::new(&(*pack).base.base));
Pack::deify(&mut *pack);
}
pack
}
#[inline]
fn empty(&self) -> Block<'a> {
let tag_addr = &self.zero as *const AllocTag<'a> as usize;
let zero_addr = tag_addr.wrapping_add(mem::size_of::<AllocTag<'a>>());
unsafe { Block::from_raw_parts(zero_addr as *mut u8, 0) }
}
#[inline]
pub fn size(&self) -> usize {
self.base.size()
}
#[inline]
pub fn free(&self) -> usize {
self.base.free()
}
#[inline]
pub fn live(&self) -> usize {
self.live.load(Relaxed) as usize
}
#[inline]
pub fn used(&self) -> usize {
self.used.load(Relaxed) as usize
}
#[inline]
pub unsafe fn as_block(&mut self) -> Block<'a> {
self.base.as_block()
}
}
unsafe impl<'a> Hold<'a> for Pack<'a> {
unsafe fn alloc(&self, layout: Layout) -> Result<Block<'a>, HoldError> {
if layout.size() == 0 {
self.live.fetch_add(1, Relaxed);
return Ok(self.empty());
}
let block = self.base.alloc(layout)?;
self.live.fetch_add(1, Relaxed);
self.used.fetch_add(block.size() as u32, Relaxed);
Ok(block)
}
unsafe fn dealloc(&self, block: Block<'a>) -> usize {
let size = self.base.dealloc(block);
self.used.fetch_sub(size as u32, Relaxed);
self.live.fetch_sub(1, Relaxed);
size
}
unsafe fn resize(&self, block: Block<'a>, layout: Layout) -> Result<Block<'a>, HoldError> {
let old_size = block.size();
match self.base.resize(block, layout) {
Ok(block) => {
let new_size = block.size();
let size_diff = (new_size as isize).wrapping_sub(old_size as isize);
if size_diff > 0 {
self.used.fetch_add(size_diff as u32, Relaxed);
} else if size_diff < 0 {
self.used.fetch_sub(-size_diff as u32, Relaxed);
}
Ok(block)
},
err @ Err(_) => err,
}
}
}
impl<'a> Reify<'a, Hold<'a> + 'a> for Pack<'a> {
#[inline]
unsafe fn deify(object: &mut (Hold<'a> + 'a)) {
Reified::<Hold<'a>>::deify(mem::transmute(object));
}
#[inline]
unsafe fn reify(base: &'a Reified<Hold<'a> + 'a>) -> &'a (Hold<'a> + 'a) {
mem::transmute(base.reify())
}
}