Nginx進階資料結構源碼分析(三)-----鏈表

來源:互聯網
上載者:User
ngx_list_t是Nginx封裝的鏈表容器,使用的很頻繁。它有兩個結構體,ngx_list_t描述的是整個鏈表,而ngx_list_part_t只描述鏈表的一個元素。為了方便理解,我們可以將它稱為數組的鏈表。也就是是說,ngx_list_t是一個鏈表容器,而鏈表中的元素又是一個數組。事實上,ngx_list_part_t數組中的元素才是使用者需要儲存的東西。

這樣的結構表達方式有什麼樣的好處:

(1)鏈表中儲存的元素是靈活的,它可以是任何一種資料結構;

(2)鏈表元素需要佔用的記憶體由ngx_list_t管理,它已經通過數組分配好了;

(3)小塊的記憶體使用量鏈表訪問效率是低下的,使用數組通過位移量訪問記憶體則要高效的多。

ngx_list_t結構體的定義:

typedef struct {    ngx_list_part_t  *last;     //指向鏈表的最後一個數組元素
    ngx_list_part_t   part;     //鏈表的首個數組元素    size_t            size;     //每一個使用者要儲存的一個資料必須小於或等於size    ngx_uint_t        nalloc;   //表示數組元素的個數還是每個數組元素中的元素個數???問徐    ngx_pool_t       *pool;     //記憶體池對象} ngx_list_t;
ngx_list_part_t結構體的定義:

struct ngx_list_part_s {    void             *elts;     //指向數組的起始地址    ngx_uint_t        nelts;    //表示數組中已經用了多少個元素    ngx_list_part_t  *next;     //下一個鏈表元素的地址};
初始化數組鏈表:

static ngx_inline ngx_int_tngx_list_init(ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t n, size_t size)//初始化鏈表{    list->part.elts = ngx_palloc(pool, n * size);//申請第一個數組元素的記憶體???應該是申請整個數組鏈表的                                                 //記憶體 問徐,弄清楚了    if (list->part.elts == NULL) {        return NGX_ERROR;    }    list->part.nelts = 0;    //開始數組元素中只有0個元素    list->part.next = NULL;    list->last = &list->part;//指向第一個節點    list->size = size;    list->nalloc = n;    list->pool = pool;//記憶體池對象    return NGX_OK;}
建立數組鏈表:

ngx_list_t *ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size)//建立一個鏈表{    ngx_list_t  *list;    list = ngx_palloc(pool, sizeof(ngx_list_t));//申請ngx_list_t的記憶體    if (list == NULL) {        return NULL;    }    if (ngx_list_init(list, pool, n, size) != NGX_OK) {//調用初始化函數對ngx_list_t初始化        return NULL;    }    return list;}
添加新的元素:

void *ngx_list_push(ngx_list_t *l){    void             *elt;    ngx_list_part_t  *last;    last = l->last;    if (last->nelts == l->nalloc) {//判斷最後一個數組節點是否沒有空間了        /* the last part is full, allocate a new list part */        last = ngx_palloc(l->pool, sizeof(ngx_list_part_t));//再申請一個節點的記憶體        if (last == NULL) {            return NULL;        }        last->elts = ngx_palloc(l->pool, l->nalloc * l->size);//申請節點的儲存記憶體        if (last->elts == NULL) {            return NULL;        }        last->nelts = 0;        last->next = NULL;        l->last->next = last;//更新這兩個變數        l->last = last;    }    elt = (char *) last->elts + l->size * last->nelts;//返回可以插入元素的記憶體位址    last->nelts++;    return elt;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

以上就介紹了Nginx進階資料結構源碼分析(三)-----鏈表,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.