wodedream2013 2015-05-18 04:38 采纳率: 0%
浏览 1896

nginx动态数组中自动扩容的实现问题

为了方便阐述我的问题,我举个例子:
ngx_array_t *manager = ngx_array_create( cf->pool,1,sizeof(Node) );

在ngx_array_create的实现中,将manager的成员pool置为cf->pool; 大家知道,如果cf->pool当前内存空间不够,就会再生成另一个内存节点,那么manager可能指向的是另一个内存池节点分配的内存;

继续,关于ngx_array_push自动扩容问题的实现(我不理解的地方);
下面是实现代码:
void *
ngx_array_push(ngx_array_t *a)
{
void *elt, *new;
size_t size;
ngx_pool_t *p;

if (a->nelts == a->nalloc) {

    /* the array is full */

    size = a->size * a->nalloc;

    p = a->pool;

    if ((u_char *) a->elts + size == p->d.last   
        && p->d.last + a->size <= p->d.end) /*为什么认定当初分配的动态数组的数据区就是在cf->pool*/
    {
        /*
         * the array allocation is the last in the pool
         * and there is space for new allocation
         */

        p->d.last += a->size;
        a->nalloc++;

    } else {
        /* allocate a new array */

        new = ngx_palloc(p, 2 * size);
        if (new == NULL) {
            return NULL;
        }

        ngx_memcpy(new, a->elts, size);
        a->elts = new;
        a->nalloc *= 2;
    }
}
。。。

}

不好意思,我没有积分。。。

  • 写回答

1条回答 默认 最新

  • oyljerry 2015-05-18 15:13
    关注

    pool里面本身也还都是指针吧,空间不够也还是在其他地方分配,然后修改指针。s所有还是用pool

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题