use alloc::vec::Vec;
use bumpalo::Bump;
use cubecl_macros_internal::TypeHash;
#[derive(Default, Debug, TypeHash)]
pub struct DropBump {
bump: Bump,
drop_thunks: Vec<DropThunk>,
}
#[derive(Debug, TypeHash)]
struct DropThunk {
func: fn(*mut ()),
ptr: *mut (),
}
impl DropBump {
pub fn new() -> Self {
Default::default()
}
pub fn reset(&mut self) {
for drop in self.drop_thunks.drain(..).rev() {
(drop.func)(drop.ptr)
}
self.bump.reset();
}
pub fn alloc<T>(&mut self, val: T) -> &mut T {
let ret = self.bump.alloc(val);
if core::mem::needs_drop::<T>() {
self.drop_thunks.push(DropThunk {
func: drop_glue::<T>,
ptr: ret as *mut T as *mut (),
});
}
ret
}
}
fn drop_glue<T>(ptr: *mut ()) {
unsafe { core::ptr::drop_in_place::<T>(ptr as *mut T) };
}