ngx_list. The {C|H} structure is very simple, and if you have seen the previous array introduction, this section can be used around:
[CPP] View plaincopyprint?
- typedef struct ngx_list_part_s ngx_list_part_t;
- struct ngx_list_part_s {
- void *elts; //Data area pointer
- ngx_uint_t Nelts; //Data actual number
- ngx_list_part_t *next; //Next data pointer
- };
- typedef struct {
- ngx_list_part_t *last;
- ngx_list_part_t part; //Data section
- size_t size; //Single Data size
- ngx_uint_t Nalloc; //Preset number of data
- ngx_pool_t *pool; //memory pool to which it belongs
- } ngx_list_t;
Operation of List:
[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 partial memory space for list data
- if (List->part.elts = = NULL) {
- return NULL;
- }
- list->part.nelts = 0; ///Actual data number is 0
- List->part.next = NULL; //No next node
- List->last = &list->part; //The last one is the data itself
- list->size = size; //Initialize the size of each data
- List->nalloc = n; //Preset data volume
- List->pool = pool; //memory pool to which it belongs
- return list; //Return address
- //Here if you find it similar to creating an array, then you're getting started.
- }
- //Add data you can see and add an array basically the same, but also return the address to add the data, and then take action
- 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 was 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;
- }
[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 describes the Nginx source learning notes (11)-The basic container--ngx_list, including the content, I hope the PHP tutorial interested in a friend helpful.