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
use core::marker::PhantomData;
use core::mem;
use core::ptr;
use core::sync::atomic::{AtomicPtr, AtomicU32};
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use core::u32;
use crate::block::{Block, Layout};
use crate::alloc::{Heap, HeapError};
pub struct Slab<'a> {
hunk: Block<'a>,
unit: u32,
live: AtomicU32,
head: AtomicPtr<FreeList>,
hunk_marker: PhantomData<&'a ()>,
}
impl<'a> Slab<'a> {
#[inline]
pub fn new(hunk: Block<'a>, unit: usize) -> Slab<'a> {
if unit < mem::size_of::<FreeList>() {
panic!("unit too small");
}
if unit > u32::MAX as usize {
panic!("unit too large");
}
let mut head = ptr::null_mut();
let block_count = hunk.size() / unit;
if block_count != 0 {
let base = hunk.as_ptr() as usize;
let mut next = base.wrapping_add(unit.wrapping_mul(block_count.wrapping_sub(1)));
loop {
let tail = next as *mut FreeList;
unsafe { ptr::write(&mut (*tail).next, AtomicPtr::new(head)); }
head = tail;
if next == base {
break;
}
next = next.wrapping_sub(unit);
}
}
Slab {
hunk: hunk,
unit: unit as u32,
live: AtomicU32::new(0),
head: AtomicPtr::new(head),
hunk_marker: PhantomData,
}
}
#[inline]
pub fn size(&self) -> usize {
self.hunk.size()
}
#[inline]
pub fn block_size(&self) -> usize {
self.unit as usize
}
#[inline]
pub fn block_count(&self) -> usize {
self.hunk.size() / self.unit as usize
}
#[inline]
pub fn live(&self) -> usize {
self.live.load(Relaxed) as usize
}
#[inline]
pub fn dead(&self) -> usize {
self.block_count() - self.live()
}
#[inline]
pub fn into_block(self) -> Block<'a> {
if self.live.load(Relaxed) != 0 {
panic!("leaky slab");
}
self.hunk
}
}
impl<'a> Heap<'a> for Slab<'a> {
unsafe fn alloc(&self, layout: Layout) -> Result<Block<'a>, HeapError> {
if layout.size() > self.unit as usize {
return Err(HeapError::Oversized);
}
let mut head = self.head.load(Relaxed);
loop {
if head.is_null() {
return Err(HeapError::OutOfMemory);
}
if head as usize % layout.align() != 0 {
return Err(HeapError::Misaligned);
}
let tail = (*head).next.load(Relaxed);
match self.head.compare_exchange_weak(head, tail, Release, Relaxed) {
Ok(block) => {
self.live.fetch_add(1, Relaxed);
return Ok(Block::from_raw_parts(block as *mut u8, self.unit as usize));
},
Err(block) => {
head = block;
continue;
},
};
}
}
unsafe fn dealloc(&self, block: Block<'a>) -> usize {
let size = block.size();
let head = block.as_ptr() as *mut FreeList;
let mut tail = self.head.load(Relaxed);
loop {
ptr::write(&mut (*head).next, AtomicPtr::new(tail));
match self.head.compare_exchange_weak(tail, head, Acquire, Relaxed) {
Ok(_) => {
self.live.fetch_sub(1, Relaxed);
return size;
},
Err(block) => {
tail = block;
continue;
},
};
}
}
}
#[repr(C)]
struct FreeList {
next: AtomicPtr<FreeList>,
}