ngx_list.{c|h} The structure is very simple. If you have read the previous array introduction, you can skip this section:
[cpp] view
plaincopyprint?
- typedefstruct ngx_list_part_s ngx_list_part_t;
-
ngx_uint_t nelts; //The actual number of data
- ngx_list_part_t *next; //Next data pointer
-
}; typedef
- struct {
- ngx_list_part_t *last;
- ngx_list_part_t part; //Data part
- ngx_uint_t nalloc; //Default number of data
- ngx_pool_t *pool; / /The memory pool to which
-
} ngx_list_t; list Operation:
- [cpp] view
plaincopyprint?
- //Create list
- 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)); //Allocate list memory space
- if (list == NULL) {
- return NULL;
- }
- list->part.elts = ngx_palloc(pool, n * size); //Allocate some memory space for list data
- if (list->part.elts == NULL) {
- list->part.nelts = 0; The actual number of data is 0
- list->part.next = NULL; list->last = &list->part; /The last one is the data itself
- list->size = size; list->nalloc = n; Amount of data
- list->pool = pool; return
list; - //If you find that it is similar to creating an array here, then it means you have started to get started
}
- //Add data You can see that it is basically the same as adding an array. It also returns the address of the data to be added, and then performs the operation
- void *
- ngx_list_push( ngx_list_t * l)
- {
-
void last = l->last;
-
if- (last- >nelts == l->nalloc) {
- last = ngx_palloc(l->pool, sizeof(ngx_list_part_t));
- NULL;
-
elts = ngx_palloc(l->pool, l->nalloc * l->size); return
- NULL;
- last->nelts;
- last->nelts++;
-
return
- elt; [cpp] view
plaincopyprint?
//How to traverse list-
part = &list.part;
-
data = part->elts;
-
for- (i = 0 ;; i++) {
-
-
if-
(i >= part->nelts) {
if- (part->next == NULL ) {
- break;
-
}
Part = part->next;
Data = part->elts;
- i = 0; }
-
- ... data[i]...
-
-
The above introduces the nginx source code study notes (11) - basic container - ngx_list, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.