1.源代码位置
头文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_palloc.h
源文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_palloc.c
2.数据结构定义
先来学习一下nginx内存池的几个主要数据结构:
ngx_pool_data_t(内存池数据块结构)
1:typedef struct {2: u_char *last;3: u_char *end;4: ngx_pool_t *next;5: ngx_uint_t failed;6: } ngx_pool_data_t;
- last:是一个unsigned char 类型的指针,保存的是/当前内存池分配到末位地址,即下一次分配从此处开始。
- end:内存池结束位置;
- next:内存池里面有很多块内存,这些内存块就是通过该指针连成链表的,next指向下一块内存。
- failed:内存池分配失败次数。
ngx_pool_s(内存池头部结构)
1:struct ngx_pool_s {2: ngx_pool_data_t d;3: size_t max;4: ngx_pool_t *current;5: ngx_chain_t *chain;6: ngx_pool_large_t *large;7: ngx_pool_cleanup_t *cleanup;8: ngx_log_t *log;9: };
- d:内存池的数据块;
- max:内存池数据块的最大值;
- current:指向当前内存池;
- chain:该指针挂接一个ngx_chain_t结构;
- large:大块内存链表,即分配空间超过max的情况使用;
- cleanup:释放内存池的callback
- log:日志信息
由ngx_pool_data_t和ngx_pool_t组成的nginx内存池结构如下图所示:
3.相关函数介绍
在分析内存池方法前,需要对几个主要的内存相关函数作一下介绍:
ngx_alloc:(只是对malloc进行了简单的封装)
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1:void *<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_alloc(size_t size, ngx_log_t *log)<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: void *p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: p = malloc(size);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: if (p == NULL) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: "malloc(%uz) failed", size);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 14: return p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 15: }
ngx_calloc:(调用malloc并初始化为0)
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1:void *<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_calloc(size_t size, ngx_log_t *log)<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: void *p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: p = ngx_alloc(size, log);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: if (p) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: ngx_memzero(p, size);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: return p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: }
ngx_memzero:
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1: #define ngx_memzero(buf, n) (void) memset(buf, 0, n)
ngx_free :
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1: #define ngx_free free
ngx_memalign:
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1:void *<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: void *p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: int err;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: err = posix_memalign(&p, alignment, size);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: if (err) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: ngx_log_error(NGX_LOG_EMERG, log, err,<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: "posix_memalign(%uz, %uz) failed", alignment, size);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: p = NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 14: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 15: ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, 0,<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 16: "posix_memalign: %p:%uz @%uz", p, size, alignment);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 17: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 18: return p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 19: }
这里alignment主要是针对部分unix平台需要动态的对齐,对POSIX 1003.1d提供的posix_memalign( )进行封装,在大多数情况下,编译器和C库透明地帮你处理对齐问题。nginx中通过宏NGX_HAVE_POSIX_MEMALIGN来控制;调用posix_memalign( )成功时会返回size字节的动态内存,并且这块内存的地址是alignment的倍数。参数alignment必须是2的幂,还是void指针的大小的倍数。返回的内存块的地址放在了memptr里面,函数返回值是0.
4.内存池基本操作
- 内存池对外的主要方法有:
| 创建内存池 | 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); |
| 内存清除 | ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p); |
4.1 创建内存池ngx_create_pool
ngx_create_pool用于创建一个内存池,我们创建时,传入我们的需要的初始大小:
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1: ngx_pool_t *<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_create_pool(size_t size, ngx_log_t *log)<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: ngx_pool_t *p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: //以16(NGX_POOL_ALIGNMENT)字节对齐分配size内存<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: if (p == NULL) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: return NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: //初始状态:last指向ngx_pool_t结构体之后数据取起始位置<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: p->d.last = (u_char *) p + sizeof(ngx_pool_t);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 14: //end指向分配的整个size大小的内存的末尾<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 15: p->d.end = (u_char *) p + size;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 16: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 17: p->d.next = NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 18: p->d.failed = 0;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 19: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 20: size = size - sizeof(ngx_pool_t);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 21: //#define NGX_MAX_ALLOC_FROM_POOL (ngx_pagesize - 1),内存池最大不超过4095,x86中页的大小为4K<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 22: p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 23: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 24: p->current = p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 25: p->chain = NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 26: p->large = NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 27: p->cleanup = NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 28: p->log = log;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 29: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 30: return p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 31: }
nginx对内存的管理分为大内存与小内存,当某一个申请的内存大于某一个值时,就需要从大内存中分配空间,否则从小内存中分配空间。
nginx中的内存池是在创建的时候就设定好了大小,在以后分配小块内存的时候,如果内存不够,则是重新创建一块内存串到内存池中,而不是将原有的内存池进行扩张。当要分配大块内存是,则是在内存池外面再分配空间进行管理的,称为大块内存池。
4.2 内存申请 ngx_palloc
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1:void *<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_palloc(ngx_pool_t *pool, size_t size)<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: u_char *m;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: ngx_pool_t *p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: //如果申请的内存大小小于内存池的max值<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: if (size <= pool->max) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: p = pool->current;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: do {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: //对内存地址进行对齐处理<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 14: m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 15: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 16: //如果当前内存块够分配内存,则直接分配<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 17: if ((size_t) (p->d.end - m) >= size) <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 18: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 19: p->d.last = m + size;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 20: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 21: return m;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 22: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 23: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 24: //如果当前内存块有效容量不够分配,则移动到下一个内存块进行分配<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 25: p = p->d.next;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 26: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 27: } while (p);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 28: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 29: //当前所有内存块都没有空闲了,开辟一块新的内存,如下2详细解释<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 30: return ngx_palloc_block(pool, size);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 31: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 32: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 33: //分配大块内存<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 34: return ngx_palloc_large(pool, size);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 35: }
需要说明的几点:
1、ngx_align_ptr,这是一个用来内存地址取整的宏,非常精巧,一句话就搞定了。作用不言而喻,取整可以降低CPU读取内存的次数,提高性能。因为这里并没有真正意义调用malloc等函数申请内存,而是移动指针标记而已,所以内存对齐的活,C编译器帮不了你了,得自己动手。
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1: #define ngx_align_ptr(p, a) <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: (u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
2、开辟一个新的内存块 ngx_palloc_block(ngx_pool_t *pool, size_t size)
这个函数是用来分配新的内存块,为pool内存池开辟一个新的内存块,并申请使用size大小的内存;
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1:static void *<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_palloc_block(ngx_pool_t *pool, size_t size)<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: u_char *m;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: size_t psize;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: ngx_pool_t *p, *new;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: //计算内存池第一个内存块的大小<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: psize = (size_t) (pool->d.end - (u_char *) pool);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: //分配和第一个内存块同样大小的内存块<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: if (m == NULL) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 14: return NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 15: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 16: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 17: new = (ngx_pool_t *) m;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 18: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 19: //设置新内存块的end<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 20: new->d.end = m + psize;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 21: new->d.next = NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 22: new->d.failed = 0;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 23: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 24: //将指针m移动到d后面的一个位置,作为起始位置<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 25: m += sizeof(ngx_pool_data_t);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 26: //对m指针按4字节对齐处理<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 27: m = ngx_align_ptr(m, NGX_ALIGNMENT);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 28: //设置新内存块的last,即申请使用size大小的内存<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 29: new->d.last = m + size;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 30: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 31: //这里的循环用来找最后一个链表节点,这里failed用来控制循环的长度,如果分配失败次数达到5次,就忽略,不需要每次都从头找起<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 32: for (p = pool->current; p->d.next; p = p->d.next) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 33: if (p->d.failed++ > 4) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 34: pool->current = p->d.next;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 35: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 36: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 37: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 38: p->d.next = new;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 39: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 40: return m;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 41: }
3、分配大块内存 ngx_palloc_large(ngx_pool_t *pool, size_t size)
在ngx_palloc中首先会判断申请的内存大小是否超过内存块的最大限值,如果超过,则直接调用ngx_palloc_large,进入大内存块的分配流程;
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1:static void *<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_palloc_large(ngx_pool_t *pool, size_t size)<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: void *p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: ngx_uint_t n;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: ngx_pool_large_t *large;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: // 直接在系统堆中分配一块大小为size的空间<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: p = ngx_alloc(size, pool->log);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: if (p == NULL) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: return NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 14: n = 0;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 15: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 16: // 查找到一个空的large区,如果有,则将刚才分配的空间交由它管理 <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 17: for (large = pool->large; large; large = large->next) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 18: if (large->alloc == NULL) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 19: large->alloc = p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 20: return p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 21: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 22: //为了提高效率, 如果在三次内没有找到空的large结构体,则创建一个<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 23: if (n++ > 3) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 24: break;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 25: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 26: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 27: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 28: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 29: large = ngx_palloc(pool, sizeof(ngx_pool_large_t));<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 30: if (large == NULL) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 31: ngx_free(p);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 32: return NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 33: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 34: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 35: //将large链接到内存池<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 36: large->alloc = p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 37: large->next = pool->large;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 38: pool->large = large;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 39: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 40: return p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 41: }
整个内存池分配如下图:
4.3 内存池重置 ngx_reset_pool
<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1:void<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_reset_pool(ngx_pool_t *pool)<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: ngx_pool_t *p;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: ngx_pool_large_t *l;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: //释放大块内存<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: for (l = pool->large; l; l = l->next) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: if (l->alloc) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: ngx_free(l->alloc);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 14: // 重置所有小块内存区<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 15: for (p = pool; p; p = p->d.next) {<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 16: p->d.last = (u_char *) p + sizeof(ngx_pool_t);<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 17: p->d.failed = 0;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 18: }<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 19: <preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 20: pool->current = pool;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 21: pool->chain = NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 22: pool->large = NULL;<preCourier New',courier,monospace; width:870px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 23: }
4.4 内存池释放 ngx_pfree
<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 1: ngx_int_t<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 2: ngx_pfree(ngx_pool_t *pool, void *p)<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 3: {<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 4: ngx_pool_large_t *l;<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 5: <preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 6: //只检查是否是大内存块,如果是大内存块则释放<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 7: for (l = pool->large; l; l = l->next) {<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 8: if (p == l->alloc) {<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 9: ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 10: "free: %p", l->alloc);<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 11: ngx_free(l->alloc);<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 12: l->alloc = NULL;<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 13: <preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 14: return NGX_OK;<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 15: }<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 16: }<preCourier New',courier,monospace; width:950px; padding:0px; direction:ltr; margin:0em; line-height:12pt; background-color:rgb(244,244,244)"> 17: <preCourier New',courier,monospace; width:950px; padding:
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!