use alloc::rc::Rc;
use cubecl_macros_internal::TypeHash;
use portable_atomic::{AtomicU32, Ordering};
use super::{Type, Value};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Default, TypeHash)]
pub struct Allocator {
next_id: Rc<AtomicU32>,
}
impl PartialEq for Allocator {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.next_id, &other.next_id)
}
}
impl Eq for Allocator {}
impl Allocator {
pub fn clone_deep(&self) -> Self {
Allocator {
next_id: Rc::new(AtomicU32::new(self.next_id.load(Ordering::SeqCst))),
}
}
pub fn create_value(&self, ty: Type) -> Value {
let id = self.new_local_index();
Value::new(id, ty)
}
pub fn new_local_index(&self) -> u32 {
self.next_id.fetch_add(1, Ordering::Release) + 1
}
pub fn current_local_index(&self) -> u32 {
self.next_id.load(Ordering::SeqCst)
}
}