ngx array 相关结构
typedef struct {
void *elts; /* 数组元素起始地址 */
ngx_uint_t nelts; /* 已使用的数组元素个数 */
size_t size; /* 数组元素大小 */
ngx_uint_t nalloc; /* 数组元素个数 */
ngx_pool_t *pool; /* 分配数组内存的内存池 */
} ngx_array_t;
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>
#include <ngx_array.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);
void dump_array(ngx_array_t *array);
int
main(int argc, char *argv[])
{
// 创建 pool, 并初始化
ngx_log_t *log = NULL;
ngx_pool_t *pool;
ngx_array_t *array, *array1;
ngx_str_t *str;
printf("size of pool: %lu\n", sizeof(ngx_pool_t));
printf("size of array: %lu\n", sizeof(ngx_array_t));
printf("size of str: %lu\n", sizeof(ngx_str_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));
pool = ngx_create_pool(1024, log);
dump_pool(pool);
// 创建 array
array = ngx_array_create(pool, 10, 100); // 5个30大小元素
dump_array(array);
dump_pool(pool);
str = (ngx_str_t *)ngx_array_push(array);
printf("%p\n", str);
ngx_str_set(str, "bei jing");
str = (ngx_str_t *)ngx_array_push(array);
printf("%p\n", str);
ngx_str_set(str, "shang hai");
dump_array(array);
dump_pool(pool);
// 创建 array
array1 = ngx_array_create(pool, 9, 100);
dump_array(array1);
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);
return;
}
void
dump_array(ngx_array_t *array)
{
ngx_uint_t i;
ngx_str_t *str;
printf("\x1b[32m > dump array: \x1b[0m\n");
printf("%10s : %-p\n", "elts", array->elts);
printf("%10s : %-lu\n", "nelts", array->nelts);
printf("%10s : %-lu\n", "size", array->size);
printf("%10s : %-lu\n", "nalloc", array->nalloc);
printf("%10s : %-p\n", "pool", array->pool);
for (i = 0; i < (array->nelts); i++) {
str = (ngx_str_t *)((u_char *)array->elts + i * array->size);
printf("%10s<%lu.%-p>", str->data, i, str);
}
printf("\n");
return;
}
2.执行
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_array.c
gcc -o ./t_ngx_array ./t_ngx_array.o ../../objs/src/core/ngx_string.o ../../objs/src/os/unix/ngx_alloc.o ../../objs/src/core/ngx_palloc.o ../../objs/src/core/ngx_array.o
3.结果
size of pool: 80
size of array: 40
size of str: 16
size of pool_large: 16
size of pool_data: 32
> dump pool:
d : 0x7fef0b002000
d->last : 0x7fef0b002050
d->end : 0x7fef0b002400
d->next : 0x0
d->failed : 0
max : 944
current : 0x7fef0b002000
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
> dump array:
elts : 0x7fef0ac02820
nelts : 0
size : 100
nalloc : 10
pool : 0x7fef0b002000
> dump pool:
d : 0x7fef0b002000
d->last : 0x7fef0b002088
d->end : 0x7fef0b002400
d->next : 0x0
d->failed : 0
max : 944
current : 0x7fef0b002000
chain : 0x0
large : 0x7fef0b002078
cleanup : 0x0
log : 0x0
0x7fef0ac02820
0x7fef0ac02884
> dump array:
elts : 0x7fef0ac02820
nelts : 2
size : 100
nalloc : 10
pool : 0x7fef0b002000
bei jing<0.0x7fef0ac02820> shang hai<1.0x7fef0ac02884>
> dump pool:
d : 0x7fef0b002000
d->last : 0x7fef0b002088
d->end : 0x7fef0b002400
d->next : 0x0
d->failed : 0
max : 944
current : 0x7fef0b002000
chain : 0x0
large : 0x7fef0b002078
cleanup : 0x0
log : 0x0
> dump array:
elts : 0x7fef0b002420
nelts : 0
size : 100
nalloc : 9
pool : 0x7fef0b002000
> dump pool:
d : 0x7fef0b002000
d->last : 0x7fef0b0020b0
d->end : 0x7fef0b002400
d->next : 0x7fef0b002400
d->failed : 0
next:
> dump pool:
d : 0x7fef0b002400
d->last : 0x7fef0b0027a4
d->end : 0x7fef0b002800
d->next : 0x0
d->failed : 0
max : 0
current : 0x0
chain : 0x0
large : 0x0
cleanup : 0x0
log : 0x0
------------------------------------
max : 944
current : 0x7fef0b002000
chain : 0x0
large : 0x7fef0b002078
cleanup : 0x0
log : 0x0
网友评论