Skip to content

Commit c84afdd

Browse files
committed
memorypool: remove reusablevalue, it is unused
1 parent 76f5d3a commit c84afdd

File tree

1 file changed

+0
-125
lines changed

1 file changed

+0
-125
lines changed

src/core/memorypool.rs

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
*/
1717

1818
use crate::core::minislab::MiniSlab;
19-
use slab::Slab;
2019
use std::cell::{Cell, RefCell};
2120
use std::fmt;
2221
use std::marker::PhantomData;
2322
use std::mem;
2423
use std::ops::{Deref, DerefMut};
2524
use std::process::abort;
2625
use std::ptr::NonNull;
27-
use std::sync::Mutex;
2826

2927
pub struct InsertError<T>(pub T);
3028

@@ -105,102 +103,6 @@ impl<T> Memory<T> {
105103
}
106104
}
107105

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-
204106
#[cold]
205107
fn unlikely_abort() {
206108
abort();
@@ -424,33 +326,6 @@ impl ReusableVec {
424326
mod tests {
425327
use super::*;
426328

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-
454329
#[test]
455330
fn test_rc() {
456331
let memory = std::rc::Rc::new(RcMemory::new(2));

0 commit comments

Comments
 (0)