use concurrent_queue::ConcurrentQueue;
#[derive(Debug)]
pub struct BufferPool {
cue: ConcurrentQueue<Vec<u8>>,
}
impl BufferPool {
pub fn new() -> Self {
Self {
cue: ConcurrentQueue::bounded(1000),
}
}
pub fn alloc(&self, n: usize) -> Vec<u8> {
if let Ok(mut v) = self.cue.pop() {
v.resize(n, 0);
v
} else {
vec![0; n]
}
}
pub fn free(&self, buf: Vec<u8>) {
if buf.capacity() < 8192 {
let _ = self.cue.push(buf);
}
}
}