ngx_array_t是nginx中設計的動態數組,類似STL中的vector
一、實例
<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>
運行結果:
-------------------------------- 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、 從available pool memory
1、 從available pool memory 的變化可以得知記憶體在記憶體上是。
從源碼中可以得到證明:
ngx_array_t *
ngx_array_create(ngx_pool_t*p, ngx_array_create(ngx_pool_t*p, s
a = ngx_palloc(p, sizeof( ngx_array_t));
}
static void **
static void *
*{
// 記憶體池上分配。
large = ngx_palloc(pool,sizeof(ngx_pool_large_t));
}
2.可以參考ngx_array_push的源碼,在此就不貼了。
3、 如果分配動態陣列的大小超過一塊記憶體池的容量(在本例中是1024),會呼叫ngx_palloc_large分配大塊記憶體。
4、 如果動態數組所佔內存是大塊內存,ngx_array_destroy不會做任何事情,並且該API在nginx內核源碼中沒有被調用過。
編譯可以參考上一遍分析ngx_queue_t結構體的文章。
二、參考資料:
《深入理解nginx》陶輝
http://blog. 以上就介紹了nginx動態數組ngx_array_t,包含了方面的內容,希望對PHP教程有興趣的朋友有所幫助。