【Nginx 源码学习】动态数组_数组


文章目录

  • 结构图
  • 结构定义
  • 创建数组
  • 销毁数组
  • 添加元素操作


结构图

【Nginx 源码学习】动态数组_数据区_02

1、Nginx的数组只存储比较小的数据

2、数组的元素长度在创建数组的时候就固定死了。但是数组个数,会自动扩容。

3、数组的数据结构和元素内存都会分配在Nginx的pool内存池上。

4、数组回收会去检查pool内存池,看是否可以将数组内存交还给内存池。


结构定义

/* 数组Array数据结构 */
typedef struct {
void *elts; /* 指向数组第一个元素指针*/
ngx_uint_t nelts; /* 未使用元素的索引*/
size_t size; /* 每个元素的大小,元素大小固定*/
ngx_uint_t nalloc; /* 分配多少个元素 */
ngx_pool_t *pool; /* 内存池*/
} ngx_array_t;

创建数组

/**
* 初始化一个数组
* p:内存池容器
* n:支持多少个数组元素
* size:每个元素的大小
*/
ngx_array_t *
ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
{
ngx_array_t *a;

/* 在内存池 pool上面 分配一段内存给 ngx_array数据结构*/
a = ngx_palloc(p, sizeof(ngx_array_t));
if (a == NULL) {
return NULL;
}

/**
* 数组初始化,并且分配内存空间给数组元素
* PS:这边数组的数据结构和数组元素的存储分成了两次在pool上分配,笔者认为可以一次进行分配
* 但是Nginx是多进程的,程序执行流程是线性的,所以分两次分配也无伤大雅。
*/
if (ngx_array_init(a, p, n, size) != NGX_OK) {
return NULL;
}

return a;
}

创建数组后内存池的结构如下:

【Nginx 源码学习】动态数组_数组元素_03

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
*/

/* 初始化数组成员,注意:nelts必须比elts先初始化 */
array->nelts = 0;
array->size = size;
array->nalloc = n;
array->pool = pool;

/* 分配数组数据域所需要的内存 */
array->elts = ngx_palloc(pool, n * size);
if (array->elts == NULL) {
return NGX_ERROR;
}

return NGX_OK;
}

销毁数组

销毁动作实际上就是修改内存池的 last 指针,即数组的内存被内存池回收,并没有调用 free 等释放内存的操作。

void
ngx_array_destroy(ngx_array_t *a)
{
ngx_pool_t *p;

p = a->pool;

/*
* 如果数组元素的末尾地址和内存池pool的可用开始的地址相同
* 则将内存池pool->d.last移动到数组元素的开始地址,相当于清除当前数组的内容
*/
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;
}
}

有点奇怪哈。。。

那个调整 last 的条件有点苛刻了吧???

这能进入分支么?

欲知后事如何,且听下回分解哈哈


添加元素操作

实际的添加操作并不在这两个函数中完成,例如ngx_array_push返回可以在该数组数据区中添加这个元素的位置,ngx_array_push_n则返回可以在该数组数据区中添加n个元素的起始位置,而添加操作即在获得添加位置之后进行

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 */

size = a->size * a->nalloc; //这是干嘛,有点猛啊,哦,size是局部变量

p = a->pool;

if ((u_char *) a->elts + size == p->d.last
&& p->d.last + a->size <= p->d.end) //还没弹尽粮绝呢?那也不对啊,怎么是小等于,那不就会山穷水尽吗
{ //除非,这个end指的是最后一块内存,而不是最后一位地址
/*
* the array allocation is the last in the pool
* and there is space for new allocation
*/

p->d.last += a->size;
a->nalloc++;

} else {
/* allocate a new array */

new = ngx_palloc(p, 2 * size); //否则,扩展数组数据区为原来的2倍
if (new == NULL) {
return NULL;
}

ngx_memcpy(new, a->elts, size); //将原来数据区的内容拷贝到新的数据区
a->elts = new;
a->nalloc *= 2; //转移数据后,并未释放原来的数据区,内存池将统一释放
}
}

elt = (u_char *) a->elts + a->size * a->nelts;
a->nelts++;

return elt; //返回该末尾指针,即下一个元素应该存放的位置
}


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;

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;

} else {
/* allocate a new array */

nalloc = 2 * ((n >= a->nalloc) ? n : a->nalloc);

new = ngx_palloc(p, nalloc * a->size);
if (new == NULL) {
return NULL;
}

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;
}

向数组中添加元素实际上也是在修该内存池的last指针(若数组数据区满)及数组头信息,即使数组满了,需要扩展数据区内容,也只需要内存拷贝完成,并不需要数据的移动操作,这个效率也是相当高的。