概述
本節源碼來自 src/core/ngx_array.h/.c。Nginx 源碼的數組類似於前面介紹的《STL源碼剖析——序列容器之 vector》,在 Nginx 數組中,內存分配是基於內存池的,並不是固定不變的,也不是需要多少內存就申請多少,若當前內存不足以存儲所需元素時,按照當前數組的兩倍內存大小進行申請,這樣做減少內存分配的次數,提高效率。
數組數據結構
動態數組的數據結構定義如下:
typedef struct {
void *elts; /* 指向數組數據區域的首地址 */
ngx_uint_t 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);
創建新的動態數組:
創建數組的操作實現如下,首先分配數組頭,然後分配數組數據區,兩次分配均在傳入的內存池(pool指向的內存池)中進行。然後簡單初始化數組頭並返回數組頭的起始位置。
/* 創建動態數組對象 */
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;
}
return a;
}
/* 當一個數組對象被分配在堆上,且調用ngx_array_destroy之後,若想重新使用,則需調用該函數 */
/* 若數組對象被分配在棧上,則需調用此函數 */
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;
/* 移動內存池的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;
}
}
添加元素操作
數組添加元素的操作有兩個,ngx_array_push 和ngx_array_push_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;
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
*/
p->d.last += a->size;
a->nalloc++;
} else {
/* 若當前內存池不足以容納一個元素,則分配新的數組內存 */
/* 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;
}
/* 數組增加 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;
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;
測試程序:
#include "ngx_config.h"
#include <stdio.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_array(ngx_array_t* a)
{
if (a)
{
printf("array = 0x%x\n", a);
printf(" .elts = 0x%x\n", a->elts);
printf(" .nelts = %d\n", a->nelts);
printf(" .size = %d\n", a->size);
printf(" .nalloc = %d\n", a->nalloc);
printf(" .pool = 0x%x\n", a->pool);
printf("elements: ");
int *ptr = (int*)(a->elts);
for (; ptr < (int*)(a->elts + a->nalloc * a->size); )
{
printf("%d ", *ptr++);
}
printf("\n");
}
}
int main()
{
ngx_pool_t *pool;
int i;
printf("--------------------------------\n");
printf("create a new pool:\n");
printf("--------------------------------\n");
pool = ngx_create_pool(1024, NULL);
printf("--------------------------------\n");
printf("alloc an array from the pool:\n");
printf("--------------------------------\n");
ngx_array_t *a = ngx_array_create(pool, 5, sizeof(int));
for (i = 0; i < 5; i++)
{
int *ptr = ngx_array_push(a);
*ptr = 2*i;
}
dump_array(a);
ngx_array_destroy(a);
ngx_destroy_pool(pool);
return 0;
}
輸出結果:
$ ./test
--------------------------------
create a new pool:
--------------------------------
--------------------------------
alloc an array from the pool:
--------------------------------
array = 0x9fe2048
.elts = 0x9fe205c
.nelts = 5
.size = 4
.nalloc = 5
.pool = 0x9fe2020
elements: 0 2 4 6 8
參考資料:
《深入理解 Nginx》