nginx动态数组ngx_array_t

ngx_array_t是nginx中设计的动态数组,类似于STL中的vector<typename>。下面我们结合实例分析。

一、实例

<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 的变化可以得知,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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值