Memory Allocation Strategies & The Buddy Allocator

Tanenbaum Chapter 3: Classical contiguous allocation algorithms (First Fit, Best Fit, Worst Fit) and kernel binary buddy systems.

Classical Contiguous Memory Allocation Strategies

Before diving into advanced binary buddy allocators and paged virtual memory, operating systems historically managed raw memory as a continuous pool of free and allocated holes. When a process requests memory, the kernel uses one of several classical placement strategies to find a suitable free hole:

Figure: Comparing First Fit vs. Best Fit Placement for a 15 KB Request Alloc (100K) Hole 1 (20K) Alloc (200K) Hole 2 (80K) Alloc (150K) Incoming Request: Allocate 15 KB block First Fit chooses Hole 1 (20K) Fast, leaves 5K splinter Best Fit also considers Hole 2 (80K) Closer fit: Hole 1 (20K) is chosen (min leftover)
The Dual Pillars of Physical Memory Management: Free Lists vs. Used Lists

When a kernel manages raw physical RAM (such as through the binary buddy allocator), it must maintain strict bookkeeping to track which memory is available and which is currently assigned to processes or kernel subsystems. This is accomplished using two core data structures:

1. The Free Lists (free_area[0..MAX_ORDER])

An array of linked lists where each entry groups unallocated blocks of a specific power-of-two size (2order pages). When memory is requested, the allocator scans these lists from smallest sufficient order upward to find free blocks instantly.

2. The Used Tracking (Descriptors & Used Lists)

Tracks active allocations. In Linux and modern kernels, frame metadata (stored in struct page arrays) records ownership flags, reference counts, and order sizes so that when a process frees a pointer, the kernel immediately knows its exact boundaries and buddy address without scanning.

What is a PFN (Page Frame Number)?
Physical DRAM is partitioned by hardware into fixed-sized slots called page frames (commonly 4096 bytes or 212). The PFN is simply the sequential integer index of that frame:
PFN =
Physical Address 4096
= Physical Address >> 12
Physical address 0x0000 is PFN 0, 0x1000 is PFN 1, 0x2000 is PFN 2, and so on. The buddy system performs all pairing directly on this integer index.
Step 1 of 8 State: Idle
1. Initial Setup: The Single Max Block
Physical Memory Range (PFN 0 to 15) 16 Pages × 4 KB = 64 KB Total Pool
PFN 0 (0x0000) PFN 4 (0x4000) PFN 8 (0x8000) PFN 12 (0xC000) PFN 15 (0xF000)
Kernel Free List Array (free_area[0..4])
Active Allocations (Used Tracker)
No active allocations (Used list empty).
Kernel Memory Allocator Trace