由于nginx中其他数据结构相对来说比较简单,我们还是看看核心的数据结构内存池
struct ngx_pool_s {
ngx_pool_data_t d; /* 内存池的数据区域*/
size_t max; /* 最大每次可分配内存 */
ngx_pool_t *current; /* 指向当前的内存池指针地址。ngx_pool_t链表上最后一个缓存池结构*/
ngx_chain_t *chain; /* 缓冲区链表 */
ngx_pool_large_t *large; /* 存储大数据的链表 */
ngx_pool_cleanup_t *cleanup; /* 可自定义回调函数,清除内存块分配的内存 */
ngx_log_t *log; /* 日志 */
};
typedef struct ngx_pool_s ngx_pool_t;
typedef struct {
u_char *last; /* 内存池中未使用内存的开始节点地址 */
u_char *end; /* 内存池的结束地址 */
ngx_pool_t *next; /* 指向下一个内存池 */
ngx_uint_t failed; /* 失败次数 */
} ngx_pool_data_t;
//大块数据内存池
struct ngx_pool_large_s {
ngx_pool_large_t *next; /* 指向下一个存储地址 通过这个地址可以知道当前块长度 */
void *alloc; /* 数据块指针地址 */
};
typedef struct ngx_pool_large_s ngx_pool_large_t;
//内存池回收
struct ngx_pool_cleanup_s {
ngx_pool_cleanup_pt handler; /* 清理的回调函数 */
void *data; /* 指向存储的数据地址 */
ngx_pool_cleanup_t *next; /* 下一个ngx_pool_cleanup_t */
};
基本的内存方式:
ngx_alloc和ngx_calloc 主要封装了Nginx的内存分配函数malloc。
void *
ngx_alloc(size_t size, ngx_log_t *log)
{
void *p;
p = malloc(size);
if (p == NULL) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
"malloc(%uz) failed", size);
}
ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, 0, "malloc: %p:%uz", p, size);
return p;
}
void *
ngx_calloc(size_t size, ngx_log_t *log)
{
void *p;
p = ngx_alloc(size, log);
if (p) {
//内存数据置为0
ngx_memzero(p, size);
}
return p;
}
内存池方式分配:
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);
if (p == NULL) {
return NULL;
}
//内存开始地址,指向ngx_pool_t结构体之后数据取起始位置
p->d.last = (u_char *) p + sizeof(ngx_pool_t);
p->d.end = (u_char *) p + size;
p->d.next = NULL;
p->d.failed = 0;
size = size - sizeof(ngx_pool_t);
p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;
p->current = p;
p->chain = NULL;
p->large = NULL;
p->cleanup = NULL;
p->log = log;
return p;
}
//销毁内存池
void
ngx_destroy_pool(ngx_pool_t *pool)
{
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);
}
}
#if (NGX_DEBUG)
/*
* we could allocate the pool->log from this pool
* so we cannot use this log while free()ing the pool
*/
for (l = pool->large; l; l = l->next) {
ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc);
}
for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
"free: %p, unused: %uz", p, p->d.end - p->d.last);
if (n == NULL) {
break;
}
}
#endif
for (l = pool->large; l; l = l->next) {
if (l->alloc) {
ngx_free(l->alloc);
}
}
for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
ngx_free(p);
if (n == NULL) {
break;
}
}
}
//重设内存池
void ngx_reset_pool(ngx_pool_t *pool) {
ngx_pool_t *p;
ngx_pool_large_t *l;
/* 清理pool->large链表(pool->large为单独的大数据内存块) */
for (l = pool->large; l; l = l->next) {
if (l->alloc) {
ngx_free(l->alloc);
}
}
pool->large = NULL;
/* 循环重新设置内存池data区域的 p->d.last;data区域数据并不擦除*/
for (p = pool; p; p = p->d.next) {
p->d.last = (u_char *) p + sizeof(ngx_pool_t);
}
}
使用内存池分配一块内存
void *
ngx_palloc(ngx_pool_t *pool, size_t size)
{
#if !(NGX_DEBUG_PALLOC)
if (size <= pool->max) {
//分配小内存
return ngx_palloc_small(pool, size, 1);
}
#endif
//分配大内存
return ngx_palloc_large(pool, size);
}
static ngx_inline void *
ngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)
{
u_char *m;
ngx_pool_t *p;
p = pool->current;
do {
m = p->d.last;
if (align) {
//对齐操作,会损失内存,提高内存使用速度
m = ngx_align_ptr(m, NGX_ALIGNMENT);
}
if ((size_t) (p->d.end - m) >= size) {
p->d.last = m + size;
return m;
}
p = p->d.next;
} while (p);
//如果没有缓存池空间没有可以容纳大小为size的内存块,则需要重新申请一个缓存池pool节点
return ngx_palloc_block(pool, size);
}
ngx_pnalloc方法则是非对齐操作分配内存
//大内存分配则是直接通过ngx_alloc分配,通过链表挂载到pool->large,所以后面的释放内存也由系统做
static void *
ngx_palloc_large(ngx_pool_t *pool, size_t size)
{
void *p;
ngx_uint_t n;
ngx_pool_large_t *large;
p = ngx_alloc(size, pool->log);
if (p == NULL) {
return NULL;
}
n = 0;
for (large = pool->large; large; large = large->next) {
if (large->alloc == NULL) {
large->alloc = p;
return p;
}
if (n++ > 3) {
break;
}
}
large = ngx_palloc_small(pool, sizeof(ngx_pool_large_t), 1);
if (large == NULL) {
ngx_free(p);
return NULL;
}
large->alloc = p;
large->next = pool->large;
pool->large = large;
return p;
}