| |
| dlmalloc源码剖析之:malloc_consolidate |
|
| 来源:
ChinaUnix博客 日期:
2008.06.01 20:48 (共有条评论) 我要评论 |
| |
版权声明: 本文章由vt.buxiu发布在
www.vtzone.org
,版权归vtzone研究小组所有,转载请保持此声明!!!
@@内容摘要:
consolidate_fastbin函数用于合并fastbin中的空闲内存块,是doug lea malloc(dlmalloc)重要的函数之一。本文以dlmalloc2.7.0版本为基础,先以伪代码的形式介绍consolidate_fastbin函数的主要流程。@@
void dlmalloc_consolidate_fastbin(mstate av)
{
//第一次进来,还没初始化,先调用malloc_init_state对bins[]进行初始化
if(av->max_fast == 0)
{
malloc_init_state(av); //初始化av
return;
}
//获得unsorted_bin指针,unsorted_bin是作为队列维护,LRU顺序,首地址固定放在av->bins[0]
unsorted_bin = unsorted_chunks(av);
for(usigned int i = 0; iav->max_fast; i++)
{
if(av->fastbin != 0)
{
mchunkptr p = av->fastbin; //当前正在合并的chunk
mchunkptr nextp = p->fd; //当前正在合并的chunk的下一个chunk,控制循环使用
do{
if(p的前一个物理相邻chunk是空闲)
p与前一个chunk合并;
if(nextchunk != av->top) //与p下一个物理相邻的chunk不是top
{
if(nextchunk 是空闲)
{
p与nextchunk合并;
将nextchunk从原来链表中删除;(原来链表可能在其他bin中)
}
//注意这里!!!
//只要p下一个物理相邻的chunk不是top,程序流程都会走到这里
//unsorted_bin->first_unsorted->second_unsorted->.....->last_unsorted
// /\
// | 在这里插入
以LRU顺序将p插入到unsorted-freelist中
}
else //与p下一个物理相邻的chunk是top
(
直接与top合并,修改top指针
)
}while((p = nextp) != 0)
av->fastbin = 0; //将该av->fastbin个链表清除
}
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8059/showart_723226.html
|
| 发表评论
查看评论(共有条评论)
我要提问
|
| |
|
|
|