ptmalloc代码浅析3

malloc_consolidate函数是将fast bins中所有链表中相邻空闲chunk合并之后放入到unsorted chunk链表中。
注意fd/bk是用来获取前后空闲chunk的指针域,而要获取前后相邻chunk,需要借助宏next_chunk/prev_chunk。

 /*
    If max_fast is 0, we know that av hasn't
    yet been initialized, in which case do so below
  */


    if (get_max_fast () != 0) 
    {
        clear_fastchunks(av);


        unsorted_bin = unsorted_chunks(av);


        /*
        Remove each chunk from fast bin and consolidate it, placing it
        then in unsorted bin. Among other reasons for doing this,
        placing in unsorted bin avoids needing to calculate actual bins
        until malloc is sure that chunks aren't immediately going to be
        reused anyway.
        */


        maxfb = &fastbin (av, NFASTBINS - 1);
        fb = &fastbin (av, 0);
        do {
            p = atomic_exchange_acq (fb, 0);
            if (p != 0) {
                do {
                    check_inuse_chunk(av, p);
                    nextp = p->fd;


                    /* Slightly streamlined version of consolidation code in free() */
                    size = p->size & ~(PREV_INUSE|NON_MAIN_ARENA);
                    nextchunk = chunk_at_offset(p, size);
                    nextsize = chunksize(nextchunk);
                    // 前一个相邻chunk空闲,unlink,然后合并
                    if (!prev_inuse(p)) {
                        prevsize = p->prev_size;
                        size += prevsize;
                        p = chunk_at_offset(p, -((long) prevsize));
                        unlink(p, bck, fwd);
                    }


                    if (nextchunk != av->top) {
                        nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
                         
                        if (!nextinuse) {  // 后一个相邻chunk空闲,unlink,然后合并
                            size += nextsize;
                            unlink(nextchunk, bck, fwd);
                        } else
                        clear_inuse_bit_at_offset(nextchunk, 0);


                        first_unsorted = unsorted_bin->fd;
                        unsorted_bin->fd = p;
                        first_unsorted->bk = p;


                        if (!in_smallbin_range (size)) {
                            p->fd_nextsize = NULL;
                            p->bk_nextsize = NULL;
                        }
                        // 设置相关的头和尾标志位,然后link到unsorted bin
                        set_head(p, size | PREV_INUSE);
                        p->bk = unsorted_bin;
                        p->fd = first_unsorted;
                        set_foot(p, size);
                    }


                    else {
                        size += nextsize;
                        set_head(p, size | PREV_INUSE);
                        av->top = p;
                    }


                } while ( (p = nextp) != 0);


            }
        } while (fb++ != maxfb);
    }
    else {
        malloc_init_state(av);
        check_malloc_state(av);
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值