Q290FreeSystemVerilog
Implement best-fit allocation
Interview prompt
Question
Replace first-fit with best-fit: choose the smallest free range that can satisfy the request. Explain the one-line comparison change for worst-fit.

Starting point
Question code
function bit alloc_best_fit(
int unsigned size,
output int unsigned addr
);Reviewed example
Trace one case
Input
free capacities=[64,24,40]; request size=20Expected output
select the 24-byte range; leave a 4-byte remainderAll ranges are sufficient, but 24 is the smallest capacity and therefore the best fit.
What to cover
Requirements
- Inspect all sufficient ranges.
- Select the minimum sufficient capacity.
- Preserve exact-fit and partial-fit behavior.
- Avoid signed sentinels for unsigned capacities.

