Downgrade MULTI sentinel in rev_host_clear

CH
6mo ago
+75-4

As stated in https://github.com/sysprog21/kbox/issues/38#issuecomment-4204194495, MULTI sentinel in host_to_vfd[] is never recover to vfd once it is set. This PR fixes it by adding a per-host-FD reference count host_fd_refs[] that tracks how many VFDs alias each host FD:

rev_host_set increments the count before writing the slot.
rev_host_clear decrements it on close. When the count falls to 1, it searches for the surviving VFD and writes it directly into host_to_vfd, downgrading MULTI back to a valid mapping. When the count reaches 0, the slot is cleared to KBOX_HOST_VFD_NONE as before.

A new unit test test-fd-table-refcount.c covers the single-holder, two-sharer, and downgrade cases.

This PR resolves code gap 1 and test gap 2 in #38


Summary by cubic

Fixes stale MULTI entries in the reverse host→VFD map by tracking per-host FD refs and downgrading back to a direct mapping when one VFD remains. Restores correct reverse lookups after shared FDs close.

  • Bug Fixes
    • Add host_fd_refs[]; initialize in kbox_fd_table_init.
    • rev_host_set increments; rev_host_clear decrements with a guard. On refs == 0, set to KBOX_HOST_VFD_NONE. On refs == 1, find the surviving VFD and write it back (hide the closing VFD if the search hits it, then retry).
    • Add unit test test-fd-table-refcount.c; wire into mk/tests.mk and test-runner.c.