Nginx advanced data structure Source Code Analysis (2) ----- dynamic array

Source: Internet
Author: User
: This article mainly introduces Nginx advanced data structure Source Code Analysis (2) ----- dynamic array. For more information about PHP tutorials, see. Ngx_array_t is an ordered container, which is widely used in Nginx. It stores elements as arrays and supports changing the size of an array when the maximum array capacity is reached. It is similar to the vector container in C ++ and has built-in Nginx encapsulated memory pool. Therefore, the memory allocated by it is also applied for in the memory pool.

Ngx_array_t has the following three advantages;

(1) fast access;

(2) the number of allowed elements is uncertain;

(3) allocates the memory occupied by elements, which will be managed in a unified memory pool.

There are two ways to resize dynamic arrays:

(1) if the remaining space in the current memory pool is greater than or equal to the space to be added this time, this expansion will only expand the new space.

(2) if the remaining space in the current memory pool is smaller than the space to be added, the ngx_array_push method doubles the capacity of the original dynamic array. for ngx_array_push_n, the expansion quantity is determined by the parameter and the capacity of the original dynamic array.

Structure of the dynamic array:

Typedef struct {void * elts; // The first address ngx_uint_t nelts; // number of used elements size_t size; // the memory size occupied by each array element ngx_uint_t nalloc; // The total size of the number of elements ngx_pool_t * pool; // memory pool object} ngx_array_t;
Dynamic array initialization:

Static ngx_inline ngx_int_tngx_array_init (ngx_array_t * array, ngx_pool_t * pool, ngx_uint_t n, size_t size) // initialize the array {/** set "array-> nelts" before "array-> elts ", otherwise MSVC thinks * that "array-> nelts" may be used without having been initialized */array-> nelts = 0; // The first address is 0 array-> size = size; // memory size occupied by each element array-> nalloc = n; // number of allocated elements array-> pool = pool; // memory pool object // apply for a memory space of n * size array> elts = ngx_palloc (pool, n * size); if (array-> elts = NULL) {return NGX_ERROR;} return NGX_ OK ;}
Create a dynamic array:

Ngx_array_t * ngx_array_create (optional * p, ngx_uint_t n, size_t size) // create an array {ngx_array_t * a; a = ngx_palloc (p, sizeof (ngx_array_t )); // apply for the memory of the array itself if (a = NULL) {return NULL;} if (ngx_array_init (a, p, n, size )! = NGX_ OK) {// initialization, that is, applying for memory return NULL;} return a;} that can store elements ;}
Destroy dynamic arrays:

Voidngx_array_destroy (ngx_array_t * a) // destroy the array {ngx_pool_t * p; p = a-> pool; if (u_char *) a-> elts + a-> size * a-> nalloc = p-> d. last) {// release the memory for storing elements. Why do we need to judge ??? P-> d. last-= a-> size * a-> nalloc;} if (u_char *) a + sizeof (ngx_array_t) = p-> d. last) {// release node memory/Why ??? P-> d. last = (u_char *) ;}}
Add an element to the dynamic array:

Void * ngx_array_push (ngx_array_t * a) {void * elt, * new; size_t size; ngx_pool_t * p; if (a-> nelts = a-> nalloc) {// If the array is full, then... /* The array is full */size = a-> size * a-> nalloc; p = a-> pool; if (u_char *) a-> elts + size = p-> d. last // why do I add this equal sign to judge :?????? & P-> d. last + a-> size <= p-> d. end) // if this memory pool node still has free memory {/** the array allocation is the last in the pool * and there is space for new allocation */p-> d. last + = a-> size; a-> nalloc ++ ;} else {// if no, re-apply for a two-fold memory/* allocate a new array */new = ngx_palloc (p, 2 * size); if (new = NULL) {return NULL;} ngx_memcpy (new, a-> elts, size); // copy the original array elements to the new memory space a-> elts = new; a-> nalloc * = 2 ;}} elt = (u_char *) a-> elts + a-> size * a-> nelts; // add the new element a-> nelts ++; return elt ;}
Add n elements to the current dynamic array:

Void * ngx_array_push_n (ngx_array_t * a, ngx_uint_t n) // add n elements {void * elt, * new; size_t size; ngx_uint_t nalloc; ngx_pool_t * p; size = n * a-> size; if (a-> nelts + n> a-> nalloc) {// if the total number is greater than the number of array elements/* the array is full */p = a-> pool; if (u_char *) a-> elts + a-> size * a-> nalloc = p-> d. last // the equal sign still does not know why to judge ???? & P-> d. last + size <= p-> d. end) // if the memory pool node has the remaining memory, you can store the added elements {/** the array allocation is the last in the pool * and there is space for new allocation */p-> d. last + = size; a-> nalloc + = n;} else {/* allocate a new array */nalloc = 2 * (n> = a-> nalloc )? N: a-> nalloc); // in the number of elements to be added and the number of elements that can be stored in the original array, multiply the value by 2 new = ngx_palloc (p, nalloc * a-> size); // Apply for memory if (new = NULL) {return NULL;} ngx_memcpy (new, a-> elts, a-> nelts * a-> size); // copy the original element a-> elts = new; // update the two variables a-> nalloc = nalloc ;}} elt = (u_char *) a-> elts + a-> size * a-> nelts; // memory start address where elements can be stored a-> nelts + = n; // update return elt ;}



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces Nginx advanced data structure Source Code Analysis (2) ----- dynamic array, including some content, hope to be helpful to friends interested in PHP tutorials.

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.