//! Utilities for handling reserved memory regions

use libk_mm_interface::address::PhysicalAddress;
use libk_util::{sync::IrqSafeSpinlock, StaticVector};

use crate::phys::PhysicalMemoryRegion;

static RESERVED_MEMORY: IrqSafeSpinlock<StaticVector<PhysicalMemoryRegion, 8>> =
    IrqSafeSpinlock::new(StaticVector::new());

/// Marks a region of physical memory as reserved.
///
/// # Note
///
/// This call only has any effect **before** physical memory manager is initialized.
pub fn reserve_region(_reason: &str, region: PhysicalMemoryRegion) {
    RESERVED_MEMORY.lock().push(region);
}

/// Returns `true` if `addr` refers to any reserved memory region
pub fn is_reserved(addr: PhysicalAddress) -> bool {
    for region in RESERVED_MEMORY.lock().iter() {
        if region.range().contains(&addr) {
            return true;
        }
    }
    false
}
