An allocator hands out runs of memory and takes them back
Start with what an allocator does, on an arena small enough to draw.
Predict
An arena of 10 units. alloc(3), alloc(4) and alloc(2) all succeed. Then alloc(2) again: what comes back?
An arena of 10 units
0123456789
alloc(3) returns 0
AAA
alloc(4) returns 3
AAABBBB
alloc(2) returns 7
AAABBBBCC
alloc(2) returns None
AAABBBBCC
Only unit 9 is free.
free(3) returns 4
AAACC
B's 4 units are free again.
a live block, by lettera free unit
An allocator owns one long stretch of memory, the arena, and hands it out in pieces. Here the arena has 10 units, at addresses 0 to 9.
alloc(n) hands out a block: n units in a row, known by the address it starts at. It returns that start, or None when there's no room. free(addr) takes the block back and returns its size, and its units can be handed out again.
In C these are malloc and free. Python's own small-object allocator works from arenas too, and its docs give their size: "It uses memory mappings called “arenas” with a fixed size of either 256 KiB on 32-bit platforms or 1 MiB on 64-bit platforms."
The practice problem puts you inside a model server. It takes one arena of GPU memory when it starts and carves it up itself, for weights, activations and each request's cache. An address there is an offset into the arena, from 0 to size - 1, and a unit is a byte.
Each call makes a promise, and says what it does when it can't keep it:
Call
Promise
When it can't
alloc(n)
returns a start a such that units a to a + n - 1 are in no live block, and marks them used
no n free units in a row: return None
free(a)
takes back the block that starts at a, so its units can be handed out again
a isn't the start of a live block: raise ValueError
A block is live from the alloc that returned it until the free that takes it back. Two rules, the invariants, hold after every call, and the tests you'll write check both:
No two live blocks share a unit.
Every unit is either free or in exactly one live block.
Enough free units in total can still leave no run big enough
Pick an answer above to see the rest
Predict
An arena of 7: A holds units 0 to 2, B unit 3, C units 4 to 6. Free A and C. Can alloc(4) succeed?
An arena of 7 units
0123456
alloc(3) returns 0
AAA
alloc(1) returns 3
AAAB
alloc(3) returns 4
AAABCCC
free(0) returns 3
BCCC
free(4) returns 3
B
6 units free.
alloc(4) returns None
B
No 4 in a row.
Six units are free at the end. But they sit in two runs of 3, with the live block at unit 3 between them, so a request for 4 has to fail.
Free memory split into pieces too small to use is called external fragmentation. Much of this lesson is about keeping it down.
lowest_fit(units, n) reads an arena drawn as a string, the way the figures draw it: . is a free unit and a letter is a used one. Write it to return the start that alloc(n) would hand out, or None.
Pick an answer above to see the rest
Instructions
1
Write lowest_fit(units, n): units has one character per unit, . for a free unit and a letter for a used one. Return the lowest address where n free units in a row begin, or None when there is no such run.