Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 addresses a through b - 1. [4, 7) is the three bytes at addresses 4, 5, and 6; its length is 7 - 4 = 3.

What to implement

  • process_commands(arena_size, commands) returns one line per ALLOC, in order. Each command is one line.
  • arena_size is N. The allocator manages addresses 0 through N - 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.
CommandMeaningPrints
ALLOC sizeReserve size contiguous bytesthe block's start address, or FAIL
FREE addrRelease the allocation that starts at addr (Part 2)nothing

Part 1: First-fit allocation

For ALLOC size:

  1. Scan the free blocks in ascending address order.
  2. Choose the first block whose length is at least size.
  3. Reserve size bytes 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.
  4. 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

  • N is between 1 and 109.
  • Every size is between 1 and N.

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.

  1. The input guarantees that addr is live: an earlier ALLOC returned it, and no FREE has released it since.
  2. Like free(p), the command carries only the address, so the allocator must remember each allocation's size itself.
  3. The released range becomes a free block again, which a later first-fit ALLOC may reuse.
  4. FREE prints 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.

  1. 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.
  2. Later ALLOC commands see the single combined block.
  3. After every command, no two free blocks touch.

Tests 7–9 cover this part.

Example
Input
6
4
ALLOC 4
ALLOC 3
ALLOC 2
ALLOC 1
Output
0
FAIL
4
FAIL
Explanation

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.