The lost malloc() napcakes.nekoweb.org · 36 points · 5 comments · HN thread TL;D...
HN Best 200 @best
The lost malloc() napcakes.nekoweb.org · 36 points · 5 comments · HN thread
TL;DR A personal write-up of early Unix memory allocation, based on reading V4 through V9 source code and documents. It traces linear sbrk-based allocators, the coremap/swapmap resource-map malloc/mfree of V4-V6, the switch to traditional malloc/free by V8, new()-style macros inherited from Simula and C++, and the author's speculation about why arenas and garbage collection never became standard.
- V4 (1973) kernel code was mostly C, and the C object code improver used a linear allocator that extended the heap with sbrk(2000) and exited on failure, so callers never checked for NULL and never freed memory.
- V4's malloc/mfree took an explicit map argument (coremap or swapmap) and tracked free blocks in address order with first-fit and coalescing, as documented in the V6 commentary by John Lions.
- V6 uses the term 'arena' in alloc.s, and the V6 diff(1) code shows a linear alloc that rolls back its region.
- V7 has an ALLOC(x) macro, and V8 has several new(type) macros, some of them zero-initialising, that predate C++ new; the author finds no authors for them.
- V7 added galloc/gfree with garbage collection, and Rob Pike's Sam editor in V9 has gcnew, so the author argues the Unix developers wanted GC rather than arenas.
- The author offers three speculative reasons arenas did not take hold: most programs had simple lifetimes, memory was scarce, and manual memory management was tiresome.
Discussion The comment thread is short, with only a few comments. It is mostly admiring: one commenter, a former college student who learned C 15 years ago, is amazed that ideas like the bump allocator were tried decades earlier and asks how the senior programmers managed it. A reply credits a more theoretical culture, simpler systems that were easier to experiment with, and programmers who were closer to the hardware. Another commenter describes C as having simple syntax but complicated semantics. A separate remark notes that a story with only four points rarely reaches the front page.
- Early Unix engineers were unusually deep in the hardware and theory (most): The thread's main explanation is that in the 1970s computer science was more theoretical, systems were smaller and easier to experiment with, and programmers knew the machine internals better than most do today.
- C is simple to learn but hard to master (some): One commenter borrows a gaming phrase to describe C as easy to learn and hard to master, which fits the author's point that C could do a lot with little.
- C syntax is simple but its semantics are complicated (a few): One commenter finds C's surface syntax simple, though type declarations are painful, and says its semantics are very complicated.