use std::sync::Arc;
use bumpalo::Bump;
use crossbeam_queue::ArrayQueue;
use super::RecycleableBump;
#[derive(Debug, Clone)]
pub struct PoolConfig {
pub pool_capacity: usize,
pub bump_capacity: usize,
}
pub struct BumpPool {
pool: Arc<ArrayQueue<Bump>>,
bump_capacity: usize,
}
impl BumpPool {
pub fn new(pool_capacity: usize, bump_capacity: usize) -> Self {
let pool = ArrayQueue::new(pool_capacity);
for _idx in 0..pool_capacity {
let _ = pool.push(Bump::with_capacity(bump_capacity));
}
Self {
pool: Arc::new(pool),
bump_capacity,
}
}
pub fn capacity(&self) -> usize {
self.pool.capacity()
}
pub fn len(&self) -> usize {
self.pool.len()
}
pub fn is_empty(&self) -> bool {
self.pool.is_empty()
}
}
impl BumpPool {
pub fn take(&self) -> RecycleableBump {
let pool = Arc::downgrade(&self.pool);
let bump = self
.pool
.pop()
.or_else(|| Some(Bump::with_capacity(self.bump_capacity)))
.unwrap();
RecycleableBump {
bump: Some(bump),
pool,
}
}
}