euv-core 0.57.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Selects an unpinned `Ready` victim slot in O(1) amortized time using a persistent second-chance Clock-LRU hand.
///
/// # Arguments
///
/// - `&mut [Option<AssetSlotEntry>]` - Mutable active asset slot table.
/// - `&mut usize` - Persistent circular Clock-LRU hand cursor updated in place.
/// - `bool` - Whether candidate must release CPU RAM bytes.
/// - `bool` - Whether candidate must release GPU VRAM bytes.
///
/// # Returns
///
/// - `Option<usize>` - Selected victim slot index, or `None` if no unpinned matching slot exists.
#[must_use]
pub(crate) fn select_clock_lru_eviction_slot(
    entries: &mut [Option<AssetSlotEntry>],
    clock_hand: &mut usize,
    need_ram: bool,
    need_vram: bool,
) -> Option<usize> {
    let len: usize = entries.len();
    if len == 0 {
        return None;
    }
    if *clock_hand >= len {
        *clock_hand = 0;
    }
    let max_steps: usize = len.saturating_mul(2);
    for _ in 0..max_steps {
        let slot_index: usize = *clock_hand;
        *clock_hand = (slot_index + 1) % len;
        let Some(entry) = entries[slot_index].as_mut() else {
            continue;
        };
        if entry.get_state() != AssetLoadState::Ready || entry.get_pinned() {
            continue;
        }
        let frees_ram: bool = need_ram && entry.get_ram_bytes() > 0;
        let frees_vram: bool = need_vram && entry.get_vram_bytes() > 0;
        if !frees_ram && !frees_vram {
            continue;
        }
        if !entry.get_clock_referenced() {
            return Some(slot_index);
        }
        entry.set_clock_referenced(false);
    }
    None
}

/// Releases all `ResourceAccountant` records associated with `entry`, optionally deferring GPU VRAM handles to a fence.
///
/// # Arguments
///
/// - `&AssetSlotEntry` - Asset slot entry being released or evicted.
/// - `&mut ResourceAccountant` - Resource accountant receiving release notifications.
/// - `bool` - When `true`, leaves the GPU VRAM handle alive for `mark_owner_destroyed` fence tracking.
pub(crate) fn release_entry_accounting(
    entry: &AssetSlotEntry,
    accountant: &mut ResourceAccountant,
    defer_vram_for_fence: bool,
) {
    let task_handle: u64 = entry.get_task_account_handle();
    if task_handle != 0 {
        let _: bool = accountant.release_resource(TrackedResourceKind::AsyncTask, task_handle);
    }
    let ram_handle: u64 = entry.get_ram_account_handle();
    if ram_handle != 0 {
        let _: bool = accountant.release_resource(TrackedResourceKind::CacheAllocation, ram_handle);
    }
    let vram_handle: u64 = entry.get_vram_account_handle();
    if vram_handle != 0 && !defer_vram_for_fence {
        let _: bool = accountant.release_resource(TrackedResourceKind::GpuTexture, vram_handle);
    }
}