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
mod addr;
pub(crate) use addr::Address;
mod entry;
pub(crate) use entry::Entry;
mod generation;
pub(crate) use generation::Generation;
mod page;
mod shard;
use shard::Shard;
mod slot;
use slot::Slot;
mod stack;
use stack::TransferStack;
#[cfg(all(loom, test))]
mod tests;
use crate::loom::sync::Mutex;
use crate::util::bit;
use std::fmt;
#[cfg(target_pointer_width = "64")]
const MAX_THREADS: usize = 4096;
#[cfg(target_pointer_width = "32")]
const MAX_THREADS: usize = 2048;
const MAX_PAGES: usize = bit::pointer_width() as usize / 4;
cfg_not_loom! {
const INITIAL_PAGE_SIZE: usize = 32;
}
cfg_loom! {
const INITIAL_PAGE_SIZE: usize = 2;
}
pub(crate) struct Slab<T> {
shard: Shard<T>,
local: Mutex<()>,
}
unsafe impl<T: Send> Send for Slab<T> {}
unsafe impl<T: Sync> Sync for Slab<T> {}
impl<T: Entry> Slab<T> {
pub(crate) fn new() -> Slab<T> {
Slab {
shard: Shard::new(),
local: Mutex::new(()),
}
}
pub(crate) fn alloc(&self) -> Option<Address> {
let _local = self.local.lock().unwrap();
self.shard.alloc()
}
pub(crate) fn remove(&self, idx: Address) {
let lock = self.local.try_lock();
if lock.is_ok() {
self.shard.remove_local(idx)
} else {
self.shard.remove_remote(idx)
}
}
pub(crate) fn get(&self, token: Address) -> Option<&T> {
self.shard.get(token)
}
}
impl<T> fmt::Debug for Slab<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Slab").field("shard", &self.shard).finish()
}
}