1. 程式人生 > >菜鳥學習Nginx之ngx_array_t

菜鳥學習Nginx之ngx_array_t

在上一篇介紹了ngx_list_t,本篇介紹一下ngx_array_t,其實有了上一篇的基礎,在介紹ngx_array_t就比較簡單了。

一、資料結構

在資料結構上與ngx_list_t有一點點重複,如下所示:

typedef struct {
    void        *elts; /* 陣列的起始地址 */
    ngx_uint_t   nelts; /* 當前陣列已儲存元素量 即已經使用的 */
    size_t       size; /* 一個數組元素大小 */
    ngx_uint_t   nalloc; /* 分配n個元素 */
    ngx_pool_t  *pool; /* 代表從該記憶體池中分配陣列 */
} ngx_array_t;

從資料結構上來看,不是太複雜,比較好理解。

二、相關介面

/**
 * 建立陣列
 * @param p    記憶體池
 * @param n    陣列元素個數
 * @param 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;
}

/**
 * 陣列初始化
 * @param array 陣列
 * @param pool  記憶體池
 * @param n     陣列包含元素個數
 * @param size  一個數組元素所佔記憶體大小
 */
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;

    array->elts = ngx_palloc(pool, n * size);//分配記憶體
    if (array->elts == NULL) {
        return NGX_ERROR;
    }

    return NGX_OK;
}

/**
 * 銷燬一個數組
 * @param a  陣列
 * @describe 這個方法 是儘量回收
 */
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;
    }
}

/**
 * 新增一個元素
 * @param a 陣列
 * @return 返回可用地址  具體賦值操作由呼叫者負責
 */
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 */

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

三、總結

ngx_array_t相對簡單,但是使用頻率比較高,自己看一下程式碼就能搞定。

還有一些資料結構使用頻率也很高,例如ngx_str_t,ngx_hash_t,ngx_rbtree_t等。其中ngx_rbtree_t閱讀起來可能比較難於理解,不過好在網上有很多講解紅黑樹原理。ngx_hash_t也是常見實現方式網上也有很多,所以不打算在深入介紹了。

後面打算介紹Nginx核心內容,《菜鳥學習nginx之模組定義》