use std::cell::RefCell;
use std::collections::HashSet;
thread_local! {
static ALLOCATED_POINTERS: RefCell<HashSet<*const ()>> = RefCell::new(HashSet::new());
}
pub fn mark_pointer_allocated<T>(ptr: *const T) {
ALLOCATED_POINTERS.with(move |ptrs| {
assert!(ptrs.borrow_mut().insert(ptr as *const ()));
})
}
pub fn mark_pointer_deleted<T>(ptr: *const T) {
ALLOCATED_POINTERS.with(move |ptrs| {
assert!(
ptrs.borrow_mut().remove(&(ptr as *const ())),
"DOUBLE FREE: deleted pointer {:?} that is not allocated",
ptr
);
})
}
pub fn count_allocated_pointers() -> usize {
ALLOCATED_POINTERS.with(|ptrs| ptrs.borrow().len())
}
pub fn is_live<T>(ptr: *const T) -> bool {
ALLOCATED_POINTERS.with(move |ptrs| ptrs.borrow().contains(&(ptr as *const ())))
}