nginx学习篇:内存池

nginx管理内存池的数据结构是ngx_pool_t。在ngx_request_t, ngx_conf_t中都控制着ngx_pool_t。

一.在nginx的Ngx_palloc.h里的定义如下:

struct ngx_pool_s {
    ngx_pool_data_t       d;
    size_t                max;
    ngx_pool_t           *current;
    ngx_chain_t          *chain;
    ngx_pool_large_t     *large;
    ngx_pool_cleanup_t   *cleanup;
    ngx_log_t            *log;
};

typedef struct {
    u_char               *last;
    u_char               *end;
    ngx_pool_t           *next;
    ngx_uint_t            failed;
} ngx_pool_data_t;

last指向当前内存块的已使用的末尾,end指向当前内存块的末尾,next指向下一个内存块,nginx使用的是内存池链表。


typedef struct ngx_pool_large_s  ngx_pool_large_t;

struct ngx_pool_large_s {
    ngx_pool_large_t     *next;
    void                 *alloc;
};
当分配的内存池的大小超过 NGX_MAX_ALLOC_FROM_POOL 时,此内存池的指针保留一份于此结构体中。这样做的好处是可以


struct ngx_pool_cleanup_s {
    ngx_pool_cleanup_pt   handler;
    void                 *data;
    ngx_pool_cleanup_t   *next;
};

在这个结果中保存这需要清理的数据指针以及相应的清理函数, 让内存池销毁

二.控制内存池的相关函数

ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log);
void ngx_destroy_pool(ngx_pool_t *pool);
void ngx_reset_pool(ngx_pool_t *pool);

void *ngx_palloc(ngx_pool_t *pool, size_t size);
void *ngx_pnalloc(ngx_pool_t *pool, size_t size);
void *ngx_pcalloc(ngx_pool_t *pool, size_t size);
void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment);
ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p);


ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size);
void ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd);
void ngx_pool_cleanup_file(void *data);
void ngx_pool_delete_file(void *data);

2.1 注意与Ngx_alloc.h中的void *ngx_alloc(size_t size, ngx_log_t *log);和void *ngx_calloc(size_t size, ngx_log_t *log)区分

根据定义:

ngx_alloc(size_t size, ngx_log_t *log)
{
    void  *p;

    p = malloc(size);
    if (p == NULL) {
        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
                      "malloc(%uz) failed", size);
    }

    ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size);

    return p;
}


void *
ngx_calloc(size_t size, ngx_log_t *log)//initial the ngx_alloc
{
    void  *p;

    p = ngx_alloc(size, log);

    if (p) {
        ngx_memzero(p, size);
    }

    return p;
}
ngx_alloc和ngx_calloc只是对alloc和calloc的简单封装。ngx_palloc是对内存池的控制,意义完全不一样。

2.2 简述各个函数的意义

ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log);//创建一个内存池单元,注意nginx的内存池是用链表连接起来的
ngx_pool_t *
ngx_create_pool(size_t size, ngx_log_t *log)
{
    ngx_pool_t  *p;

    p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);//要求内存对齐,感兴趣的可以查看nginx是怎么做到对齐的,做得很tricky,可见nginx对细节的处理。
    if (p == NULL) {
        return NULL;
    }

    p->d.last = (u_char *) p + sizeof(ngx_pool_t);
    p->d.end = (u_char *) p + size;
    p->d.next = NULL;
    p->d.failed = 0;

    size = size - sizeof(ngx_pool_t);
    p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;//设置的size不能超过NGX_MAX_ALLOC_FROM_POOL

    p->current = p;
    p->chain = NULL;
    p->large = NULL;
    p->cleanup = NULL;
    p->log = log;

    return p;
}

void ngx_destroy_pool(ngx_pool_t *pool);//清晰内存池结构,这里清理的对象包括cleanup,large和d。
void
ngx_destroy_pool(ngx_pool_t *pool)
{
    ngx_pool_t          *p, *n;
    ngx_pool_large_t    *l;
    ngx_pool_cleanup_t  *c;

    for (c = pool->cleanup; c; c = c->next) {
        if (c->handler) {
            ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
                           "run cleanup: %p", c);
            c->handler(c->data);
        }
    }

    for (l = pool->large; l; l = l->next) {

        ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc);

        if (l->alloc) {
            ngx_free(l->alloc);
        }
    }

#if (NGX_DEBUG)

    /*
     * we could allocate the pool->log from this pool
     * so we cannot use this log while free()ing the pool
     */

    for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
        ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
                       "free: %p, unused: %uz", p, p->d.end - p->d.last);

        if (n == NULL) {
            break;
        }
    }

#endif

    for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
        ngx_free(p);

        if (n == NULL) {
            break;
        }
    }
}


void ngx_reset_pool(ngx_pool_t *pool);//根据字面reset,此函数的作用是重置一个pool。
void
ngx_reset_pool(ngx_pool_t *pool)
{
    ngx_pool_t        *p;
    ngx_pool_large_t  *l;

    for (l = pool->large; l; l = l->next) {
        if (l->alloc) {
            ngx_free(l->alloc);
        }
    }

    for (p = pool; p; p = p->d.next) {
        p->d.last = (u_char *) p + sizeof(ngx_pool_t);//ngx_pool_t本身要占用一段内存
        p->d.failed = 0;
    }

    pool->current = pool;
    pool->chain = NULL;
    pool->large = NULL;
}


void *ngx_palloc(ngx_pool_t *pool, size_t size);//从分配好的内存池中取一部分
void *
ngx_palloc(ngx_pool_t *pool, size_t size)
{
    u_char      *m;
    ngx_pool_t  *p;

    if (size <= pool->max) {

        p = pool->current;

        do {
            m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);//alignment 

            if ((size_t) (p->d.end - m) >= size) {
                p->d.last = m + size;

                return m;
            }

            p = p->d.next;

        } while (p);

        return ngx_palloc_block(pool, size);//获取失败,再分配一块内存池
    }

    return ngx_palloc_large(pool, size);//获取失败,再分配一块large内存池
}


void *ngx_pnalloc(ngx_pool_t *pool, size_t size);//与ngx_palloc的区别是:ngx_pnalloc获取的内存不会考虑对齐的情况。
void *
ngx_pnalloc(ngx_pool_t *pool, size_t size)
{
    u_char      *m;
    ngx_pool_t  *p;

    if (size <= pool->max) {

        p = pool->current;

        do {
            m = p->d.last;//直接在后面获取,不考虑对齐

            if ((size_t) (p->d.end - m) >= size) {
                p->d.last = m + size;

                return m;
            }

            p = p->d.next;

        } while (p);

        return ngx_palloc_block(pool, size);
    }

    return ngx_palloc_large(pool, size);
}


void *ngx_pcalloc(ngx_pool_t *pool, size_t size);//对mgx_palloc分配的内存进行了初始化
void *
ngx_pcalloc(ngx_pool_t *pool, size_t size)
{
    void *p;

    p = ngx_palloc(pool, size);
    if (p) {
        ngx_memzero(p, size);
    }

    return p;
}


static void *ngx_palloc_large(ngx_pool_t *pool, size_t size);//分配大内存块
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;
        }


        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;//put in the head???
    pool->large = large;


    return p;
}


void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment);//分配大内存块,与ngx_palloc_large相比,增加了对齐的功能。
void *
ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
{
    void              *p;
    ngx_pool_large_t  *large;


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


    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;
}



ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p);//小块内存得用destroy释放,但是大块内存可以用free来释放。
ngx_int_t
ngx_pfree(ngx_pool_t *pool, void *p)
{
    ngx_pool_large_t  *l;


    for (l = pool->large; l; l = l->next) {
        if (p == l->alloc) {
            ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
                           "free: %p", l->alloc);
            ngx_free(l->alloc);
            l->alloc = NULL;


            return NGX_OK;
        }
    }


    return NGX_DECLINED;
}


ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size);//注册一段内存的分配
ngx_pool_cleanup_t *
ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)
{
    ngx_pool_cleanup_t  *c;


    c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));
    if (c == NULL) {
        return NULL;
    }


    if (size) {
        c->data = ngx_palloc(p, size);
        if (c->data == NULL) {
            return NULL;//这里可能可以进一步优化,因为data分配失败,应该立马free掉c占有的内存。
        }


    } else {
        c->data = NULL;
    }


    c->handler = NULL;
    c->next = p->cleanup;//put int header


    p->cleanup = c;


    ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, "add cleanup: %p", c);


    return c;
}
与文件相关的三个函数操作如下,不再累述。
void ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd);

void ngx_pool_cleanup_file(void *data);

void ngx_pool_delete_file(void *data);



你可能感兴趣的:(nginx,系列学习篇,ngx_pool_t,nginx)