18/34
Part 5 of 7 · Alignment and growthLearn and do

The units skipped to reach an aligned start stay free

Rounding up skips some units. Those units still belong on the free list.

alloc(16, align=16) on the free run (3, 64)
016324864
memory

The block starts at 16. The units skipped to reach it, (3, 16), stay free, and so does (32, 64).

On an arena of 64 units with a block at 0 to 2, the free list is [(3, 64)]. A request for 16 units aligned to 16 rounds 3 up to 16. The block is 16 to 31, and the free list becomes [(3, 16), (32, 64)].

Taking an aligned block out of the middle of a run can leave two pieces: the units skipped to reach at, and the tail after the block. Those 13 skipped units are still free, and the next alloc(13) returns 3.

Drop the gap and they're gone for good. No live block holds them, so nothing ever frees them, and they never reach the free list. That's a leak.

take_aligned(free, n, align) is take with alignment: it rounds each run's start up and keeps the gap before the block as a free run. Fill in the two marked lines in the editor.