Memory Allocator

malloc and free over one address range

Design malloc and free over one contiguous range of addresses: find room, split what is too big, and merge freed neighbors so the range does not crumble into slivers.

Overview

An allocator owns one range of addresses, say 0 to 64, and hands out pieces of it on request. malloc(size) returns the start of a piece at least size bytes long that nobody else holds; free(address) gives the piece back. Under C's malloc and free, and under every language runtime, something like this is running.

The problem is hard for a reason that is easy to state: pieces come back in a different order than they went out. After a while the range is a patchwork of used and free pieces, and a request can fail even when the free bytes add up to more than it needs. Every design choice, the fit rule, splitting, merging, alignment, is about keeping that patchwork usable.

A strong answer keeps the blocks in one list in address order, splits a free block on malloc, merges a freed block with free neighbors on free, and names the two kinds of fragmentation with an example of each. Interviewers then push on the fit rule, on making free O(1), and on threads.

Scoping the problem

The scope comes down to six questions. Ask each one; the answer assumed in this chapter follows it.

  • Is this a model of the algorithm, or real memory? Treat it as a model, with addresses as integers. A model can keep block records in Python objects, while a real allocator stores them inside the range.
  • What is the alignment? 8 bytes, configurable. Requests are rounded up to it, which is one source of waste.
  • What happens when nothing fits? The choice is to return a sentinel or raise. Here malloc returns None, and free of a bad address raises.
  • Which fit rule? It changes which hole a request takes, so offer first fit and best fit as swappable policies.
  • Do we merge freed neighbors at once? Yes, on every free. This decides whether free bytes stay usable.
  • Many threads? Every change to the block list must be atomic, so the allocator takes one lock.

Functional requirements

  • malloc(size) returns the start address of a free run of at least size bytes, rounded up to the alignment, or None when no hole is big enough
  • free(address) returns a block; a freed block merges with any free neighbor on either side at once
  • A hole bigger than the request is split; the leftover stays free
  • free of an address that is not the start of a live block, including a second free of the same block, raises
  • The fit rule is a policy object; first fit and best fit are provided
  • layout(), free_bytes() and largest_free() expose the state for tests and for explaining fragmentation

Non-functional requirements

  • malloc is O(number of blocks) for the scan; free is O(1) thanks to the address dict and the doubly linked list
  • Every change to the block list happens under one lock
  • Memory overhead is one Block record per live or free run; the list is always sorted by address
  • The model never has two adjacent free blocks after a free, so the largest hole is as large as it can be

Interview questions

  • Is first fit or best fit better? The scan for best fit is longer.
  • Many objects have the same size. Can you do better than a general allocator for them?
  • Why merge on every free? You could wait until a malloc fails.
  • Your free() uses a dict. A real allocator has no dict. How does free(p) find the block?
  • Two threads call malloc at the same time. What breaks, and what do production allocators do about it?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio