1. 程式人生 > >nginx記憶體管理--大塊記憶體空間申請 ngx_palloc_large

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

“`