Skip to main content

filter_ids

Function filter_ids 

Source
fn filter_ids(ids: &mut [u32; 32], keep: impl Fn(u32) -> bool)
Expand description

Drop the hidden GPUs from a gpuIds[] array in place — but only ever a strict subset, so the filter can never accidentally blank out every GPU and strand the container with none. Ids that pass keep are compacted to the front and the rest back-filled with INVALID_GPU_ID.

  1. Scan the populated prefix: iterate until the first INVALID_GPU_ID, counting total live ids and collecting the kept ones into a scratch array (nkept).
  2. Commit only a strict subset (0 < nkept < total): copy the kept prefix back over ids and set every trailing slot to INVALID_GPU_ID.
  3. Fail-safe: if none survive or all survive, leave the array untouched — never blank out every GPU, and never do redundant work when nothing was dropped.

The keep predicate is injected so this stays free of /proc and /dev access and is unit-testable in isolation.