Q277FreeSystemVerilog
Compact live allocations
Interview prompt
Question
Compact live allocations toward the allocator base, return an old-to-new relocation map, and rebuild one consolidated free interval.
Starting point
Question code
function void compact(
ref int unsigned relocated_addr[int unsigned]
);Reviewed example
Trace one case
Input
allocator=[0,99]; live blocks=(base=10,size=20),(base=50,size=10)Expected output
relocations={10->0,50->20}; consolidated free range=[30,99]Packing in allocation order keeps blocks nonoverlapping and advances the final free cursor to 30.
What to cover
Requirements
- Track live allocation bases and sizes.
- Preserve allocation order and prevent overlap during conceptual movement.
- Return a relocation entry for every moved allocation.
- Rebuild the free list from the final packed cursor to the allocator limit.

