Linked list structure
ngx_list_t is a linked list container in Nginx package, and the memory allocation of the list container is based on the memory pool, which is easy to operate and high in efficiency. The Nginx linked list container is similar to the normal linked list, and has both a linked list header and a linked list node, which forms a linked list through node pointers. Its structure is defined as follows:
/* Linked list structure */typedef struct ngx_list_part_s ngx_list_part_t;/* node structure in a linked list */struct ngx_list_part_s { void *elts ; /* points to the first address of the data area of the node */ ngx_uint_t nelts;/* The number of elements that the node data area actually holds */ ngx_list_part_t *next;/* points to the next node in the list */};/* List header structure */typedef struct { ngx_list_part_t *last;/* point to the last node in the list */ ngx_list_part_t part;/ * The table header in the list contains the first node */ size_t size;/* element's byte size */ ngx_uint_t nalloc;/* The number of elements each node can hold in the list */ngx_ pool_t *pool;/* The memory pool object of the linked table node space */} ngx_list_t;
The linked list data structure looks like this:
Linked list operations
There are only two Nginx list operations: Create linked lists and add elements.
Create a linked list
When a new linked list is created, the list header is assigned first, the linked list is initialized, and the head node data area memory is allocated during initialization.
/* Create linked list */ngx_list_t *ngx_list_create (ngx_pool_t *pool, ngx_uint_t N, size_t size) { ngx_list_t *list; /* Allocate the memory of the list header * /list = Ngx_palloc (pool, sizeof (ngx_list_t)); if (list = = null) { return null; } /* Initialize the linked list * /if (Ngx_list_init (list, pool, n, size)! = NGX_OK) { return NULL; } return list;} /* Initialize linked list */static ngx_inline ngx_int_tngx_list_init (ngx_list_t *list, ngx_pool_t *pool, ngx_uint_t N, size_t size) { /* Allocates node data area memory and returns the first address of the data area of the node * /list->part.elts = Ngx_palloc (pool, n * size); if (list->part.elts = = NULL) { return ngx_error; } /* Initialize node member */ list->part.nelts = 0; List->part.next = NULL; List->last = &list->part; list->size = size; List->nalloc = n; List->pool = pool; return NGX_OK;}
adding elements
When adding an element to a linked list, it starts from the last node and first determines whether the data area of the last node is stored with the newly added element, if it is sufficient to store the new element, returns the location where the new element memory is stored, and if there is not enough memory to store the newly added element, a new node is allocated. The new node is then connected to the existing list and returns the location where the new element's memory is stored. Note: The added element can be an integer, or it can be a struct.
/* Add an element */void *ngx_list_push (ngx_list_t *l) {void *elt; ngx_list_part_t *last; /* The last node pointer points to the final node of the list */previous = l->last; /* If the data area of the last node is already full */if (last->nelts = = L->nalloc) {/* The end of the previous part is fully, allocate a new list part */ /* Assigns a new node */last = Ngx_palloc (L->pool, sizeof (ngx_list_part_t)); if (last = = null) {return null; }/* Allocates new node data area memory and points the node structure to the first address of the data region */Last->elts = Ngx_palloc (L->pool, L->nalloc * l->size); if (last->elts = = null) {return null; }/* Initializes the new node structure */last->nelts = 0; Last->next = NULL; /* Connect the new node to the existing list */L->last->next = last; L->last = Last; }/* Calculates where the new element is stored */ELT = (char *) Last->elts + l->size * last->nelts; last->nelts++; /* Actual storage element plus 1 */* Returns the location of the new element */return ELT;}
Resources:
"Deep understanding of Nginx"
Nginx Linked list Structure ngx_list_t