4 ms·
Regurgitating from an older comment of mine: https://news.ycombinator.com/item?id=13110538 https://news.ycombinator.com/item?id=13110538 ---- The first versio
by LukeShu 4y ago
Regurgitating from an older comment of mine: https://news.ycombinator.com/item?id=13110538 https://news.ycombinator.com/item?id=13110538
----
The first version of UNIX to include `calloc` was Research UNIX V6, and here's how it was implemented: https://github.com/dspinellis/unix-history-repo/blob/Research-V6/usr/source/iolib/calloc.c https://github.com/dspinellis/unix-history-repo/blob/Researc...
calloc(n, s)
{
return(alloc(n*s));
}
There are several interesting things we learn from poking around V6:
- `calloc` originated not on UNIX, but as part of Mike Lesk's "iolib", which was written to make it easier to write C programs portable across PDP 11 UNIX, Honeywell 6000 GCOS, and IBM 370 OS[0]. Presumably the reason calloc is the-way-it-is is hidden in the history of the implementation for GCOS or IBM 370 OS, not UNIX. Unfortunately, I can't seem to track down a copy of Bell Labs "Computing Science Technical Report #31", which seems to be the appropriate reference.
- `calloc` predates `malloc`. As you can see in the above code, there was a `malloc`-like function called just `alloc` (though there were also several other functions named `alloc` that allocated things other than memory). (Ok, fine, since V5 the kernel's internal memory allocator happened to be named `malloc`, but it worked differently[1]).
[0]: https://github.com/dspinellis/unix-history-repo/blob/Research-V6/usr/doc/iolib/iolib https://github.com/dspinellis/unix-history-repo/blob/Researc... (format with `nroff -ms usr/doc/iolib/iolib`)
[1]: https://github.com/dspinellis/unix-history-repo/blob/Research-V6/usr/sys/ken/malloc.c https://github.com/dspinellis/unix-history-repo/blob/Researc...
- aap_ 4y agoI think the `m` in `malloc` and `mfree` in the kernel means `map` as opposed to `memory` in libc. It allocates both core memory and swap memory and uses a `map` struct for this.
- rightbyte 4y agoNice read. Oh I wish the register keyword was actually honored in today's compilers. I am tired of compilers thinking they are smarter than me and having too trust their fancy register allocator algorithm.
- __turbobrew__ 4y agoAt 4:10 in this video there is some interesting history on alloc and malloc, by the original author them self! https://youtu.be/Xe5ffO6Ouwg https://youtu.be/Xe5ffO6Ouwg