欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

nginx动态数组ngx_array_t

程序员文章站 2022-04-27 09:42:29
...
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 = 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

以上就介绍了nginx动态数组ngx_array_t,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。