Complete nginx source code annotation (3) ngx_list.h/ngx_list.c

Source: Internet
Author: User
List header files ngx_list.h
# Ifndef _ ngx_list_h_included _ # DEFINE _ ngx_list_h_included _ # include <ngx_config.h> # include <ngx_core.h> typedef struct ngx_list_part_s ngx_list_part_t; // a part is equivalent to a node in the list, struct ngx_list_part_s {void * ELTs; // data storage area, ngx_uint_t nelts; // Number of stored elements, ngx_list_part_t * Next; // next part }; // The List defines typedef struct {ngx_list_part_t * last; // The position ngx_list_part_t part Of the last part; // the header size_t size; ngx_uint_t nalloc; // list the maximum number of elements of a single node ngx_pool_t * pool; // memory pool} ngx_list_t; ngx_list_t * ngx_list_create (ngx_pool_t * Pool, ngx_uint_t N, size_t size ); static ngx_inline ngx_int_tngx_list_init (ngx_list_t * List, ngx_pool_t * Pool, ngx_uint_t N, size_t size) {// assign N * size to the data storage area of the first element of list-> part. ELTs = ngx_palloc (pool, N * size); If (list-> part. ELTs = NULL) {return ngx_error;} List-> part. nelts = 0; // The number of saved elements is 0 list-> part. next = NULL; List-> last = & list-> part; // currently available, that is, the header node list-> size = size; // list the size (number of bytes) of each element of each node list-> nalloc = N; // list the maximum number of elements of a single node list-> pool = pool; // memory pool return ngx_ OK;} // each node in the list is of the same size (the same number of elements and the same size of each element)/*** the iteration through the 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]... **} */void * ngx_list_push (ngx_list_t * List); # endif/* _ ngx_list_h_included _*/
List source files ngx_list.c
# Include <ngx_config.h> # include <ngx_core.h> ngx_list_t * ngx_list_create (ngx_pool_t * Pool, limit N, size_t size) {ngx_list_t * List; // list definition list = ngx_palloc (pool, sizeof (ngx_list_t); If (list = NULL) {return NULL;} // list-> part. ELTs = ngx_palloc (pool, N * size); If (list-> part. ELTs = NULL) {return NULL;} // The number of saved elements on the table header node is 0 list-> part. nelts = 0; List-> part. next = NULL; List-> last = & list-> part; List-> size = size; List-> nalloc = N; List-> pool = pool; // return to the created list return list;} // insert an element to the list void * ngx_list_push (ngx_list_t * l) {void * ELT; ngx_list_part_t * last; last = L-> last; // The number of existing element prime numbers of the last node = the maximum number of elements of a single node in the list, if (last-> nelts = L-> nalloc) {/* the last part is full, allocate a new list part * // from the memory pool of the list, allocate a node to the last = ngx_palloc (L-> pool, sizeof (ngx_list_part_t )); if (last = NULL) {return NULL;} // The memory pool from the list, allocate space to the last node's data storage zone last-> ELTs = ngx_palloc (L-> pool, L-> nalloc * l-> size ); if (last-> ELTs = NULL) {return NULL;} // The number of existing elements in the last node is 0. Last-> nelts = 0; last-> next = NULL; // create a new node and connect it to L-> last-> next = last; // treat the new node as the last node l-> last = last ;} // the start position of the data storage area of the last node + element size * Number of saved elements of the last node // the location where the next element can be placed ELT = (char *) last-> ELTs + L-> size * Last-> nelts; // Add 1 last-> nelts ++ to the existing element Prime Number of the last node; // return the position of the next element that can be placed. Return ELT ;}

From the above, we can see that "CREATE" is the memory allocated from the pool to define the list structure and allocated to the header node. Init is to initialize the existing list.

Complete nginx source code annotation (3) ngx_list.h/ngx_list.c

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.