天天看點

7.nginx源碼分析之資料結構:ngx_array_t

nginx源碼分析之資料結構:ngx_array_t

頭檔案定義

ngx_array_t是一個數組結構,實作的非常精簡。結構體的定義如下所示:

typedef struct {
    void        *elts;
    ngx_uint_t   nelts;
    size_t       size;
    ngx_uint_t   nalloc;
    ngx_pool_t  *pool;
} ngx_array_t;
           

elts 是實際存儲的其實位置;

nelts 數組實際元素個數;

size 單個數組元素的大小;

nalloc 數組的容量,如果超過該容量會觸發數組進行擴容;

pool 數組申請所在的記憶體池指針(可能是連結清單);

如圖所示:

關于數組的接口生命如下所示:

//數組的建立、銷毀(并沒有真正的銷毀數組)、添加一個元素和添加n個元素
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);
void *ngx_array_push_n(ngx_array_t *a, ngx_uint_t n);

           

接口實作

ngx_array_create 數組建立

關于上述接口的實作在ngx_array.c中:

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個size大小的成員空間
    if (ngx_array_init(a, p, n, size) != NGX_OK) {
        return NULL;
    }

    return a;
}
           

關于ngx_array_init的實作如下所示,這個是一個靜态的函數:

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

ngx_array_destroy數組的銷毀

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

    p = a->pool;   //數組所存儲的記憶體池

    //這裡的銷毀并沒有真正的釋放記憶體,隻是把數組所對應的記憶體池的last指針向前移動了a->size * a->nalloc個位元組
    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向數組中添加一個元素

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;   //指向數組對應的記憶體池

        //記憶體池的數量足夠擴充數組為2倍空間,則直接擴充
        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,  * size);
            if (new == NULL) {
                return NULL;
            }

            ngx_memcpy(new, a->elts, size);
            a->elts = new;
            a->nalloc *= ;
        }
    }

    //數組空間充裕直接進行申請
    elt = (u_char *) a->elts + a->size * a->nelts;
    a->nelts++;

    //傳回申請元素的首位址
    return elt;
}
           

ngx_array_push_n向數組中添加n個元素

添加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 =  * ((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;
}
           

小結

ngx_array_t是nginx中一個比較精簡的資料結構,希望大家快速掌握。

繼續閱讀