Nginx High-level data structure source Analysis (iii)-----linked list

Source: Internet
Author: User
ngx_list_t is a linked list container in Nginx package, which is used very frequently. It has two structures, ngx_list_t describes the entire list, and ngx_list_part_t only describes one element of the linked list. For ease of understanding, we can refer to it as an array of linked lists. That is to say, ngx_list_t is a list container, and the element in the list is an array. In fact, the elements in the ngx_list_part_t array are what the user needs to store.

What are the benefits of such a structure expression:

(1) The element stored in the linked list is flexible, it can be any kind of data structure;

(2) The memory that the list element needs to occupy is managed by ngx_list_t, it has been allocated through the array;

(3) Small chunks of memory using the linked list of access efficiency is low, using an array to access memory through the offset is much more efficient.

Definition of ngx_list_t structure:

typedef struct {    ngx_list_part_t  *last;     Point to the last array element of the list
    ngx_list_part_t part   ;     The first array element of the list is    size_t            size;     One data to be stored by each user must be less than or equal to size    ngx_uint_t        nalloc;   Indicates the number of elements in an array or the number of elements in each array element??? Ask Xu    ngx_pool_t       *pool;     Memory Pool Object} ngx_list_t;
Definition of ngx_list_part_t structure:

struct ngx_list_part_s {    void             *elts;     The starting address of the pointer to the array    ngx_uint_t        nelts;    Indicates how many elements have been used in the array    ngx_list_part_t  *next;     The address of the next linked list element};
Initialize the list of arrays:

Static Ngx_inline Ngx_int_tngx_list_init (ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t N, size_t size)//Initialize linked list {    List->part.elts = Ngx_palloc (pool, n * size);//Request the memory of the first array element??? Should be to apply the entire array list of                                                 //memory Ask Xu, figure out    if (list->part.elts = = NULL) {        return ngx_error;    }    list->part.nelts = 0;    There are only 0 elements in the start array element    list->part.next = NULL;    List->last = &list->part;//points to the first node    list->size = size;    List->nalloc = n;    List->pool = pool;//Memory Pool object    return NGX_OK;}
To create an array-linked list:

ngx_list_t *ngx_list_create (ngx_pool_t *pool, ngx_uint_t N, size_t size)//Create a linked list {    ngx_list_t  *list;    List = Ngx_palloc (pool, sizeof (ngx_list_t));//Application for ngx_list_t memory    if (list = = null) {        return null;    }    if (Ngx_list_init (list, pool, n, size)! = NGX_OK) {//Call initialization function to ngx_list_t initialize        return NULL;    }    return list;}
To add a new element:

void *ngx_list_push (ngx_list_t *l) {    void             *elt;    ngx_list_part_t  *last;    last = l->last;    if (last->nelts = = L->nalloc) {//To determine if the last array node is out of space */The end part was full        , allocate a new list part */        Las t = ngx_palloc (l->pool, sizeof (ngx_list_part_t));//apply a node's memory        if (last = = null) {            return null;        }        Last->elts = Ngx_palloc (L->pool, L->nalloc * l->size);//The storage memory of the application node        if (last->elts = = NULL) {            return NULL;        }        last->nelts = 0;        Last->next = NULL;        L->last->next = last;//Update the two variables        l->last = last;    }    ELT = (char *) Last->elts + l->size * last->nelts;//returns the memory address where the element can be inserted    last->nelts++;    return ELT;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the Nginx high-level data structure source analysis (c)-----linked list, including the aspects of the content, I hope to be interested in the PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.