use std::cell::Cell;
thread_local! {
static USED: Cell<usize> = const { Cell::new(0) };
static LIMIT: Cell<usize> = const { Cell::new(usize::MAX) };
}
pub fn bop_memory_init(limit: usize) {
USED.set(0);
LIMIT.set(limit);
}
pub fn bop_alloc(bytes: usize) {
USED.with(|u| u.set(u.get().saturating_add(bytes)));
}
pub fn bop_dealloc(bytes: usize) {
USED.with(|u| u.set(u.get().saturating_sub(bytes)));
}
pub fn bop_memory_exceeded() -> bool {
USED.with(|u| LIMIT.with(|l| u.get() > l.get()))
}
pub fn bop_would_exceed(bytes: usize) -> bool {
USED.with(|u| LIMIT.with(|l| u.get().saturating_add(bytes) > l.get()))
}