doug lea malloc源码剖析之:malloc_consolidate

流行分配器dlmalloc剖析之:malloc_consolidate

版权声明: 本文章由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; i<av->max_fast; i++)
    
{
        
if(av->fastbin[i] != 0)
        
{
            mchunkptr p 
= av->fastbin[i]; //当前正在合并的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[i] = 0;           //将该av->fastbin[i]个链表清除
        }

    
    }


}



作者:vt.buxiu@www.vtzone.org

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Bruce Eckel. "Thinking in Java" (4th Edition). Prentice Hall, 2006. 2. James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. "The Java Language Specification" (3rd Edition). Addison-Wesley Professional, 2005. 3. Herbert Schildt. "Java: A Beginner's Guide" (6th Edition). McGraw-Hill Education, 2014. 4. Cay S. Horstmann and Gary Cornell. "Core Java" (Volume 1 and 2). Prentice Hall, 2013. 5. Joshua Bloch. "Effective Java" (2nd Edition). Addison-Wesley Professional, 2008. 6. Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. "Java Concurrency in Practice". Addison-Wesley Professional, 2006. 7. Elliotte Rusty Harold. "Java I/O" (2nd Edition). O'Reilly Media, 2006. 8. Doug Lea. "Concurrent Programming in Java: Design Principles and Pattern" (2nd Edition). Addison-Wesley Professional, 2000. 9. Martin Fowler. "Refactoring: Improving the Design of Existing Code" (2nd Edition). Addison-Wesley Professional, 2018. 10. Bruce Tate, Justin Gehtland, and Erik Hatcher. "Better, Faster, Lighter Java" (1st Edition). O'Reilly Media, 2004. 中文翻译: 1. Bruce Eckel. "Java编程思想" (第4版). 机械工业出版社, 2008. 2. James Gosling, Bill Joy, Guy Steele, and Gilad Bracha. "Java语言规范" (第3版). 机械工业出版社, 2006. 3. Herbert Schildt. "Java程序设计教程" (第6版). 机械工业出版社, 2015. 4. Cay S. Horstmann and Gary Cornell. "Java核心技术" (卷1和2). 机械工业出版社, 2013. 5. Joshua Bloch. "Effective Java" (第2版). 机械工业出版社, 2009. 6. Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. "Java并发编程实践". 机械工业出版社, 2009. 7. Elliotte Rusty Harold. "Java I/O" (第2版). 中国电力出版社, 2014. 8. Doug Lea. "Java并发编程:设计原则与模式" (第2版). 机械工业出版社, 2009. 9. Martin Fowler. "重构:改善既有代码的设计" (第2版). 人民邮电出版社, 2019. 10. Bruce Tate, Justin Gehtland, and Erik Hatcher. "轻量级Java企业应用开发" (第1版). 机械工业出版社, 2005.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值