nginx 동적 배열 ngx_array_t

WBOY
풀어 주다: 2016-08-08 09:23:17
원래의
1007명이 탐색했습니다.

ngx_array_t는 STL의 벡터과 유사하게 nginx로 설계된 동적 배열입니다. 아래에서는 예시를 통해 분석해보겠습니다.

1. 예시

<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. 사용 가능한 풀 메모리의 변화를 보면 ngx_array_t 및 ngx_pool_large_t 구조체 자체가 차지하는 메모리가 메모리 풀에 할당되어 있음을 알 수 있습니다.

소스 코드에서 증거를 얻을 수 있습니다:

ngx_array_t *

ngx_array_create(ngx_pool_t *p , ngx_uint_t n, size_t 크기)

{

a = ngx_palloc(p, sizeof(ngx_array_t));

}

정적 무효 *

ngx_palloc_large( ngx_pool_t* 풀, size_t 크기)

{

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 구조를 분석한 이전 글을 참고하세요.

2. 참고문헌 :

"nginx에 대한 심층적인 이해" Tao Hui

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

위 내용은 관련 내용을 포함하여 nginx 동적 배열 ngx_array_t를 소개한 내용이 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!