Coalescing Memory Allocator
Prerequisites: malloc Internals and the Hot Path, pmr, Allocators, and Object Pools
The malloc article's allocator splits a larger chunk when no bin holds one of the right size, and files freed chunks away for reuse. Build that machinery in miniature, over one contiguous arena of N bytes.
- Arena: one big region that allocations are carved out of, as in Module 3. Here each block is freed on its own, not all at once.
- Address: a byte offset from the start of the arena.
- Block
[a, b): the half-open range of addressesathroughb - 1.[4, 7)is the three bytes at addresses 4, 5, and 6; its length is7 - 4 = 3.
What to implement
process_commands(arena_size, commands)returns one line perALLOC, in order. Each command is one line.arena_sizeisN. The allocator manages addresses0throughN - 1, and the arena starts as one free block,[0, N).- On stdin, the first line is
N, the second is the number of commands, and each remaining line is one command. - There are three parts, each building on the previous one. One solution must pass all nine tests.
| Command | Meaning | Prints |
|---|---|---|
ALLOC size | Reserve size contiguous bytes | the block's start address, or FAIL |
FREE addr | Release the allocation that starts at addr (Part 2) | nothing |
Part 1: First-fit allocation
For ALLOC size:
- Scan the free blocks in ascending address order.
- Choose the first block whose length is at least
size. - Reserve
sizebytes from the start of that block and print its start address. The rest of the block, if any, stays free: the request splits the block in two. - If no single free block is long enough, print
FAIL. A failed allocation changes nothing.
This is first fit by address: the lowest-addressed block that fits wins, even when a later block would fit more tightly. It is not best fit, which would pick the smallest block that fits.
Limits
Nis between 1 and 109.- Every
sizeis between 1 andN.
Tests 1–3 cover this part. They contain only ALLOC commands.
Part 2: Reuse freed blocks
FREE addr releases the complete allocation that began at addr.
- The input guarantees that
addris live: an earlierALLOCreturned it, and noFREEhas released it since. - Like
free(p), the command carries only the address, so the allocator must remember each allocation's size itself. - The released range becomes a free block again, which a later first-fit
ALLOCmay reuse. FREEprints nothing.
Tests 4–6 cover this part. In them, no allocation depends on two touching free ranges being combined.
Part 3: Coalesce adjacent space
Fragmentation still blocks large requests. Free ranges [0, 3) and [3, 7) touch but are still two blocks, so an ALLOC 7 fails even though all seven bytes of [0, 7) are free.
- Whenever a range is freed, coalesce it immediately: merge it with a free block that ends exactly where it starts, a free block that starts exactly where it ends, or both.
- Later
ALLOCcommands see the single combined block. - After every command, no two free blocks touch.
Tests 7–9 cover this part.
6 4 ALLOC 4 ALLOC 3 ALLOC 2 ALLOC 1
0 FAIL 4 FAIL
The arena is [0, 6). ALLOC 4 takes [0, 4) and prints 0, which leaves [4, 6) free.
ALLOC 3 needs three bytes, but the only free block has two, so it prints FAIL and the arena stays as it was.
ALLOC 2 then fits exactly in [4, 6) and prints 4. Nothing is free after that, so ALLOC 1 fails.