编程与软件工程vorpus.org编辑精选
为什么 calloc 这个函数会存在?
Why does calloc exist?
2016 — 2022,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章探讨 calloc 存在的原因:它不仅封装了 malloc+memset,还能检查乘法溢出,并借助操作系统的零页和写时复制机制,让大块内存分配更快、更省内存。作者认为应该直接用 calloc 而不是 malloc+memset。
为什么经久不衰:它把一个看似平凡的 C 标准库函数背后的历史、安全性和操作系统机制讲透,是理解内存分配与写时复制的经典读物。
HN 评论精选
批评作者:依赖隐式写时复制是糟糕的 API 设计,应显式声明保证。
抱歉,这篇文章写得既滑稽又糟糕。如果你依赖写时复制功能,就应该使用明确保证写时复制语义的 API,哪怕需要针对每个平台写 #ifdef、做平台特定处理。作为 API 设计者,你的职责是以最清晰简单的方式暴露这个功能,而不是通过某个随机函数的实现细节隐晦地实现它(然后让没料到写时复制的人承受相应的性能代价)。这就是为什么我们总是得不到好东西。
jblow查看英文原文
Sorry, but this is just goofy and bad. If you depend on copy-on-write functionality, then you need to use an API that is specced to guarantee copy-on-write functionality. If that means you use an #ifdef per platform and do OS-specific stuff, then that is what you do. Anything else is amateur hour. If copy-on-write is a desirable feature, then as the API creator, your job is to expose this functionality in the clearest and simplest way possible, not to hack it in obscurely via the implementation details of some random routine. (And then surprise people who didn't expect copy-on-write with the associated performance penalties.) This is why we can't have nice things.
补充反面场景:追求低延迟时应避免零页/写时复制技巧。
「calloc 的存在让内存分配器和内核暗中合谋,使你的代码更快、更省内存,你应该让它这么做,别用 malloc+memset!」——反过来说,如果你的关键指标是延迟,那 calloc 和操作系统的这些把戏恰恰是你要避免的:应该 memset 缓冲区,有权限的话还要 mlock 防止被换出。当然这都假设缓冲区不是一次性的,最好改用长生命周期的资源。
wyldfire查看英文原文
> So basically, calloc exists because it lets the memory allocator and kernel engage in a sneaky conspiracy to make your code faster and use less memory. You should let it! Don't use malloc+memset! On the flip side, if your critical metric is latency then these tricks of calloc's and the OS's are exactly what you try to avoid. memset() the buffer, and if you have the privileges you should mlock() it to prevent it from being paged out. Of course, this all presumes that it's not an ephemeral buffer to begin with. Best to change your design to leverage a long-lived resource if possible.
考据历史:calloc 起源于跨平台移植库 iolib,且早于 malloc。
第一个包含 calloc 的 UNIX 版本是 Research UNIX V6,实现只有一行:return(alloc(n*s))。有趣的是:calloc 并非起源于 UNIX,而是 Mike Lesk 的 iolib 的一部分,该库为了让 C 程序在 PDP 11 UNIX、Honeywell 6000 GCOS 和 IBM 370 OS 之间可移植;calloc 的真正由来可能藏在 GCOS 或 IBM 370 的实现历史里。另外 calloc 比 malloc 出现得更早,当时只有类似 malloc 的函数叫 alloc。
LukeShu查看英文原文
Regurgitating from an older comment of mine: 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 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 (format with `nroff -ms usr/doc/iolib/iolib`) [1]: https://github.com/dspinellis/unix-history-repo/blob/Research-V6/usr/sys/ken/malloc.c
指出文章描述的溢出检查和跳过清零是现代实现,历史上的 calloc 只是 malloc+bzero。
「calloc 会检查乘法溢出、且不需要时不清零内存」也许今天是真的,但这是现代的修正主义。历史上 calloc 两者都没做。比如 1993 年左右的 4.4 BSD 实现:size *= num; if (p = malloc(size)) bzero(p, size); 就是简单的 malloc 加 bzero。
ksherlock查看英文原文
It may be true today that calloc checks for multiplication overflow and doesn't zero memory if it doesn't need to, that's modern revisionism. Historically, calloc did not do either of those things. eg, from the 4.4 BSD, circa 1993: https://minnie.tuhs.org/cgi-bin/utree.pl?file=4.4BSD/usr/src/lib/libc/stdlib/calloc.c void * calloc(num, size) size_t num; register size_t size; { register void *p; size *= num; if (p = malloc(size)) bzero(p, size); return(p); }
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选