use std::{iter, sync::Mutex};
use oxc_data_structures::stack::Stack;
use crate::Allocator;
pub struct StandardAllocatorPool {
allocators: Mutex<Stack<Allocator>>,
}
impl StandardAllocatorPool {
pub fn new(thread_count: usize) -> StandardAllocatorPool {
let allocators = iter::repeat_with(Allocator::new).take(thread_count).collect();
StandardAllocatorPool { allocators: Mutex::new(allocators) }
}
pub fn get(&self) -> Allocator {
let allocator = {
let mut allocators = self.allocators.lock().unwrap();
allocators.pop()
};
allocator.unwrap_or_else(Allocator::new)
}
pub(super) unsafe fn add(&self, mut allocator: Allocator) {
allocator.reset();
let mut allocators = self.allocators.lock().unwrap();
allocators.push(allocator);
}
}