Round 3, 16 and 17 up to a multiple of 16. What do you get?
Pick one to see what it prints.
Hardware and libraries often need a block to start at an address that's a multiple of some power of two. glibc's malloc always returns a multiple of 8, or 16 on 64-bit systems. aligned_alloc takes a bigger multiple, and it must be a power of two. The practice problem uses the same rule.
In each run, the lowest aligned address is the run's start rounded up to a multiple of align. -(-start // align) * align rounds up with integers only: // rounds down, and negating before and after turns that into rounding up.
The block fits in this run if at + n <= end, where at is the rounded start. If it doesn't, no later aligned address in the same run fits either, since each one ends later still. So move on to the next run.
Because align is a power of two, (start + align - 1) & ~(align - 1) gives the same number with bit operations. The two agreed on every start from 0 to 4,999 for alignments of 1, 2, 4, 8, 16, 256 and 4,096. Either is fine. Say which one you're using and why it rounds up.
round_up(start, align) and fits(run, n, align) are the two checks that first fit makes in each run when a block must be aligned. Write both with integers only.
Pick an answer above to see the rest
Instructions
1
Write round_up(start, align): the lowest multiple of align at or above start, with integers only.
2
Write fits(run, n, align): True when a block of n units that starts at the run's start, rounded up, still ends inside the half-open run.