백엔드 개발 PHP 튜토리얼 nginx 동적 배열 ngx_array_t

nginx 동적 배열 ngx_array_t

Aug 08, 2016 am 09:23 AM
pool printf quot

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 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

리눅스 printf는 어디에 있나요? 리눅스 printf는 어디에 있나요? Mar 10, 2023 am 09:05 AM

linux printf는 명령줄에서 사용됩니다. 이 명령은 인쇄 데이터의 형식을 지정하는 데 사용됩니다. printf의 명령 형식은 "printf FORMAT [ARGUMENT]...printf OPTION"입니다. 여기서 "help" 옵션은 도움말 정보 표시를 의미합니다. "version" 옵션은 버전 정보 표시를 의미합니다.

putchar와 printf의 차이점은 무엇입니까 putchar와 printf의 차이점은 무엇입니까 Aug 22, 2023 pm 01:55 PM

putchar와 printf의 차이점: 1. putchar의 매개변수 유형은 int이고 printf의 매개변수 유형은 문자열입니다. 2. putchar는 한 문자만 출력할 수 있고 printf는 여러 문자를 출력할 수 있습니다. 3. putchar는 출력 형식을 지정할 수 없습니다. printf는 출력 형식을 지정할 수 있습니다. 4. putchar에는 반환 값이 없으며, printf는 성공적으로 출력된 문자 수를 반환합니다. 5. putchar는 콘솔에 대한 출력으로 제한되지 않습니다.

fprintf와 printf의 차이점 fprintf와 printf의 차이점 Nov 28, 2023 am 10:48 AM

fprintf와 printf의 차이점은 출력 대상이 다르다는 것입니다. printf는 표준 출력 스트림으로 출력하는 반면 fprintf는 지정된 파일 스트림으로 출력합니다. 필요에 따라 출력 작업을 수행하려면 적절한 기능을 선택하십시오. fprintf 함수는 먼저 fopen 함수를 통해 파일을 열고, 사용 후에는 fclose 함수를 통해 파일을 닫아야 한다는 점에 유의해야 합니다. 또한 파일 열기에 실패하거나 작업 오류가 발생한 경우 오류 처리가 필요합니다.

图片消失怎么解决 图片消失怎么解决 Apr 07, 2024 pm 03:02 PM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 Jun 13, 2016 am 10:24 AM

为什么我在php上写的这个代码,在浏览器上什么都不显示啊

图片消失怎么解决 图片消失怎么解决 Jun 13, 2016 am 10:09 AM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

See all articles