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
use std::collections::{BTreeSet, VecDeque};
use std::marker::PhantomData;
use std::ops::AddAssign;
use std::sync::Mutex;
#[derive(Debug, Default)]
pub struct Gen<Id: From<u64> + Default> {
id: u64,
phantom: PhantomData<Id>,
}
impl<Id: From<u64> + Default> Gen<Id> {
pub fn allocate_id(&mut self) -> Id {
let id = self.id;
self.id += 1;
id.into()
}
}
pub type IdGen = Gen<u64>;
#[derive(Debug)]
pub struct IdAllocator<T>(Mutex<IdAllocatorInner<T>>);
#[derive(Debug)]
struct IdAllocatorInner<T> {
next: T,
max: T,
free: VecDeque<T>,
}
impl<T> IdAllocator<T>
where
T: From<u8> + AddAssign + PartialOrd + Copy,
{
pub fn new(min: T, max: T) -> IdAllocator<T> {
IdAllocator(Mutex::new(IdAllocatorInner {
next: min,
max,
free: VecDeque::new(),
}))
}
pub fn alloc(&self) -> Option<T> {
let mut inner = self.0.lock().expect("lock poisoned");
if let Some(id) = inner.free.pop_front() {
Some(id)
} else {
let id = inner.next;
if id > inner.max {
None
} else {
inner.next += 1.into();
Some(id)
}
}
}
pub fn free(&self, id: T) {
let mut inner = self.0.lock().expect("lock poisoned");
inner.free.push_back(id);
}
}
#[derive(Debug)]
pub struct PortAllocator(Mutex<BTreeSet<u16>>);
impl PortAllocator {
pub fn new(min: u16, max: u16) -> PortAllocator {
PortAllocator(Mutex::new((min..=max).collect()))
}
pub fn new_with_filter(min: u16, max: u16, banned: &[u16]) -> PortAllocator {
PortAllocator(Mutex::new(
(min..=max).filter(|p| !banned.contains(p)).collect(),
))
}
pub fn alloc(&self) -> Option<u16> {
let mut inner = self.0.lock().expect("lock poisoned");
let port = inner.iter().next().cloned();
if let Some(port) = port {
assert!(inner.remove(&port));
}
port
}
pub fn free(&self, id: u16) {
let mut inner = self.0.lock().expect("lock poisoned");
let _ = inner.insert(id);
}
pub fn mark_allocated(&self, port: u16) -> bool {
let mut inner = self.0.lock().expect("lock poisoned");
inner.remove(&port)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_id_alloc() {
let ida = IdAllocator::new(3, 5);
assert_eq!(ida.alloc().unwrap(), 3);
assert_eq!(ida.alloc().unwrap(), 4);
assert_eq!(ida.alloc().unwrap(), 5);
ida.free(4);
assert_eq!(ida.alloc().unwrap(), 4);
ida.free(5);
ida.free(3);
assert_eq!(ida.alloc().unwrap(), 5);
assert_eq!(ida.alloc().unwrap(), 3);
match ida.alloc() {
Some(id) => panic!(
"id allocator returned {}, not expected id exhaustion error",
id
),
None => (),
}
}
#[test]
fn test_port_allocator() {
let ida = PortAllocator::new(1, 10);
assert!(ida.mark_allocated(4));
while let Some(id) = ida.alloc() {
assert_ne!(4, id);
}
ida.free(3);
assert_eq!(3, ida.alloc().unwrap());
assert!(ida.alloc().is_none());
ida.free(4);
assert_eq!(4, ida.alloc().unwrap());
assert!(ida.alloc().is_none());
assert!(!ida.mark_allocated(9));
}
}