菜鸟nginx源码剖析数据结构篇(一)动态数组ngx_array_t
Author:Echo Chen(陈斌)
Email:chenb19870707@gmail.com
Blog:Blog.csdn.net/chen19870707
Date:October 20h, 2014
1.ngx_array优势和特点
ngx_array _t是一个顺序容器,支持达到数组容量上限时动态改变数组的大小,类似于STL中vector,具有以下特性:
- 下标直接索引,访问速度快
- 动态增长
- 由slab内存池统一管理分配出的内存,效率高
2.源代码位置
头文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_array.h
源文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_array.c
3.数据结构定义
typedef struct {
void *elts; //elts指向数组的首地址
ngx_uint_t nelts; //nelts是数组中已经使用的元素个数
size_t size; //每个数组元素占用内存大小
ngx_uint_t nalloc; //当前数组中能容纳袁术个数的总大小
ngx_pool_t *pool; //内存池对象
} ngx_array_t;
其结构如下图所示:
4.动态数组创建ngx_array_create和初始化ngx_array_init
//p为内存池,n为初始分配的元素的个数,size为每个元素占的内存大小
ngx_array_t * ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size) { ngx_array_t *a; //分配动态数组指针 a = ngx_palloc(p, sizeof(ngx_array_t)); if (a == NULL) { return NULL; } if (ngx_array_init(a, p, n, size) != NGX_OK) { return NULL; } return a; } static ngx_inline ngx_int_t ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size) { /* * set "array->nelts" before "array->elts", otherwise MSVC thinks * that "array->nelts" may be used without having been initialized */ //初始化数据 array->nelts = 0; array->size = size; array->nalloc = n; array->pool = pool; //分配n个大小为size的内存,elts指向首地址 array->elts = ngx_palloc(pool, n * size); if (array->elts == NULL) { return NGX_ERROR; } return NGX_OK; }
5.动态数组释放ngx_array_destroy
void
ngx_array_destroy(ngx_array_t *a)
{
ngx_pool_t *p;
p = a->pool;
//释放动态数组每个元素
if ((u_char *) a->elts + a->size * a->nalloc == p->d.last) {
p->d.last -= a->size * a->nalloc;
}
//释放动态数组首指针
if ((u_char *) a + sizeof(ngx_array_t) == p->d.last) {
p->d.last = (u_char *) a;
}
}
6.动态数组的添加元素操作ngx_array_push和ngx_array_push_n
1.ngx_array_push
//a为要添加到的动态数组的指针
void * ngx_array_push(ngx_array_t *a)
{
void *elt, *new;
size_t size;
ngx_pool_t *p;
//使用的和预先分配的个数相等,数组已满
if (a->nelts == a->nalloc) {
/* the array is full */
//再分配预分配nalloc个,现在就有2*nalloc个了
size = a->size * a->nalloc;
p = a->pool;
//如果内存池内存还够,直接从内存池分配,只分配一个
if ((u_char *) a->elts + size == p->d.last
&& p->d.last + a->size <= p->d.end)
{
/*
* the array allocation is the last in the pool
* and there is space for new allocation
*/
//内存池尾指针后移一个元素大小,分配内存一个元素,并把nalloc+1
p->d.last += a->size;
a->nalloc++;
//如果内存池内存不够了,分配一个新的数组,大小为两倍的nalloc
} else {
/* allocate a new array */
//内存分配
new = ngx_palloc(p, 2 * size);
if (new == NULL) {
return NULL;
}
//将以前的数组拷贝到新数组,并将数组大小设置为以前二倍
ngx_memcpy(new, a->elts, size);
a->elts = new;
a->nalloc *= 2;
}
}
//已分配个数+1 ,并返回之
elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts++;
return elt;
}
可以看到,当内存池有空间时,数组满后仅增加一个元素,当内存池没有未分配空间时,直接分配2*nalloc 个大小,有了内存池,比vector直接2n+1更加有效。
2.ngx_array_push_n
//a为要放入的数组,n为要放入的个数
void *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
{
void *elt, *new;
size_t size;
ngx_uint_t nalloc;
ngx_pool_t *p;
size = n * a->size;
//如果数组剩余个数不够分配
if (a->nelts + n > a->nalloc) {
/* the array is full */
p = a->pool;
//如果内存池中剩余的够分配n个元素,从内存池中分配
if ((u_char *) a->elts + a->size * a->nalloc == p->d.last
&& p->d.last + size <= p->d.end)
{
/*
* the array allocation is the last in the pool
* and there is space for new allocation
*/
p->d.last += size;
a->nalloc += n;
//如果内存池中剩余的不够分配n个元素
} else {
/* allocate a new array */
//当n比数组预分配个数nalloc大时,分配2n个,否则分配2*nalloc个
nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);
new = ngx_palloc(p, nalloc * a->size);
if (new == NULL) {
return NULL;
}
//拷贝以前元素,设置nalloc
ngx_memcpy(new, a->elts, a->nelts * a->size);
a->elts = new;
a->nalloc = nalloc;
}
}
//增加已分配个数,并返回
elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts += n;
return elt;
}
7.实战
研究开源代码的主要意义就在于理解设计的原理和适用的场合,并在合适的场合使用代码,如果单纯的分析代码,但都不能使用,肯定达不到学习的目的,这里就给出ngx_array的测试代码,希望多动手。
1: typedef struct
2: {
3: u_char *name;
4: int age;
5: }Student;
6:
7: ngx_array_t* pArray = ngx_array_create(cf->pool,1,sizeof(Student));
8:
9: Student *pStudent = ngx_array_push(pArray);
10: pStudent->age = 10;
11:
12: Students *pStudents = ngx_array_push_n(pArray,3);
13: pStudents->age = 1;
14: (pStudents + 1 )->age =2;
15: (pStudents + 2 )->age = 3;
16:
17: //遍历
18: Student *pStudent = pArray->elts;
19: ngx_uint_i = 0;
20: for(; i < pArray->nelts;i++)
21: {
22: a = pStudent + i;
23: //....
24: }
-
Echo Chen:Blog.csdn.net/chen19870707
-