在 malloc 实现中维护空闲列表
我正在尝试为我的操作系统类实现 malloc,并且我想知道维护空闲内存块的双向链表相对于单链表的优势。
I'm trying to implement malloc for my Operating Systems class, and I was wondering about the advantages of maintaining a doubly linked list of free memory blocks as opposed to a singly linked list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在 malloc() 中将一大块内存分成较小的内存块,那么当您使用 free() 返回这些内存块时,您必须将每个返回的块与其 2 个邻居连接起来。在这种情况下,双向链表是最容易处理的。
If you split one big block of memory into smaller ones in malloc(), then when you return those pieces of it with free(), you'll have to concatenate each returned block with its 2 neighbors. A doubly linked list is the easiest to deal with in such a situation.
使用隐式列表的
free_block
的粗略草图。内存被排列为一个整数数组,block
是块到该数组中空闲的偏移量,prev
和next
是偏移量前面和后面的块。每个块都以包含块大小的整数开始和结束,包括页眉和页脚:由于包含块大小的页眉在每个块的开头和结尾处重复,因此它充当双向链表,您可以向前和向后遍历空闲列表。指针是隐式的或相对的。
Rough sketch of
free_block
using an implict list. Memory is arranged as an array of ints,block
is the offset of the block to the free in that array,prev
andnext
are the offsets of the preceding and following blocks. Each block both begins and ends with an integer containing the size of the block, including the header and footer:Because the header containing the block size is duplicated at the beginning and end of each block, it serves as the doubly linked list and you can traverse the free list forwards and backwards. The pointers are implicit or or relative.