use core::sync::atomic::{AtomicIsize, Ordering};
use ostd::{cpu::all_cpus, cpu_local, trap::DisabledLocalIrqGuard};
cpu_local! {
static FREE_SIZE: AtomicIsize = AtomicIsize::new(0);
}
pub(super) fn add_free_size(irq_guard: &DisabledLocalIrqGuard, size: usize) {
FREE_SIZE
.get_with(irq_guard)
.fetch_add(size as isize, Ordering::Relaxed);
}
pub(super) fn sub_free_size(irq_guard: &DisabledLocalIrqGuard, size: usize) {
FREE_SIZE
.get_with(irq_guard)
.fetch_sub(size as isize, Ordering::Relaxed);
}
pub(super) fn read_total_free_size() -> usize {
let mut total: isize = 0;
for cpu in all_cpus() {
total = total.wrapping_add(FREE_SIZE.get_on_cpu(cpu).load(Ordering::Relaxed));
}
if total < 0 {
0
} else {
total as usize
}
}