8/34
Part 2 of 7 · The free listLearn and do
The block takes the front of its run, and the rest stays free in place
The block takes the front of the run it came from, and the rest of the run stays free. In the figure, alloc(2) turned (3, 7) into (5, 7). Three requests, starting from [(2, 4), (7, 15), (17, 20)]:
| Request | Runs before | Run chosen | Block gets units | Runs after |
|---|---|---|---|---|
| 3 | [(2, 4), (7, 15), (17, 20)] | (7, 15) | 7 to 9 | [(2, 4), (10, 15), (17, 20)] |
| 2 | [(2, 4), (10, 15), (17, 20)] | (2, 4) | 2 to 3 | [(10, 15), (17, 20)] |
| 6 | [(10, 15), (17, 20)] | none | unchanged, and alloc returns None |
- The first row skips
(2, 4), which holds 2 units. - The second uses
(2, 4)up exactly, so it leaves the list. An empty(4, 4)left behind would hold nothing, and it would get in the way. Freeing the used units 4 to 6 later would put(4, 7)right next to it instead of merging with anything. - The third has 8 units free in total and no run of 6: the first failure again.
Taking the front is what makes first fit hand out the lowest address. It also keeps the list sorted without moving anything: the leftover (start + n, end) sits exactly where the old run did.
First fit is O(k) for k runs, whatever the arena's size
Walking to the last of k runs, best of 5:
First fit walked to the last of 1,000 runs in 0.027 ms. About how long for 10,000 runs?
Pick an answer above to see the rest