The lost malloc(): how early Unix allocated memory
Prompted by an idea for another project, the author went digging through the original Unix sources to see how the mundane task of allocating and managing memory in C was handled back then. The journey starts with the Fourth Edition (V4, November 1973): C had first appeared in the second edition a year earlier but saw little use, by V4 the majority of the kernel was already written in what the author calls 'medieval' C, and a tape containing V4 was found only last year, having been thought completely lost. In the compiler sources (/usr/c/c20.c:814), the routine alloc() is essentially a linear allocator: it aligns the requested size, extends the region via sbrk(2000)[1] when space runs out, and on failure prints 'Out of space' and exits — so callers never check for NULL, and nothing is ever freed, since everything lives as long as the program.
The centerpiece is a malloc.c in Dennis Ritchie's directory, with malloc(mp, size) and mfree(mp, size, aa): both take a first 'map' argument naming the region to work on — coremap for main memory or swapmap for the disk swap area — and mfree() also receives the size of the object being released. John Lions' commentary on the Sixth Edition explains that main memory was handed out in units of 32 words (64 bytes) and swap in units of 256 words (512 bytes); the allocation itself, the author notes, works on an address-ordered list of blocks, taking the first block big enough and merging neighbouring free blocks. The author finds these region-based interfaces far better than the modern malloc()/free() pair: a library's whole memory could be freed by discarding its region, heap corruption bugs would be less severe thanks to partitioning, and third-party allocations could be capped — though the real reason for splitting memory into 'core' and 'swap' was probably hardware constraints. The word 'arena'[2] itself appears for the first time in V6, in /usr/source/s4/alloc.s:17.
Most early code inlines size calculations such as alloc((l+1)*sizeof(a)); meanwhile, in V6's /usr/source/s1/diff1.c:88 — likely the first version of diff(1) ever — the unsort() function allocates memory and then rolls the region pointer back with area = a;, seemingly redundantly since the data is copied right afterwards. V7 (January 1979) already has a #define ALLOC(x) (struct x *) ckalloc(sizeof(struct x)) macro, found only in the Fortran 77 compiler and in make, both worked on by Stuart Feldman. V7 is also the first edition to include assert(), which was already used extensively at the time.
Read the original napcakes.nekoweb.orgDiscussion
Discussion
Commenters enjoyed seeing code from before K&R C[3], with its flipped augmented-assignment operator (=+ instead of +=), and one wondered why such region-based allocators never caught on in web servers, where memory could be freed in bulk after each HTTP request. Another answers that Unix through V7 mostly ran on the PDP-11, where each program got 64KB of data address space, and that there was no mmap()[4] through V8, so program data space stayed fundamentally linear.
Sources
-
napcakes.nekoweb.org
the lost malloc()


