通过对 Unix 早期版本(从 V4 到 V8)源代码的深度挖掘,一篇技术分析文章重构了 malloc() 等内存分配函数的演进脉络 1。在 1973 年 11 月发布的 Unix V4 中,alloc() 函数采用了一种线性分配器机制,其显著特征是在分配失败时直接终止程序运行,从而免除了调用者检查返回指针是否为 NULL 的义务 1。Dennis Ritchie 目录下的 malloc.c 文件则展示了一种更为复杂的接口设计,该设计包含一个名为 mp(map pointer)的参数,旨在实现对 coremap(核心内存映射)和 swapmap(交换区映射)的显式内存区域划分 1。
随着时间推移,1975 年春季发布的 Unix V6 引入了“arena”这一术语,并明确区分了不同存储介质的分配单位:核心内存以 64 字节为单位,而磁盘交换区则以 512 字节为单位 1。到了 1985 年 2 月发布的 Unix V8,代码中出现了名为 tryalloc 的宏,其行为类似于 C++ 中的 new 操作符,默认会对分配的内存进行零初始化 1。尽管存在这种相似性,但 C++ 创始人 Bjarne Stroustrup 澄清称,new 操作符的设计灵感源自 Simula 语言,主要目的是解决手动内存管理带来的错误风险,而非直接继承自 Unix 源码中的这些宏定义 1。
除了传统的分配机制,早期 Unix 开发团队也曾尝试通过其他手段优化内存管理 1。Unix V7 曾引入过具备垃圾回收功能的 galloc 函数,反映出当时开发者对于减少手动内存管理负担的意愿 1。此外,P. J. Plauger 在其 1992 年的著作中建议通过聚集相关数据对象来最小化堆碎片,然而这一策略并未改变通用 malloc/free 接口的广泛采用 1。
An analysis of source code from Unix versions V4 through V8 reveals the historical development of memory allocation functions, uncovering mechanisms that have largely disappeared from modern software design. 1
The earliest iteration found in Unix V4, released in November 1973, utilized a linear allocator named alloc(). 1 This implementation was designed to exit the program immediately upon failure, meaning callers did not need to check for NULL returns. 1 Dennis Ritchie’s directory contained a malloc.c file that introduced an interface with an mp (map pointer) parameter, allowing for explicit partitioning of memory into coremap and swapmap regions. 1
By the spring release of Unix V6 in 1975, the terminology shifted to "arena," which explained distinct allocation units for core memory and disk swap areas: 64 bytes and 512 bytes, respectively. 1 The evolution continued into Unix V8, released in February 1985, which featured a tryalloc macro similar to the C++ new operator; this macro defaulted to zero-initializing allocated memory. 1
Earlier in Unix V7, developers had experimented with garbage collection by introducing a galloc function, indicating an intent to reduce the burden of manual memory management. 1 Bjarne Stroustrup, the creator of C++, clarified that the inspiration for the new operator came from the Simula language rather than direct inheritance from these Unix macros, aiming to mitigate errors associated with manual management. 1 Despite P.J. Plauger’s suggestion in a 1992 publication to minimize heap fragmentation by clustering related data objects, the conventional malloc/free interface remained the dominant standard in software engineering. 1
评论
还没有评论,欢迎留下第一条。