nginx動態數組ngx_array_t

來源:互聯網
上載者:User
ngx_array_t是nginx中設計的動態數組,類似於STL中的vector。下面我們結合執行個體分析。

一、執行個體

#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\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; }

運行結果:

--------------------------------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、 從 available pool memory 的變化可以得知,ngx_array_t、ngx_pool_large_t結構體本身所佔記憶體是在記憶體池上分配記憶體的。

從源碼中可以得到證明:

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)

{

// 在記憶體池上分配。

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

}

2、 ngx_array_push如果擴容,並不會釋放原來佔用的記憶體。可以參考ngx_array_push的源碼,在此就不貼了。

3、 如果分配動態數組的大小超過一塊記憶體池的容量(在本例中是1024),會調用ngx_palloc_large分配大塊記憶體。

4、 如果動態數組所佔記憶體是大塊記憶體,ngx_array_destroy不會做任何事情,並且該API在nginx核心源碼中沒有被調用過。

編譯可以參考上一遍分析ngx_queue_t結構體的文章。

二、參考資料:

《深入理解nginx》陶輝

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

以上就介紹了nginx動態數組ngx_array_t,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.