ngx pool 相关结构
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; /* 分配失败次数,当>3时,将更新 current = d.next */
} 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; /* 当分配的 size 大于 max时,使用该结构 */
ngx_pool_cleanup_t *cleanup;
ngx_log_t *log; /* 日志指针 */
};
#define ngx_align_ptr(p, a) \
(u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
- 实例
#include <stdio.h>
#include <ngx_config.h>
#include <ngx_conf_file.h>
#include <nginx.h>
#include <ngx_core.h>
#include <ngx_string.h>
#include <ngx_palloc.h>
volatile ngx_cycle_t *ngx_cycle;
void ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
const char *fmt, ...)
{
}
void dump_pool(ngx_pool_t *pool);
int
main(int argc, char *argv[])
{
// 创建 pool, 并初始化
ngx_log_t *log = NULL;
ngx_pool_t *pool;
printf("size of pool: %lu\n", sizeof(ngx_pool_t));
printf("size of pool_large: %lu\n", sizeof(ngx_pool_large_t));
printf("size of pool_data: %lu\n", sizeof(ngx_pool_data_t));
printf("size of alignment: %lu\n", sizeof(unsigned long));
pool = ngx_create_pool(1024, log);
dump_pool(pool);
ngx_palloc(pool, 900);
dump_pool(pool);
ngx_palloc(pool, 50);
dump_pool(pool);
ngx_palloc(pool, 10);
dump_pool(pool);
ngx_palloc(pool, 10);
dump_pool(pool);
ngx_palloc(pool, 10);
dump_pool(pool);
// 销毁 pool
ngx_destroy_pool(pool);
return 0;
}
void
dump_pool(ngx_pool_t *pool)
{
printf("\x1b[32m > dump pool: \x1b[0m\n");
printf("%10s : %-p\n", "d", &(pool->d));
printf("%10s : %-p\n", "d->last", pool->d.last);
printf("%10s : %-p\n", "d->end", pool->d.end);
printf("%10s : %-p\n\n", "d->next", pool->d.next);
printf("%10s : %-lu\n", "d->failed", pool->d.failed);
if (pool->d.next) {
printf("next: \n");
dump_pool(pool->d.next);
printf("------------------------------------\n");
}
printf("%10s : %-lu\n", "max", pool->max);
printf("%10s : %-p\n", "current", pool->current);
printf("%10s : %-p\n", "chain", pool->chain);
printf("%10s : %-p\n", "large", pool->large);
printf("%10s : %-p\n", "cleanup", pool->cleanup);
printf("%10s : %-p\n", "log", pool->log);
printf("\n");
return;
}
- 执行
gcc -c -O -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value -Werror -g -I ../../objs/ -I ../os/unix/ -I../core/ -I /usr/local/opt/pcre/include/ -I../event/ -I../os/ ./t_ngx_pool.c
gcc -o ./t_ngx_pool ./t_ngx_pool.o ../../objs/src/core/ngx_string.o ../../objs/src/os/unix/ngx_alloc.o ../../objs/src/core/ngx_palloc.o
- 结果
size of pool: 80
size of pool_large: 16
size of pool_data: 32
size of alignment: 8
1.> dump pool:
d : 0x7fd57f802000
d->last : 0x7fd57f802050
d->end : 0x7fd57f802400
d->next : 0x0
d->failed : 0
max : 944
current : 0x7fd57f802000
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
2.> dump pool:
d : 0x7fd57f802000
d->last : 0x7fd57f8023d4
d->end : 0x7fd57f802400
d->next : 0x0
d->failed : 0
max : 944
current : 0x7fd57f802000
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
3.> dump pool:
d : 0x7fd57f802000
d->last : 0x7fd57f8023d4
d->end : 0x7fd57f802400
d->next : 0x7fd57f802400
d->failed : 0
next:
> dump pool:
d : 0x7fd57f802400
d->last : 0x7fd57f802452
d->end : 0x7fd57f802800
d->next : 0x0
d->failed : 0
max : 0
current : 0x0
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
------------------------------------
max : 944
current : 0x7fd57f802000
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
4.> dump pool:
d : 0x7fd57f802000
d->last : 0x7fd57f8023e2
d->end : 0x7fd57f802400
d->next : 0x7fd57f802400
d->failed : 0
next:
> dump pool:
d : 0x7fd57f802400
d->last : 0x7fd57f802452
d->end : 0x7fd57f802800
d->next : 0x0
d->failed : 0
max : 0
current : 0x0
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
------------------------------------
max : 944
current : 0x7fd57f802000
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
5.> dump pool:
d : 0x7fd57f802000
d->last : 0x7fd57f8023f2
d->end : 0x7fd57f802400
d->next : 0x7fd57f802400
d->failed : 0
next:
> dump pool:
d : 0x7fd57f802400
d->last : 0x7fd57f802452
d->end : 0x7fd57f802800
d->next : 0x0
d->failed : 0
max : 0
current : 0x0
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
------------------------------------
max : 944
current : 0x7fd57f802000
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
6.> dump pool:
d : 0x7fd57f802000
d->last : 0x7fd57f8023f2
d->end : 0x7fd57f802400
d->next : 0x7fd57f802400
d->failed : 0
next:
> dump pool:
d : 0x7fd57f802400
d->last : 0x7fd57f802462
d->end : 0x7fd57f802800
d->next : 0x0
d->failed : 0
max : 0
current : 0x0
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
------------------------------------
max : 944
current : 0x7fd57f802000
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0