Permalink
Browse files

ngx array

  • Loading branch information...
gxpisme committed Jan 16, 2019
1 parent 4bd1d14 commit 663a1aa7d885fd564c58d1a1092a20ebd08cad80
Showing with 29 additions and 8 deletions.
  1. +16 −2 src/core/ngx_array.c
  2. +13 −6 src/core/ngx_array.h
@@ -9,16 +9,19 @@
#include <ngx_core.h>


//创建动态数组对象
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;
}

//分配容量为n 动态数组数据区 并将其初始化
if (ngx_array_init(a, p, n, size) != NGX_OK) {
return NULL;
}
@@ -27,17 +30,20 @@ ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)
}


//销毁数组对象,即数组所占据的内存被内存池回收
void
ngx_array_destroy(ngx_array_t *a)
{
ngx_pool_t *p;

p = a->pool;

//移动内存池的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;
}
@@ -53,8 +59,9 @@ ngx_array_push(ngx_array_t *a)

if (a->nelts == a->nalloc) {

/* the array is full */
/* the array is full 数组满了 */

//所有元素占用的内存大小
size = a->size * a->nalloc;

p = a->pool;
@@ -63,6 +70,7 @@ ngx_array_push(ngx_array_t *a)
&& p->d.last + a->size <= p->d.end)
{
/*
* 若当前内存池的内存空间至少可容纳一个元素大小
* the array allocation is the last in the pool
* and there is space for new allocation
*/
@@ -71,22 +79,27 @@ ngx_array_push(ngx_array_t *a)
a->nalloc++;

} else {
/* allocate a new array */
/* allocate a new array 分配新的数组内存*/

//新的数组内存是现有的2倍
new = ngx_palloc(p, 2 * size);
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;
}

@@ -135,6 +148,7 @@ ngx_array_push_n(ngx_array_t *a, ngx_uint_t n)
}

elt = (u_char *) a->elts + a->size * a->nelts;
//加n个元素
a->nelts += n;

return elt;
@@ -13,21 +13,27 @@
#include <ngx_core.h>


// 动态数组
typedef struct {
void *elts;
ngx_uint_t nelts;
size_t size;
ngx_uint_t nalloc;
ngx_pool_t *pool;
void *elts; //elts指向数组的首地址
ngx_uint_t nelts; //nelts数组中已经使用的元素个数
size_t size; //每个数组元素占用的内存大小
ngx_uint_t nalloc; //当前数组中能够容纳元素的总大小
ngx_pool_t *pool; //内存池对象
} ngx_array_t;


//创建新的动态数组
ngx_array_t *ngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size);
//销毁数组对象,内存被内存池回收
void ngx_array_destroy(ngx_array_t *a);
//添加一个数组元素
void *ngx_array_push(ngx_array_t *a);
//添加n个数组元素
void *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n);


//初始化
static ngx_inline ngx_int_t
ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)
{
@@ -36,11 +42,13 @@ ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)
* 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;
@@ -49,5 +57,4 @@ ngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)
return NGX_OK;
}


#endif /* _NGX_ARRAY_H_INCLUDED_ */

0 comments on commit 663a1aa

Please sign in to comment.