Nginx Dynamic Array ngx_array_t

Source: Internet
Author: User
ngx_array_t is a dynamic array designed in Nginx, similar to the vector in STL . Below we combine the example analysis.

First, the example

#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 "ngx_queue.h" volatile ngx_cycle_t *ngx_cycle; void Ngx_log_error_core (ngx_uint_t 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\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];//* 4 = 512}testnode;     int main () {ngx_pool_t *pool;    printf ("--------------------------------\ n");    printf ("Create a new pool:\n");    printf ("--------------------------------\ n");    Pool = Ngx_create_pool (1024x768, 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);//There is nothing to do here dump_pool (pool);    Ngx_destroy_pool (pool);return 0; }
  

Operation 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. Las t = 0x95ae25c. End = 0x95ae420. Next = 0x0. Failed = 0. Max = 984. Current = 0x95ae020. Chain = 0x0. Large = 0x0. Cl   Eanup = 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. Lar GE = 0x95ae25c. Cleanup = 0x0. log = 0x0available pool memory = 444 *****large_pool*******0x95ae25c->null******ngx_arra  Y_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, from the available pool memory changes can be learned that the ngx_array_t, ngx_pool_large_t structure of the body itself is allocated memory on the memory pool.

From the source can be proven:

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)

{

Allocated on the memory pool.

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

}

2, Ngx_array_push if the expansion, and will not release the original occupied memory. can refer to the source code of Ngx_array_push, not posted here.

3. If the size of the allocated dynamic array exceeds the capacity of a memory pool (in this case, 1024), Ngx_palloc_large is called to allocate large chunks of memory.

4, if the dynamic array of memory is a chunk of memory, Ngx_array_destroy will not do anything, and the API in the Nginx kernel source code has not been called.

Compiling can refer to the previous analysis of the ngx_queue_t structure of the article.

Second, reference materials:

"Deep understanding of Nginx" Tao Hui

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

The above describes the Nginx dynamic array ngx_array_t, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.