Nginx dynamic array ngx_array_t

Source: Internet
Author: User
: This article mainly introduces nginx dynamic array ngx_array_t. if you are interested in the PHP Tutorial, refer to it. Ngx_array_t is a dynamic array designed in nginx, similar to the vector in STL . The following is an example analysis.

I. instances

# Include
  
   
# Include "ngx_config.h" # include "ngx_conf_file.h" # include "nginx. h "# include" ngx_core.h "# include" ngx_string.h "# include" ngx_palloc.h "# include" volume "volatile cycle * ngx_cycle; void cycle (volume level, ngx_log_t * log, ngx_err_t err, const char * fmt ,...) {} void dump_pool (ngx_pool_t * pool) {while (pool) {printf ("pool = 0x % x \ n", pool); printf (". d \ n "); printf (". last = 0x % x \ n ", pool-> d. last); printf (". end = 0x % x \ n ", pool-> d. end); printf (". next = 0x % x \ n ", pool-> d. next); printf (". failed = % d \ n ", pool-> d. failed); printf (". max = % d \ n ", pool-> max); printf (". current = 0x % x \ n ", pool-> current); printf (". chain = 0x % x \ n ", pool-> chain); printf (". large = 0x % x \ n ", pool-> large); printf (". cleanup = 0x % x \ n ", pool-> cleanup); printf (". log = 0x % x \ n ", pool-> log); printf (" available pool memory = % d \ n ", pool-> d. end-pool-> d. last); ngx_pool_large_t * large = pool-> large; printf ("****** large_pool ******* \ n"); while (large) {printf ("% p->", large); large = large-> next;} printf ("\ n"); pool = pool-> d. next ;}} typedef struct {intarray [128]; // 128*4 = 512} TestNode; int main () {ngx_pool_t * pool; printf ("-------------------------------- \ n "); printf ("create a new pool: \ n"); printf ("-------------------------------- \ n"); pool = ngx_create_pool (1024, NULL); dump_pool (pool ); ngx_array_t * myArray = ngx_array_create (pool, 1, sizeof (TestNode )); printf ("******** ngx_array_create *********** \ n"); dump_pool (pool); TestNode * t1 = ngx_array_push (myArray ); testNode * t2 = ngx_array_push (myArray); printf ("******** ngx_array_push *********** \ n"); dump_pool (pool ); ngx_array_destroy (myArray); // nothing is done here dump_pool (pool); ngx_destroy_pool (pool); return 0 ;}
  

Running result:

--------------------------------create a new pool:--------------------------------pool = 0x95ae020  .d   .last = 0x95ae048   .end = 0x95ae420   .next = 0x0   .failed = 0 .max = 984 .current = 0x95ae020  .chain= 0x0 .large = 0x0 .cleanup = 0x0 .log = 0x0available pool memory = 984 *****large_pool*******NULL******ngx_array_create**********pool = 0x95ae020  .d   .last = 0x95ae25c   .end = 0x95ae420   .next = 0x0   .failed = 0 .max = 984 .current = 0x95ae020 .chain = 0x0 .large = 0x0 .cleanup = 0x0 .log = 0x0available pool memory = 452 *****large_pool*******NULL******ngx_array_push**********pool = 0x95ae020  .d   .last = 0x95ae264   .end = 0x95ae420   .next = 0x0    .failed = 0 .max = 984 .current = 0x95ae020 .chain = 0x0 .large = 0x95ae25c .cleanup = 0x0 .log = 0x0available pool memory = 444 *****large_pool*******0x95ae25c->NULL******ngx_array_destroy******pool = 0x95ae020  .d   .last = 0x95ae264   .end = 0x95ae420   .next = 0x0   .failed = 0 .max = 984 .current = 0x95ae020 .chain = 0x0 .large = 0x95ae25c .cleanup = 0x0 .log = 0x0available pool memory = 444 *****large_pool*******0x95ae25c->NULL

1. we can see from the changes in the available pool memory that the memory occupied by the ngx_array_t and ngx_pool_large_t struct is allocated to the memory pool.

It can be proved from the source code:

Ngx_array_t *

Ngx_array_create (ngx_pool_t * p, ngx_uint_t n, size_t size)

{

A = ngx_palloc (p, sizeof (ngx_array_t ));

}

Static void *

Ngx_palloc_large (ngx_pool_t * pool, size_t size)

{

// Allocate it on the memory pool.

Large = ngx_palloc (pool, sizeof (ngx_pool_large_t ));

}

2. if ngx_array_push is scaled up, the occupied memory will not be released. You can refer to the source code of ngx_array_push and will not post it here.

3. if the size of the allocated dynamic array exceeds the capacity of a memory pool (1024 in this example), ngx_palloc_large will be called to allocate large memory blocks.

4. if the memory occupied by the dynamic array is a large block of memory, ngx_array_destroy will not do anything, and this API has not been called in the nginx kernel source code.

For compilation, refer to the previous article on analyzing the ngx_queue_t struct.

II. references:

Tao Hui, an in-depth understanding of nginx

Http://blog.csdn.net/livelylittlefish/article/details/6586946

The above describes the nginx dynamic array ngx_array_t, including the content, hope to be helpful to friends who are 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.