一、nginx中的内存池技术
所有的内存池都有一个共同的特点,那就是一开始就将申请大块内存,避免重复申请释放小区块造成内存碎片。nginx也不例外,我们来看看nginx是怎样管理内存吧!
(1)内存池数据结构
在nginx的内存池结构有一个内存池头部,该头部又包含一个数据部。头部(除数据部)主要用来为用户分配大块内存(通过链表)、管理外部资源、日志信息以及内存池的一些其它信息。数据部主要用来为用户分配小块内存(通过移动内存池起始指针),以及链接到下一个内存池的指针。
注意,在nginx中内存池不止一个,而且内存池的大小一开始就固定下来了,所以当内存池中容量不够时,就要重新开辟一个内存池。
- //回调函数结构体
- 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;//日志信息
- };
//回调函数结构体 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创建一个新的内存池
- 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;
- }
//根据给定大小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; }
- //内存池中内存分配
- 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);
- }
//内存池中内存分配 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); }
- //重新建立相同大小的内存池
- 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;
- }
//重新建立相同大小的内存池 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; }
- //申请大块空闲数据块
- 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;
- }
//申请大块空闲数据块 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; }
- //控制大块内存的释放
- 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_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 *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;
- }
//向外部资源管理的结构体链表中添加新的节点数据 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; }
- 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;
- }
- }
- }
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中内存池不止一个,而且内存池的大小一开始就固定下来了,所以当内存池中容量不够时,就要重新开辟一个内存池。
- //回调函数结构体
- 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;//日志信息
- };
//回调函数结构体 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创建一个新的内存池
- 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;
- }
//根据给定大小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; }
- //内存池中内存分配
- 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);
- }
//内存池中内存分配 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); }
- //重新建立相同大小的内存池
- 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;
- }
//重新建立相同大小的内存池 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; }
- //申请大块空闲数据块
- 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;
- }
//申请大块空闲数据块 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; }
- //控制大块内存的释放
- 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_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 *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;
- }
//向外部资源管理的结构体链表中添加新的节点数据 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; }
- 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;
- }
- }
- }
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