hn.today

The lost malloc()

napcakes.nekoweb.org57 points17 comments
Screenshot of The lost malloc()

A tour of early Unix source code traces how memory allocation evolved in the 1970s, when much of the operating system was already written in C. A compiler utility in Unix V4 used a simple linear allocator: it rounded requests to an even size, handed out successive chunks, and extended the heap with sbrk() when needed. Since the program’s allocations lived until it exited, there was no reason to free them. The account contrasts this with Unix’s kernel malloc() and mfree(), which accepted an explicit resource map and allocation size. Separate maps managed main memory and disk swap space in units suited to each resource; the allocator used first-fit search and merged adjacent free blocks.

The explicit-region interface suggests practical benefits that remain relevant: libraries could be confined to designated memory, their allocations discarded together, and their consumption limited. The discussion also finds linear allocation in the early diff utility, including a rollback-like pattern that reuses memory after processing. It connects these conventions to C++’s new operator, which drew on Simula’s terminology and combined allocation with object initialization, while noting that Unix code also used macros to simplify typed allocation. The central puzzle is why such approaches did not become more common: many programs instead called malloc() directly and repeated sizing and error checks. The historical examples show that arenas, region-based allocation, and convenient allocation syntax were present early, even if their broader advantages were not widely adopted.

Read on napcakes.nekoweb.org17 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.