Q262FreeSystemVerilog
Allocate on a power-of-two boundary
Interview prompt
Question
Return a block whose starting address is aligned to a requested power-of-two boundary while preserving every unused fragment.
Starting point
Question code
function bit alloc_aligned(
int unsigned size,
int unsigned align,
output int unsigned addr
);Reviewed example
Trace one case
Input
free=[[0x1003,0x10FF]]; size=0x20; alignment=0x40Expected output
base=0x1040; remaining=[[0x1003,0x103F],[0x1060,0x10FF]]Rounding 0x1003 upward selects the first 64-byte boundary, and both unused fragments remain available.
What to cover
Requirements
- Reject zero or non-power-of-two alignment.
- Round each candidate range start upward without signed arithmetic.
- Verify that the complete request fits after alignment.
- Preserve leading and trailing free fragments.

