Β§17.1Free-Space Management: The Problem, and the Ground Rules

Part I OSTEP pp. 167–168 Β· ~4 min read

A small detour β€” and a debt to pay. The segmentation chapter ended with physical memory full of odd-sized holes; this chapter is about living with them. The problem is fundamental to any memory manager, whether a malloc library managing a process’s heap or the OS itself managing physical memory under segmentation.

When the managed space is divided into fixed-sized units, the job is easy: keep a list of them; a client wants one, return the first entry. (Hold that thought β€” it’s called paging, and it’s next chapter.) The difficulty β€” and the fun β€” arrives with variable-sized units, where external fragmentation lurks:

The chapter’s whole problem in 30 bytes: 20 free, and a 15-byte request still fails
free β€” 10 bytes β“˜0used β“˜10free β€” 10 bytes β“˜2030

click a region marked β“˜ for details

The free list for this little heap has two entries β€” and no answer for a 15-byte request:

The free list: two 10-byte chunks, no 15-byte answer

head→addr:0
len:10
β†’addr:20
len:10
β†’NULL

The Crux: How To Manage Free Space

How should free space be managed, when satisfying variable-sized requests? What strategies can be used to minimize fragmentation? What are the time and space overheads of alternate approaches?

17.1 Assumptions

The chapter draws on the great history of user-level allocators (Wilson et al.’s survey β€” nearly 80 pages, so you really have to be interested). The ground rules:

The ground rules for this chapter β€” each one earns its keep
what we assume
The interfacevoid *malloc(size_t size) and void free(void *ptr)
The arenaa region historically called the heap; its free chunks tracked by a free list
The enemyexternal fragmentation (mostly)
No relocationonce handed out, memory cannot be moved until freed
One fixed regionthe allocator manages a single contiguous byte range of fixed size
Dotted-underlined cells have explanations β€” click one.

Two of these deserve a second look. The interface’s missing size parameter means the library must remember, for every outstanding chunk, how big it is β€” that bookkeeping is where the mechanisms section begins. And no relocation means compaction β€” chapter 16’s cure β€” is unavailable here: compacting works for an OS moving whole segments (one base register redirects everything) but not for a library whose clients hold raw C pointers. The allocator must fight fragmentation without ever moving a granted byte.

Check yourself

1.Why is free-space management trivial with fixed-sized units, but a whole chapter with variable-sized ones?

2.free(void *ptr) takes no size parameter. What does that force upon the allocation library?

3.Chapter 16 offered compaction as fragmentation's cure. Why is that off the table for a malloc library?

4.The 30-byte heap: bytes 0–10 free, 10–20 in use, 20–30 free. A request for 15 bytes arrives.

4 questions