nginx内存管理--大块内存空间申请 ngx_palloc_large

关于内存池里面大块内存插入的时候,有点小疑惑,网上关于nginx内存管理的帖子也很多,但是关于我这个问题很少有讲的非常明白

if !NGX_DEBUG_POOL

static void *ngx_palloc_large(ngx_pool_t *pool, size_t size)
{
void *p;
ngx_uint_t n;
ngx_pool_large_t *large;

p = ngx_alloc(size, pool->log);
if (p == NULL) {
    return NULL;
}

n = 0;

for (large = pool->large; large; large = large->next) {
    if (large->alloc == NULL) {
        large->alloc = p;
        return p;
    }

//关于“3”这个值,大于3采用头插法,小于3采用尾插法,这样的好处就是你最近插入的点,查找的效率快很多。但是为什么这个值是3,个人认为有可能是经验值

         if (n++ > 3) {
                      break;
                }
    }
large = ngx_palloc(pool, sizeof(ngx_pool_large_t));
if (large == NULL) {
    ngx_free(p);
    return NULL;
}

large->alloc = p;
large->next = pool->large;
pool->large = large;

return p;

}

else

static void *
ngx_palloc_large(ngx_pool_t *pool, size_t size)
{
void *p;
ngx_pool_large_t *large;

p = ngx_alloc(size + sizeof(ngx_pool_large_t), pool->log);
if (p == NULL) {
    return NULL;
}

large = (ngx_pool_large_t *)((u_char *)p + size);
if (large == NULL) {
    ngx_free(p);
    return NULL;
}

large->alloc = p;
large->next = pool->large;
pool->large = large;

return p;

}

endif

“`

你可能感兴趣的:(nginx,nginx,nginx内存管理,ngx-palloc)