【Nginx核心基础】内存池

         本节将研究Nginx关于内存申请与释放的核心代码; 

基本示意图

内存池对象初始状态

小内存申请后状态

大内存申请后状态

核心代码分析

核心结构体声明

1//大内存管理结构 2struct ngx_pool_large_s { 3 ngx_pool_large_t *next; //连接下一个大内存管理 4 void *alloc; //申请的大内存地址 5}; 6 7//内存池中数据管理 8typedef struct { 9 u_char *last; //可用内存的起始地址 10 u_char *end; //可用内存的末尾地址 11 ngx_pool_t *next; //指向下一个内存池 12 ngx_uint_t failed; //申请时,失败的次数 13} ngx_pool_data_t; 14 15//内存池 16struct ngx_pool_s { 17 ngx_pool_data_t d; //存放数据 18 size_t max; //存放数据的可用内存大小,最大为1页 19 ngx_pool_t *current; //指向分配内存的内存池 20 ngx_chain_t *chain; 21 ngx_pool_large_t *large; //连接大内存管理结构 22 ngx_pool_cleanup_t *cleanup; //清理对象头 23 ngx_log_t *log; 24}; 25

初始化内存池对象

1//创建一个size的内存池 2ngx_pool_t * 3ngx_create_pool(size_t size, ngx_log_t *log) 4{ 5 ngx_pool_t *p; 6 7 p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log); //以对齐的方式来申请size字节内存 8 if (p == NULL) { 9 return NULL; 10 } 11 12 p->d.last = (u_char *) p + sizeof(ngx_pool_t); //指向可用的内存起始地址 13 p->d.end = (u_char *) p + size; //指向可用内存的末尾地址 14 p->d.next = NULL; //初始时,下一个可用内存为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; //最大只能是一页大小 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} 28

内存池销毁和重设

1//销毁内存池 2void 3ngx_destroy_pool(ngx_pool_t *pool) 4{ 5 ngx_pool_t *p, *n; 6 ngx_pool_large_t *l; 7 ngx_pool_cleanup_t *c; 8 9 //运行清理对象的handler 10 for (c = pool->cleanup; c; c = c->next) { 11 if (c->handler) { 12 ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 13 "run cleanup: %p", c); 14 c->handler(c->data); 15 } 16 } 17 18 //释放大内存 19 for (l = pool->large; l; l = l->next) { 20 21 ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc); 22 23 if (l->alloc) { 24 ngx_free(l->alloc); //使用free释放malloc申请的内存 25 } 26 } 27 28#if (NGX_DEBUG) 29 30 /* 31 * we could allocate the pool->log from this pool 32 * so we cannot use this log while free()ing the pool 33 */ 34 35 for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 36 ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 37 "free: %p, unused: %uz", p, p->d.end - p->d.last); 38 39 if (n == NULL) { 40 break; 41 } 42 } 43 44#endif 45 46 //释放每一个申请的内存池对象ngx_pool_t 47 for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) { 48 ngx_free(p); 49 50 if (n == NULL) { 51 break; 52 } 53 } 54} 55 56//重设内存池 57void 58ngx_reset_pool(ngx_pool_t *pool) 59{ 60 ngx_pool_t *p; 61 ngx_pool_large_t *l; 62 63 //释放大内存 64 for (l = pool->large; l; l = l->next) { 65 if (l->alloc) { 66 ngx_free(l->alloc); 67 } 68 } 69 70 //内存池对象,仅仅改变last的指针位置 71 for (p = pool; p; p = p->d.next) { 72 p->d.last = (u_char *) p + sizeof(ngx_pool_t); //导致所有的内存池对象的可用内存的起始地址偏移都一样 73 p->d.failed = 0; 74 } 75 76 pool->current = pool; 77 pool->chain = NULL; 78 pool->large = NULL; 79} 80 81

分配内存

1//分配内存(地址对齐) 2void * 3ngx_palloc(ngx_pool_t *pool, size_t size) 4{ 5 u_char *m; 6 ngx_pool_t *p; 7 8 if (size <= pool->max) { //小内存申请时,以size为标准 9 10 p = pool->current; 11 12 do { 13 m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT); //首先将d.last地址对齐 14 15 if ((size_t) (p->d.end - m) >= size) { //可用的内存大于要申请的内存 16 p->d.last = m + size; //直接更新d.last 17 18 return m; //直接返回 19 } 20 21 p = p->d.next; //否则找下一个可用的内存池对象 22 23 } while (p); 24 25 //没有找到,则要申请新的内存池对象 26 return ngx_palloc_block(pool, size); 27 } 28 29 return ngx_palloc_large(pool, size); //大内存申请处理 30} 31 32//分配内存(地址可以不对齐) 33void * 34ngx_pnalloc(ngx_pool_t *pool, size_t size) 35{ 36 u_char *m; 37 ngx_pool_t *p; 38 39 if (size <= pool->max) { //小内存 40 41 p = pool->current; 42 43 do { 44 m = p->d.last; 45 46 if ((size_t) (p->d.end - m) >= size) { 47 p->d.last = m + size; 48 49 return m; 50 } 51 52 p = p->d.next; 53 54 } while (p); 55 56 return ngx_palloc_block(pool, size); //申请新内存池对象 57 } 58 59 return ngx_palloc_large(pool, size); //大内存 60} 61

小内存分配

1//申请新的内存池对象 2static void * 3ngx_palloc_block(ngx_pool_t *pool, size_t size) 4{ 5 u_char *m; 6 size_t psize; 7 ngx_pool_t *p, *new; 8 9 psize = (size_t) (pool->d.end - (u_char *) pool); //申请内存的总大小 10 11 m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log); //对齐方式申请内存 12 if (m == NULL) { 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); //只有一个ngx_pool_data_t,节省了ngx_pool_t的其余开销 23 m = ngx_align_ptr(m, NGX_ALIGNMENT); 24 new->d.last = m + size; //可用的内存的起始地址 25 26 //如果当前申请内存的失败的次数已经有5次了,第6次,current将会指向新的内存池对象 27 for (p = pool->current; p->d.next; p = p->d.next) { 28 if (p->d.failed++ > 4) { 29 pool->current = p->d.next; 30 } 31 } 32 33 p->d.next = new; //连接刚刚申请的内存池对象 34 35 return m; 36} 37 38

大内存分配

1//大内存申请处理 2static void * 3ngx_palloc_large(ngx_pool_t *pool, size_t size) 4{ 5 void *p; 6 ngx_uint_t n; 7 ngx_pool_large_t *large; 8 9 p = ngx_alloc(size, pool->log); //直接malloc申请内存 10 if (p == NULL) { 11 return NULL; 12 } 13 14 n = 0; 15 16 for (large = pool->large; large; large = large->next) { 17 if (large->alloc == NULL) { //如果有内存被释放了,可重用 18 large->alloc = p; 19 return p; 20 } 21 22 if (n++ > 3) { //但是只找4次,第5次直接break,创建大内存的管理结构 23 break; 24 } 25 } 26 27 large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); //从内存池对象申请内存 28 if (large == NULL) { 29 ngx_free(p); 30 return NULL; 31 } 32 33 large->alloc = p; //指向申请的大内存 34 35 //插入large的头 36 large->next = pool->large; 37 pool->large = large; 38 39 return p; 40} 41 42

直接分配内存

1//不管内存大小多大,向操作系统申请内存 2void * 3ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment) 4{ 5 void *p; 6 ngx_pool_large_t *large; 7 8 p = ngx_memalign(alignment, size, pool->log); //申请的内存 9 if (p == NULL) { 10 return NULL; 11 } 12 13 large = ngx_palloc(pool, sizeof(ngx_pool_large_t)); //申请一个大内存管理结构 14 if (large == NULL) { 15 ngx_free(p); 16 return NULL; 17 } 18 19 //放入到内存池ngx_pool_t中管理 20 large->alloc = p; //指向申请的内存 21 22 //插入到头部 23 large->next = pool->large; 24 pool->large = large; 25 26 return p; 27} 28 29

内存释放

1//释放内存 2ngx_int_t 3ngx_pfree(ngx_pool_t *pool, void *p) 4{ 5 ngx_pool_large_t *l; 6 7 //只释放大内存 8 for (l = pool->large; l; l = l->next) { 9 if (p == l->alloc) { 10 ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, 11 "free: %p", l->alloc); 12 ngx_free(l->alloc); 13 l->alloc = NULL; //置为空 14 15 return NGX_OK; 16 } 17 } 18 19 return NGX_DECLINED; 20} 21 22

代码交流 2021