ngx_array_t is a dynamic array designed in nginx, similar to vector
1. Example
<span style="font-size:18px;">#include <stdio.h> #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\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);// 这里什么也没做 dump_pool(pool); ngx_destroy_pool(pool); return 0; }</span>
Operation results:
-------------------------------- 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 = 0x0 available 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 = 0x0 available 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 = 0x0 available 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 = 0x0 available pool memory = 444 *****large_pool******* 0x95ae25c->NULL
1. From the changes in available pool memory, we can know that the memory occupied by the ngx_array_t and ngx_pool_large_t structures themselves is allocated on 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);
large = ngx_palloc(pool,sizeof(ngx_pool_large_t));
}
2. If ngx_array_push is expanded, the original occupied memory will not be released. You can refer to the source code of ngx_array_push, which will not be 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 will be called to allocate a large block of memory.
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, please refer to the previous article analyzing the ngx_queue_t structure.
2. References:
"In-depth understanding of nginx" Tao Hui
http://blog.csdn.net/livelylittlefish/article/details/6586946
The above introduces the nginx dynamic array ngx_array_t, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.