nginx中的内存池技术


 一、nginx中的内存池技术

所有的内存池都有一个共同的特点,那就是一开始就将申请大块内存,避免重复申请释放小区块造成内存碎片。nginx也不例外,我们来看看nginx是怎样管理内存吧!

(1)内存池数据结构

在nginx的内存池结构有一个内存池头部,该头部又包含一个数据部。头部(除数据部)主要用来为用户分配大块内存(通过链表)、管理外部资源、日志信息以及内存池的一些其它信息。数据部主要用来为用户分配小块内存(通过移动内存池起始指针),以及链接到下一个内存池的指针。

注意,在nginx中内存池不止一个,而且内存池的大小一开始就固定下来了,所以当内存池中容量不够时,就要重新开辟一个内存池。

  1. //回调函数结构体  
  2. struct ngx_pool_cleanup_s  
  3. {  
  4.     ngx_pool_cleanup_pt   handler;//回调函数  
  5.     void                  *data;//外部资源  
  6.     ngx_pool_cleanup_t    *next;//下一个回调函数结构体  
  7. };  
  8. typedef struct ngx_pool_large_s  ngx_pool_large_t;  
  9. //大内存结构  
  10. struct ngx_pool_large_s  
  11. {  
  12.     ngx_pool_large_t      *next;//下一个大内存结构  
  13.     void                  *alloc;//指向大块内存空间的指针  
  14. };  
  15. //内存池的数据块  
  16. typedef struct  
  17. {  
  18.     u_char                *last;//内存池可用空间的起始位置  
  19.     u_char                *end;//内存池可用空间的结束位置  
  20.     ngx_pool_t            *next;//链接到下一个内存池  
  21.     ngx_uint_t            failed;//该内存池分配失败(空间不够)次数  
  22. } ngx_pool_data_t;  
  23. //内存池结构  
  24. struct ngx_pool_s {  
  25.     ngx_pool_data_t       d;//数据块  
  26.     size_t                max;//小块内存的最大值  
  27.     ngx_pool_t            *current;//保存当前内存值  
  28.     ngx_chain_t           *chain;//目前未知  
  29.     ngx_pool_large_t      *large;//大内存结构链表,将申请超过max大小的内存块链入  
  30.     ngx_pool_cleanup_t    *cleanup;//对外部资源进行清理结构的链表  
  31.     ngx_log_t             *log;//日志信息  
  32. };  
//回调函数结构体
struct ngx_pool_cleanup_s
{
    ngx_pool_cleanup_pt   handler;//回调函数
    void                  *data;//外部资源
    ngx_pool_cleanup_t    *next;//下一个回调函数结构体
};
typedef struct ngx_pool_large_s  ngx_pool_large_t;
//大内存结构
struct ngx_pool_large_s
{
    ngx_pool_large_t      *next;//下一个大内存结构
    void                  *alloc;//指向大块内存空间的指针
};
//内存池的数据块
typedef struct
{
    u_char                *last;//内存池可用空间的起始位置
    u_char                *end;//内存池可用空间的结束位置
    ngx_pool_t            *next;//链接到下一个内存池
    ngx_uint_t            failed;//该内存池分配失败(空间不够)次数
} ngx_pool_data_t;
//内存池结构
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;//大内存结构链表,将申请超过max大小的内存块链入
    ngx_pool_cleanup_t    *cleanup;//对外部资源进行清理结构的链表
    ngx_log_t             *log;//日志信息
};

(2)创建一个内存池

通过给定容量size初始化内存池大小。

  1. //根据给定大小size创建一个新的内存池  
  2. ngx_pool_t * ngx_create_pool(size_t size, ngx_log_t *log)  
  3. {  
  4.     ngx_pool_t  *p;  
  5.   
  6.     p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);//为p分配size大小的空间,ngx_memalign为宏,实际是ngx_alloc  
  7.     if (p == NULL)  
  8.     {  
  9.         return NULL;  
  10.     }  
  11.   
  12.     p->d.last = (u_char *) p + sizeof(ngx_pool_t);//初始指向ngx_pool_t结构体结尾  
  13.     p->d.end = (u_char *) p + size; //整个结构的结尾  
  14.     p->d.next = NULL;  
  15.     p->d.failed = 0;  
  16.   
  17.     size = size - sizeof(ngx_pool_t);//内存池实际可用大小  
  18.     p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;//最大不超过NGX_MAX_ALLOC_FROM_POOL  
  19.   
  20.     p->current = p;  
  21.     p->chain = NULL;  
  22.     p->large = NULL;  
  23.     p->cleanup = NULL;  
  24.     p->log = log;  
  25.   
  26.     return p;  
  27. }  
//根据给定大小size创建一个新的内存池
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);//为p分配size大小的空间,ngx_memalign为宏,实际是ngx_alloc
    if (p == NULL)
    {
        return NULL;
    }

    p->d.last = (u_char *) p + sizeof(ngx_pool_t);//初始指向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;//最大不超过NGX_MAX_ALLOC_FROM_POOL

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

    return p;
}

(3)内存池中内存分配(考虑内存对齐)
在内存池中分配内存时有三种情况(后面会分别详细说明):
1、申请的是小块内存,内存池中空间足够。
2、申请的是小块内存,内存池中空间不够。
3、申请的是大块内存。
  1. //内存池中内存分配  
  2. void * ngx_palloc(ngx_pool_t *pool, size_t size)  
  3. {  
  4.     u_char      *m;  
  5.     ngx_pool_t  *p;  
  6.   
  7.     //申请小块内存  
  8.     if (size <= pool->max)  
  9.     {  
  10.   
  11.         p = pool->current;  
  12.         //内存池空间足够,直接移动起始指针  
  13.         do  
  14.         {  
  15.             m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);//将申请内存上调至NGX_ALIGNMENT的倍数,方便对齐内存  
  16.   
  17.             if ((size_t) (p->d.end - m) >= size)  
  18.             {  
  19.                 p->d.last = m + size;  
  20.   
  21.                 return m;  
  22.             }  
  23.   
  24.             p = p->d.next;  
  25.   
  26.         } while (p);  
  27.   
  28.         //内存池中空间不够,再建一个同等大小的内存池  
  29.         return ngx_palloc_block(pool, size);  
  30.     }  
  31.     //申请大块内存,需要在大数据块内存链表上申请内存空间  
  32.     return ngx_palloc_large(pool, size);  
  33. }  
//内存池中内存分配
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);//将申请内存上调至NGX_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);
}

第一种情况:直接移动起始指针。

第二种情况: 建立同等大小的内存池并分配size大小的空间。

  1. //重新建立相同大小的内存池  
  2. static void * ngx_palloc_block(ngx_pool_t *pool, size_t size)  
  3. {  
  4.     u_char      *m;  
  5.     size_t       psize;  
  6.     ngx_pool_t  *p, *new, *current;  
  7.   
  8.     psize = (size_t) (pool->d.end - (u_char *) pool);  
  9.   
  10.     m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);  
  11.     if (m == NULL)  
  12.     {  
  13.         return NULL;  
  14.     }  
  15.   
  16.     new = (ngx_pool_t *) m;  
  17.   
  18.     new->d.end = m + psize;  
  19.     new->d.next = NULL;  
  20.     new->d.failed = 0;  
  21.   
  22.     m += sizeof(ngx_pool_data_t);  
  23.     m = ngx_align_ptr(m, NGX_ALIGNMENT);  
  24.     new->d.last = m + size;  
  25.   
  26.     current = pool->current;  
  27.   
  28.     for (p = current; p->d.next; p = p->d.next)  
  29.     {  
  30.         if (p->d.failed++ > 4)  
  31.         {  
  32.             current = p->d.next;  
  33.         }  
  34.     }  
  35.   
  36.     p->d.next = new;  
  37.   
  38.     pool->current = current ? current : new;  
  39.   
  40.     return m;  
  41. }  
//重新建立相同大小的内存池
static void * ngx_palloc_block(ngx_pool_t *pool, size_t size)
{
    u_char      *m;
    size_t       psize;
    ngx_pool_t  *p, *new, *current;

    psize = (size_t) (pool->d.end - (u_char *) pool);

    m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
    if (m == NULL)
    {
        return NULL;
    }

    new = (ngx_pool_t *) m;

    new->d.end = m + psize;
    new->d.next = NULL;
    new->d.failed = 0;

    m += sizeof(ngx_pool_data_t);
    m = ngx_align_ptr(m, NGX_ALIGNMENT);
    new->d.last = m + size;

    current = pool->current;

    for (p = current; p->d.next; p = p->d.next)
    {
        if (p->d.failed++ > 4)
        {
            current = p->d.next;
        }
    }

    p->d.next = new;

    pool->current = current ? current : new;

    return m;
}
第三种情况:在堆中申请 大块内存,并用链表管理。

  1. //申请大块空闲数据块  
  2. static void * ngx_palloc_large(ngx_pool_t *pool, size_t size)  
  3. {  
  4.     void              *p;  
  5.     ngx_uint_t         n;  
  6.     ngx_pool_large_t  *large;  
  7.   
  8.     p = ngx_alloc(size, pool->log);//使用ngx_alloc申请数据空间  
  9.     if (p == NULL)  
  10.     {  
  11.         return NULL;  
  12.     }  
  13.   
  14.     n = 0;  
  15.   
  16.     //查看大块内存链表的前三个节点是否有alloc为NULL的节点  
  17.     for (large = pool->large; large; large = large->next)  
  18.     {  
  19.         if (large->alloc == NULL)  
  20.         {  
  21.             large->alloc = p;  
  22.             return p;  
  23.         }  
  24.   
  25.         if (n++ > 3) {  
  26.             break;  
  27.         }  
  28.     }  
  29.   
  30.     //如果没有则新生成一个节点链进去  
  31.     large = ngx_palloc(pool, sizeof(ngx_pool_large_t));  
  32.     if (large == NULL)  
  33.     {  
  34.         ngx_free(p);  
  35.         return NULL;  
  36.     }  
  37.   
  38.     large->alloc = p;  
  39.     large->next = pool->large;  
  40.     pool->large = large;  
  41.   
  42.     return p;  
  43. }  
//申请大块空闲数据块
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);//使用ngx_alloc申请数据空间
    if (p == NULL)
    {
        return NULL;
    }

    n = 0;

    //查看大块内存链表的前三个节点是否有alloc为NULL的节点
    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;
    pool->large = large;

    return p;
}

(4)内存池中内存释放
对于nginx的内存池,小块内存申请后是 不释放的,释放大块内存时,先通过要释放区域的地址在大块内存的管理链表中找到相应的节点将其释放掉。注意,这里只释放大块的内存,并不会释放其对应的头部结构,头部结构会做下一次申请大内存之用。

  1. //控制大块内存的释放  
  2. ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p)  
  3. {  
  4.     ngx_pool_large_t  *l;  
  5.   
  6.     for (l = pool->large; l; l = l->next)  
  7.     {  
  8.         if (p == l->alloc)  
  9.         {  
  10.             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, ”free: %p”, l->alloc);  
  11.             ngx_free(l->alloc);  
  12.             l->alloc = NULL;  
  13.   
  14.             return NGX_OK;  
  15.         }  
  16.     }  
  17.   
  18.     return NGX_DECLINED;  
  19. }  
//控制大块内存的释放
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;
}

(5)外部资源管理
nginx内存池还通过回调函数对外部资源的清理。ngx_pool_cleanup_t是外部资源管理的结构体节点,内存池中有一个链表用来链接这些节点。我们可以通过ngx_pool_cleanup_add获取一个新的节点的指针,然后通过该指针来设置要管理的外部资源地址以及清理这些资源要用到的方法(回调函数)。

  1. //向外部资源管理的结构体链表中添加新的节点数据  
  2. ngx_pool_cleanup_t * ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)  
  3. {  
  4.     ngx_pool_cleanup_t  *c;  
  5.   
  6.     c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));  
  7.     if (c == NULL)  
  8.     {  
  9.         return NULL;  
  10.     }  
  11.   
  12.     if (size)  
  13.     {  
  14.         c->data = ngx_palloc(p, size);//申请存放目标数据的空间  
  15.         if (c->data == NULL)  
  16.         {  
  17.             return NULL;  
  18.         }  
  19.     }  
  20.     else  
  21.     {  
  22.         c->data = NULL;  
  23.     }  
  24.   
  25.     c->handler = NULL;//这里暂时设为NULL,返回ngx_pool_cleanup_t后在外部设置回调函数  
  26.     c->next = p->cleanup;  
  27.   
  28.     p->cleanup = c;  
  29.   
  30.     ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, ”add cleanup: %p”, c);  
  31.   
  32.     return c;  
  33. }  
//向外部资源管理的结构体链表中添加新的节点数据
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;
        }
    }
    else
    {
        c->data = NULL;
    }

    c->handler = NULL;//这里暂时设为NULL,返回ngx_pool_cleanup_t后在外部设置回调函数
    c->next = p->cleanup;

    p->cleanup = c;

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

    return c;
}

(6) 销毁内存池
销毁内存池时主要是对外部资源进行清理,以及对大块数据内存进行清理。
这里的代码我觉得是有问题的,虽然外部资源和大块数据的内存都被释放,但是两个链表中的节点依然存在,而这些节点之前也是在堆中申请了空间,理应释放掉,但是销毁内存池时并没有出现相关代码,我觉得nginx应该不会犯这种低级错误,可能有哪些东西我没有注意到,有知道的同学请指正。
  1. void ngx_destroy_pool(ngx_pool_t *pool)  
  2. {  
  3.     ngx_pool_t          *p, *n;  
  4.     ngx_pool_large_t    *l;  
  5.     ngx_pool_cleanup_t  *c;  
  6.     //遍历链表对外部资源进行清理  
  7.     for (c = pool->cleanup; c; c = c->next)  
  8.     {  
  9.         if (c->handler) {  
  10.             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,”run cleanup: %p”, c);  
  11.             c->handler(c->data);//通过回调函数清理  
  12.         }  
  13.     }  
  14.     //遍历链表对大块数据内存清理  
  15.     for (l = pool->large; l; l = l->next)  
  16.     {  
  17.         ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, ”free: %p”, l->alloc);  
  18.   
  19.         if (l->alloc)  
  20.         {  
  21.             ngx_free(l->alloc);//ngx_free为宏,本质是free  
  22.         }  
  23.     }  
  24.   
  25.     for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next)  
  26.     {  
  27.         ngx_free(p);  
  28.   
  29.         if (n == NULL)  
  30.         {  
  31.             break;  
  32.         }  
  33.     }  
  34. }  
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);//ngx_free为宏,本质是free
        }
    }

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

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

二、nginx内存池 vs STL内存池

大数据块:nginx和STL一样,对于大的数据区块都是直接在堆中分配空间,只不过nginx将这些大的数据区块的链接起来进行管理,而STL中则是由申请数据块的程序自己负责管理。

小数据块:nginx和STL一样,都要对申请小的数据区块值上调到某个值的倍数,方便内存对齐。在nginx中是直接在内存池的那块连续空间中进行分配,而STL中内存池中的连续空间只能为空闲链表提供数据块,然后根据申请的数据块的大小找到相应的空闲链表取出相应数据块。

nginx内存池的独特之处:能够对外部资源进行管理、能够记录日志。

STL内存池的独特之处:空闲链表节点的设计非常巧妙,避免空间的浪费。详见我的另一篇博文:


参考:

《深入理解nginx》

http://blog.163.com/zhangjie_0303/blog/static/99082706201311321157588/

http://www.cnblogs.com/sld666666/archive/2010/06/27/1766255.html

http://blog.csdn.net/chen19870707/article/details/41015613




 一、nginx中的内存池技术

所有的内存池都有一个共同的特点,那就是一开始就将申请大块内存,避免重复申请释放小区块造成内存碎片。nginx也不例外,我们来看看nginx是怎样管理内存吧!

(1)内存池数据结构

在nginx的内存池结构有一个内存池头部,该头部又包含一个数据部。头部(除数据部)主要用来为用户分配大块内存(通过链表)、管理外部资源、日志信息以及内存池的一些其它信息。数据部主要用来为用户分配小块内存(通过移动内存池起始指针),以及链接到下一个内存池的指针。

注意,在nginx中内存池不止一个,而且内存池的大小一开始就固定下来了,所以当内存池中容量不够时,就要重新开辟一个内存池。

  1. //回调函数结构体  
  2. struct ngx_pool_cleanup_s  
  3. {  
  4.     ngx_pool_cleanup_pt   handler;//回调函数  
  5.     void                  *data;//外部资源  
  6.     ngx_pool_cleanup_t    *next;//下一个回调函数结构体  
  7. };  
  8. typedef struct ngx_pool_large_s  ngx_pool_large_t;  
  9. //大内存结构  
  10. struct ngx_pool_large_s  
  11. {  
  12.     ngx_pool_large_t      *next;//下一个大内存结构  
  13.     void                  *alloc;//指向大块内存空间的指针  
  14. };  
  15. //内存池的数据块  
  16. typedef struct  
  17. {  
  18.     u_char                *last;//内存池可用空间的起始位置  
  19.     u_char                *end;//内存池可用空间的结束位置  
  20.     ngx_pool_t            *next;//链接到下一个内存池  
  21.     ngx_uint_t            failed;//该内存池分配失败(空间不够)次数  
  22. } ngx_pool_data_t;  
  23. //内存池结构  
  24. struct ngx_pool_s {  
  25.     ngx_pool_data_t       d;//数据块  
  26.     size_t                max;//小块内存的最大值  
  27.     ngx_pool_t            *current;//保存当前内存值  
  28.     ngx_chain_t           *chain;//目前未知  
  29.     ngx_pool_large_t      *large;//大内存结构链表,将申请超过max大小的内存块链入  
  30.     ngx_pool_cleanup_t    *cleanup;//对外部资源进行清理结构的链表  
  31.     ngx_log_t             *log;//日志信息  
  32. };  
//回调函数结构体
struct ngx_pool_cleanup_s
{
    ngx_pool_cleanup_pt   handler;//回调函数
    void                  *data;//外部资源
    ngx_pool_cleanup_t    *next;//下一个回调函数结构体
};
typedef struct ngx_pool_large_s  ngx_pool_large_t;
//大内存结构
struct ngx_pool_large_s
{
    ngx_pool_large_t      *next;//下一个大内存结构
    void                  *alloc;//指向大块内存空间的指针
};
//内存池的数据块
typedef struct
{
    u_char                *last;//内存池可用空间的起始位置
    u_char                *end;//内存池可用空间的结束位置
    ngx_pool_t            *next;//链接到下一个内存池
    ngx_uint_t            failed;//该内存池分配失败(空间不够)次数
} ngx_pool_data_t;
//内存池结构
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;//大内存结构链表,将申请超过max大小的内存块链入
    ngx_pool_cleanup_t    *cleanup;//对外部资源进行清理结构的链表
    ngx_log_t             *log;//日志信息
};

(2)创建一个内存池

通过给定容量size初始化内存池大小。

  1. //根据给定大小size创建一个新的内存池  
  2. ngx_pool_t * ngx_create_pool(size_t size, ngx_log_t *log)  
  3. {  
  4.     ngx_pool_t  *p;  
  5.   
  6.     p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);//为p分配size大小的空间,ngx_memalign为宏,实际是ngx_alloc  
  7.     if (p == NULL)  
  8.     {  
  9.         return NULL;  
  10.     }  
  11.   
  12.     p->d.last = (u_char *) p + sizeof(ngx_pool_t);//初始指向ngx_pool_t结构体结尾  
  13.     p->d.end = (u_char *) p + size; //整个结构的结尾  
  14.     p->d.next = NULL;  
  15.     p->d.failed = 0;  
  16.   
  17.     size = size - sizeof(ngx_pool_t);//内存池实际可用大小  
  18.     p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;//最大不超过NGX_MAX_ALLOC_FROM_POOL  
  19.   
  20.     p->current = p;  
  21.     p->chain = NULL;  
  22.     p->large = NULL;  
  23.     p->cleanup = NULL;  
  24.     p->log = log;  
  25.   
  26.     return p;  
  27. }  
//根据给定大小size创建一个新的内存池
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);//为p分配size大小的空间,ngx_memalign为宏,实际是ngx_alloc
    if (p == NULL)
    {
        return NULL;
    }

    p->d.last = (u_char *) p + sizeof(ngx_pool_t);//初始指向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;//最大不超过NGX_MAX_ALLOC_FROM_POOL

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

    return p;
}

(3)内存池中内存分配(考虑内存对齐)
在内存池中分配内存时有三种情况(后面会分别详细说明):
1、申请的是小块内存,内存池中空间足够。
2、申请的是小块内存,内存池中空间不够。
3、申请的是大块内存。
  1. //内存池中内存分配  
  2. void * ngx_palloc(ngx_pool_t *pool, size_t size)  
  3. {  
  4.     u_char      *m;  
  5.     ngx_pool_t  *p;  
  6.   
  7.     //申请小块内存  
  8.     if (size <= pool->max)  
  9.     {  
  10.   
  11.         p = pool->current;  
  12.         //内存池空间足够,直接移动起始指针  
  13.         do  
  14.         {  
  15.             m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);//将申请内存上调至NGX_ALIGNMENT的倍数,方便对齐内存  
  16.   
  17.             if ((size_t) (p->d.end - m) >= size)  
  18.             {  
  19.                 p->d.last = m + size;  
  20.   
  21.                 return m;  
  22.             }  
  23.   
  24.             p = p->d.next;  
  25.   
  26.         } while (p);  
  27.   
  28.         //内存池中空间不够,再建一个同等大小的内存池  
  29.         return ngx_palloc_block(pool, size);  
  30.     }  
  31.     //申请大块内存,需要在大数据块内存链表上申请内存空间  
  32.     return ngx_palloc_large(pool, size);  
  33. }  
//内存池中内存分配
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);//将申请内存上调至NGX_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);
}

第一种情况:直接移动起始指针。

第二种情况: 建立同等大小的内存池并分配size大小的空间。

  1. //重新建立相同大小的内存池  
  2. static void * ngx_palloc_block(ngx_pool_t *pool, size_t size)  
  3. {  
  4.     u_char      *m;  
  5.     size_t       psize;  
  6.     ngx_pool_t  *p, *new, *current;  
  7.   
  8.     psize = (size_t) (pool->d.end - (u_char *) pool);  
  9.   
  10.     m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);  
  11.     if (m == NULL)  
  12.     {  
  13.         return NULL;  
  14.     }  
  15.   
  16.     new = (ngx_pool_t *) m;  
  17.   
  18.     new->d.end = m + psize;  
  19.     new->d.next = NULL;  
  20.     new->d.failed = 0;  
  21.   
  22.     m += sizeof(ngx_pool_data_t);  
  23.     m = ngx_align_ptr(m, NGX_ALIGNMENT);  
  24.     new->d.last = m + size;  
  25.   
  26.     current = pool->current;  
  27.   
  28.     for (p = current; p->d.next; p = p->d.next)  
  29.     {  
  30.         if (p->d.failed++ > 4)  
  31.         {  
  32.             current = p->d.next;  
  33.         }  
  34.     }  
  35.   
  36.     p->d.next = new;  
  37.   
  38.     pool->current = current ? current : new;  
  39.   
  40.     return m;  
  41. }  
//重新建立相同大小的内存池
static void * ngx_palloc_block(ngx_pool_t *pool, size_t size)
{
    u_char      *m;
    size_t       psize;
    ngx_pool_t  *p, *new, *current;

    psize = (size_t) (pool->d.end - (u_char *) pool);

    m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
    if (m == NULL)
    {
        return NULL;
    }

    new = (ngx_pool_t *) m;

    new->d.end = m + psize;
    new->d.next = NULL;
    new->d.failed = 0;

    m += sizeof(ngx_pool_data_t);
    m = ngx_align_ptr(m, NGX_ALIGNMENT);
    new->d.last = m + size;

    current = pool->current;

    for (p = current; p->d.next; p = p->d.next)
    {
        if (p->d.failed++ > 4)
        {
            current = p->d.next;
        }
    }

    p->d.next = new;

    pool->current = current ? current : new;

    return m;
}
第三种情况:在堆中申请 大块内存,并用链表管理。

  1. //申请大块空闲数据块  
  2. static void * ngx_palloc_large(ngx_pool_t *pool, size_t size)  
  3. {  
  4.     void              *p;  
  5.     ngx_uint_t         n;  
  6.     ngx_pool_large_t  *large;  
  7.   
  8.     p = ngx_alloc(size, pool->log);//使用ngx_alloc申请数据空间  
  9.     if (p == NULL)  
  10.     {  
  11.         return NULL;  
  12.     }  
  13.   
  14.     n = 0;  
  15.   
  16.     //查看大块内存链表的前三个节点是否有alloc为NULL的节点  
  17.     for (large = pool->large; large; large = large->next)  
  18.     {  
  19.         if (large->alloc == NULL)  
  20.         {  
  21.             large->alloc = p;  
  22.             return p;  
  23.         }  
  24.   
  25.         if (n++ > 3) {  
  26.             break;  
  27.         }  
  28.     }  
  29.   
  30.     //如果没有则新生成一个节点链进去  
  31.     large = ngx_palloc(pool, sizeof(ngx_pool_large_t));  
  32.     if (large == NULL)  
  33.     {  
  34.         ngx_free(p);  
  35.         return NULL;  
  36.     }  
  37.   
  38.     large->alloc = p;  
  39.     large->next = pool->large;  
  40.     pool->large = large;  
  41.   
  42.     return p;  
  43. }  
//申请大块空闲数据块
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);//使用ngx_alloc申请数据空间
    if (p == NULL)
    {
        return NULL;
    }

    n = 0;

    //查看大块内存链表的前三个节点是否有alloc为NULL的节点
    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;
    pool->large = large;

    return p;
}

(4)内存池中内存释放
对于nginx的内存池,小块内存申请后是 不释放的,释放大块内存时,先通过要释放区域的地址在大块内存的管理链表中找到相应的节点将其释放掉。注意,这里只释放大块的内存,并不会释放其对应的头部结构,头部结构会做下一次申请大内存之用。

  1. //控制大块内存的释放  
  2. ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p)  
  3. {  
  4.     ngx_pool_large_t  *l;  
  5.   
  6.     for (l = pool->large; l; l = l->next)  
  7.     {  
  8.         if (p == l->alloc)  
  9.         {  
  10.             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, ”free: %p”, l->alloc);  
  11.             ngx_free(l->alloc);  
  12.             l->alloc = NULL;  
  13.   
  14.             return NGX_OK;  
  15.         }  
  16.     }  
  17.   
  18.     return NGX_DECLINED;  
  19. }  
//控制大块内存的释放
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;
}

(5)外部资源管理
nginx内存池还通过回调函数对外部资源的清理。ngx_pool_cleanup_t是外部资源管理的结构体节点,内存池中有一个链表用来链接这些节点。我们可以通过ngx_pool_cleanup_add获取一个新的节点的指针,然后通过该指针来设置要管理的外部资源地址以及清理这些资源要用到的方法(回调函数)。

  1. //向外部资源管理的结构体链表中添加新的节点数据  
  2. ngx_pool_cleanup_t * ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)  
  3. {  
  4.     ngx_pool_cleanup_t  *c;  
  5.   
  6.     c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));  
  7.     if (c == NULL)  
  8.     {  
  9.         return NULL;  
  10.     }  
  11.   
  12.     if (size)  
  13.     {  
  14.         c->data = ngx_palloc(p, size);//申请存放目标数据的空间  
  15.         if (c->data == NULL)  
  16.         {  
  17.             return NULL;  
  18.         }  
  19.     }  
  20.     else  
  21.     {  
  22.         c->data = NULL;  
  23.     }  
  24.   
  25.     c->handler = NULL;//这里暂时设为NULL,返回ngx_pool_cleanup_t后在外部设置回调函数  
  26.     c->next = p->cleanup;  
  27.   
  28.     p->cleanup = c;  
  29.   
  30.     ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, ”add cleanup: %p”, c);  
  31.   
  32.     return c;  
  33. }  
//向外部资源管理的结构体链表中添加新的节点数据
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;
        }
    }
    else
    {
        c->data = NULL;
    }

    c->handler = NULL;//这里暂时设为NULL,返回ngx_pool_cleanup_t后在外部设置回调函数
    c->next = p->cleanup;

    p->cleanup = c;

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

    return c;
}

(6) 销毁内存池
销毁内存池时主要是对外部资源进行清理,以及对大块数据内存进行清理。
这里的代码我觉得是有问题的,虽然外部资源和大块数据的内存都被释放,但是两个链表中的节点依然存在,而这些节点之前也是在堆中申请了空间,理应释放掉,但是销毁内存池时并没有出现相关代码,我觉得nginx应该不会犯这种低级错误,可能有哪些东西我没有注意到,有知道的同学请指正。
  1. void ngx_destroy_pool(ngx_pool_t *pool)  
  2. {  
  3.     ngx_pool_t          *p, *n;  
  4.     ngx_pool_large_t    *l;  
  5.     ngx_pool_cleanup_t  *c;  
  6.     //遍历链表对外部资源进行清理  
  7.     for (c = pool->cleanup; c; c = c->next)  
  8.     {  
  9.         if (c->handler) {  
  10.             ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,”run cleanup: %p”, c);  
  11.             c->handler(c->data);//通过回调函数清理  
  12.         }  
  13.     }  
  14.     //遍历链表对大块数据内存清理  
  15.     for (l = pool->large; l; l = l->next)  
  16.     {  
  17.         ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, ”free: %p”, l->alloc);  
  18.   
  19.         if (l->alloc)  
  20.         {  
  21.             ngx_free(l->alloc);//ngx_free为宏,本质是free  
  22.         }  
  23.     }  
  24.   
  25.     for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next)  
  26.     {  
  27.         ngx_free(p);  
  28.   
  29.         if (n == NULL)  
  30.         {  
  31.             break;  
  32.         }  
  33.     }  
  34. }  
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);//ngx_free为宏,本质是free
        }
    }

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

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

二、nginx内存池 vs STL内存池

大数据块:nginx和STL一样,对于大的数据区块都是直接在堆中分配空间,只不过nginx将这些大的数据区块的链接起来进行管理,而STL中则是由申请数据块的程序自己负责管理。

小数据块:nginx和STL一样,都要对申请小的数据区块值上调到某个值的倍数,方便内存对齐。在nginx中是直接在内存池的那块连续空间中进行分配,而STL中内存池中的连续空间只能为空闲链表提供数据块,然后根据申请的数据块的大小找到相应的空闲链表取出相应数据块。

nginx内存池的独特之处:能够对外部资源进行管理、能够记录日志。

STL内存池的独特之处:空闲链表节点的设计非常巧妙,避免空间的浪费。详见我的另一篇博文:


参考:

《深入理解nginx》

http://blog.163.com/zhangjie_0303/blog/static/99082706201311321157588/

http://www.cnblogs.com/sld666666/archive/2010/06/27/1766255.html

http://blog.csdn.net/chen19870707/article/details/41015613



猜你喜欢

转载自blog.csdn.net/luguifang2011/article/details/80639974