|
16 | 16 | */ |
17 | 17 |
|
18 | 18 | use crate::core::minislab::MiniSlab; |
19 | | -use slab::Slab; |
20 | 19 | use std::cell::{Cell, RefCell}; |
21 | 20 | use std::fmt; |
22 | 21 | use std::marker::PhantomData; |
23 | 22 | use std::mem; |
24 | 23 | use std::ops::{Deref, DerefMut}; |
25 | 24 | use std::process::abort; |
26 | 25 | use std::ptr::NonNull; |
27 | | -use std::sync::Mutex; |
28 | 26 |
|
29 | 27 | pub struct InsertError<T>(pub T); |
30 | 28 |
|
@@ -105,102 +103,6 @@ impl<T> Memory<T> { |
105 | 103 | } |
106 | 104 | } |
107 | 105 |
|
108 | | -pub struct ReusableValue<T> { |
109 | | - reusable: std::sync::Arc<Reusable<T>>, |
110 | | - value: *mut T, |
111 | | - key: usize, |
112 | | -} |
113 | | - |
114 | | -impl<T> ReusableValue<T> { |
115 | | - // Vec element addresses are guaranteed to be stable once created, |
116 | | - // and elements are only removed when the Reusable is dropped, and |
117 | | - // the Arc'd Reusable is guaranteed to live as long as |
118 | | - // ReusableValue, therefore it is safe to assume the element will |
119 | | - // live at least as long as the ReusableValue |
120 | | - |
121 | | - fn get(&self) -> &T { |
122 | | - unsafe { &*self.value } |
123 | | - } |
124 | | - |
125 | | - fn get_mut(&mut self) -> &mut T { |
126 | | - unsafe { &mut *self.value } |
127 | | - } |
128 | | -} |
129 | | - |
130 | | -impl<T> Drop for ReusableValue<T> { |
131 | | - fn drop(&mut self) { |
132 | | - let mut entries = self.reusable.entries.lock().unwrap(); |
133 | | - |
134 | | - entries.0.remove(self.key); |
135 | | - } |
136 | | -} |
137 | | - |
138 | | -impl<T> Deref for ReusableValue<T> { |
139 | | - type Target = T; |
140 | | - |
141 | | - fn deref(&self) -> &Self::Target { |
142 | | - self.get() |
143 | | - } |
144 | | -} |
145 | | - |
146 | | -impl<T> DerefMut for ReusableValue<T> { |
147 | | - fn deref_mut(&mut self) -> &mut Self::Target { |
148 | | - self.get_mut() |
149 | | - } |
150 | | -} |
151 | | - |
152 | | -// Like Memory, but for preinitializing each value and reusing |
153 | | -pub struct Reusable<T> { |
154 | | - entries: Mutex<(Slab<()>, Vec<T>)>, |
155 | | -} |
156 | | - |
157 | | -impl<T> Reusable<T> { |
158 | | - pub fn new<F>(capacity: usize, init_fn: F) -> Self |
159 | | - where |
160 | | - F: Fn() -> T, |
161 | | - { |
162 | | - let mut values = Vec::with_capacity(capacity); |
163 | | - |
164 | | - for _ in 0..capacity { |
165 | | - values.push(init_fn()); |
166 | | - } |
167 | | - |
168 | | - // Allocate the slab with fixed capacity |
169 | | - let s = Slab::with_capacity(capacity); |
170 | | - |
171 | | - Self { |
172 | | - entries: Mutex::new((s, values)), |
173 | | - } |
174 | | - } |
175 | | - |
176 | | - #[cfg(test)] |
177 | | - pub fn len(&self) -> usize { |
178 | | - let entries = self.entries.lock().unwrap(); |
179 | | - |
180 | | - entries.0.len() |
181 | | - } |
182 | | - |
183 | | - #[allow(clippy::result_unit_err)] |
184 | | - pub fn reserve(self: &std::sync::Arc<Self>) -> Result<ReusableValue<T>, ()> { |
185 | | - let mut entries = self.entries.lock().unwrap(); |
186 | | - |
187 | | - // Out of capacity. The number of buffers is fixed |
188 | | - if entries.0.len() == entries.0.capacity() { |
189 | | - return Err(()); |
190 | | - } |
191 | | - |
192 | | - let key = entries.0.insert(()); |
193 | | - |
194 | | - let value = &mut entries.1[key] as *mut T; |
195 | | - |
196 | | - Ok(ReusableValue { |
197 | | - reusable: self.clone(), |
198 | | - value, |
199 | | - key, |
200 | | - }) |
201 | | - } |
202 | | -} |
203 | | - |
204 | 106 | #[cold] |
205 | 107 | fn unlikely_abort() { |
206 | 108 | abort(); |
@@ -424,33 +326,6 @@ impl ReusableVec { |
424 | 326 | mod tests { |
425 | 327 | use super::*; |
426 | 328 |
|
427 | | - #[test] |
428 | | - fn test_reusable() { |
429 | | - let reusable = std::sync::Arc::new(Reusable::new(2, || vec![0; 128])); |
430 | | - assert_eq!(reusable.len(), 0); |
431 | | - |
432 | | - let mut buf1 = reusable.reserve().unwrap(); |
433 | | - assert_eq!(reusable.len(), 1); |
434 | | - |
435 | | - let mut buf2 = reusable.reserve().unwrap(); |
436 | | - assert_eq!(reusable.len(), 2); |
437 | | - |
438 | | - // No room |
439 | | - assert!(reusable.reserve().is_err()); |
440 | | - |
441 | | - buf1[..5].copy_from_slice(b"hello"); |
442 | | - buf2[..5].copy_from_slice(b"world"); |
443 | | - |
444 | | - assert_eq!(&buf1[..5], b"hello"); |
445 | | - assert_eq!(&buf2[..5], b"world"); |
446 | | - |
447 | | - mem::drop(buf1); |
448 | | - assert_eq!(reusable.len(), 1); |
449 | | - |
450 | | - mem::drop(buf2); |
451 | | - assert_eq!(reusable.len(), 0); |
452 | | - } |
453 | | - |
454 | 329 | #[test] |
455 | 330 | fn test_rc() { |
456 | 331 | let memory = std::rc::Rc::new(RcMemory::new(2)); |
|
0 commit comments