日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx源码分析—内存池结构ngx_pool_t及内存管理

發布時間:2025/3/21 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx源码分析—内存池结构ngx_pool_t及内存管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本博客( http://blog.csdn.net/livelylittlefish)貼出作者(阿波)相關研究、學習內容所做的筆記,歡迎廣大朋友指正!

Content

0.

1.內存池結構

1.1 ngx_pool_t結構

1.2其他相關結構

1.3 ngx_pool_t的邏輯結構

2.內存池操作

2.1創建內存池

2.2銷毀內存池

2.3重置內存池

2.4分配內存

2.4.1 ngx_palloc()函數分析

2.4.2 ngx_palloc_block()函數分析

2.5釋放內存

2.6注冊cleanup

2.7內存池的物理結構

3.一個例子

3.1代碼

3.2如何編譯

3.3運行結果

4.小結

5.致謝

0.

?

nginx對內存的管理由其自己實現的內存池結構ngx_pool_t來完成,本文重點敘述nginx的內存管理。

?

nginx內存管理相關文件:

(1) ./src/os/unix/ngx_alloc.h/.c

  • 內存相關的操作,封裝了最基本的內存分配函數
  • free/malloc/memalign/posix_memalign,分別被封裝為ngx_freengx_alloc/ngx_calloc, ngx_memalign
    • ngx_alloc:封裝malloc分配內存
    • ngx_calloc:封裝malloc分配內存,并初始化空間內容為0
    • ngx_memalign:返回基于一個指定alignment的大小為size的內存空間,且其地址為alignment的整數倍,alignment2的冪。

(2) ./src/core/ngx_palloc.h/.c

  • 封裝創建/銷毀內存池,從內存池分配空間等函數

?

.表示nginx-1.0.4代碼目錄,本文為/usr/src/nginx-1.0.4

?

1.內存池結構

nginx對內存的管理均統一完成,例如,在特定的生命周期統一建立內存池(main函數系統啟動初期即分配1024B大小的內存池),需要內存時統一分配內存池中的內存,在適當的時候釋放內存池的內存(如關閉http鏈接時調用ngx_destroy_pool進行銷毀)

因此,開發者只需在需要內存時進行申請即可,不用過多考慮內存的釋放等問題,大大提高了開發的效率。先看一下內存池結構。

?

1.1 ngx_pool_t結構

此處統一一下概念,內存池的數據塊:即分配內存在這些數據塊中進行,一個內存池可以有多一個內存池數據塊。nginx的內存池結構如下。?

[cpp] view plain copy
  • 00048:?typedef?struct?{??
  • 00049:???u_char??????*last;??//當前內存池分配到此處,即下一次分配從此處開始??
  • 00050:???u_char??????*end;???//內存池結束位置??
  • 00051:???ngx_pool_t??*next;??//內存池里面有很多塊內存,這些內存塊就是通過該指針連成鏈表的??
  • 00052:???ngx_uint_t??failed;?//內存池分配失敗次數??
  • 00053:?}?ngx_pool_data_t;????//內存池的數據塊位置信息??
  • 00054:??
  • 00055:??
  • 00056:?struct?ngx_pool_s{????//內存池頭部結構??
  • 00057:????ngx_pool_data_t?????d;???????//內存池的數據塊??
  • 00058:????size_t??????????????max;?????//內存池數據塊的最大值??
  • 00059:????ngx_pool_t?????????*current;?//指向當前內存池??
  • 00060:????ngx_chain_t????????*chain;???//該指針掛接一個ngx_chain_t結構??
  • 00061:????ngx_pool_large_t???*large;???//大塊內存鏈表,即分配空間超過max的內存??
  • 00062:????ngx_pool_cleanup_t?*cleanup;?//釋放內存池的callback??
  • 00063:????ngx_log_t??????????*log;?????//日志信息??
  • 00064:?};??
  • 其中,sizeof(ngx_pool_data_t)=16Bsizeof(ngx_pool_t)=40B
    nginx將幾乎所有的結構體放在ngx_core.h文件中重新進行了申明,如下。

    [cpp] view plain copy
  • typedef?struct?ngx_module_s??????ngx_module_t;??
  • typedef?struct?ngx_conf_s????????ngx_conf_t;??
  • typedef?struct?ngx_cycle_s???????ngx_cycle_t;??
  • typedef?struct?ngx_pool_s????????ngx_pool_t;??
  • typedef?struct?ngx_chain_s???????ngx_chain_t;??
  • typedef?struct?ngx_log_s?????????ngx_log_t;??
  • typedef?struct?ngx_array_s???????ngx_array_t;??
  • typedef?struct?ngx_open_file_s???ngx_open_file_t;??
  • typedef?struct?ngx_command_s?????ngx_command_t;??
  • typedef?struct?ngx_file_s????????ngx_file_t;??
  • typedef?struct?ngx_event_s???????ngx_event_t;??
  • typedef?struct?ngx_event_aio_s???ngx_event_aio_t;??
  • typedef?struct?ngx_connection_s??ngx_connection_t;??
  • 1.2其他相關結構

    其他與內存池相干的數據結構,如清除資源的cleanup鏈表,分配的大塊內存鏈表等,如下。

    [cpp] view plain copy
  • 00015:?/*?
  • 00016:?*?NGX_MAX_ALLOC_FROM_POOL?should?be?(ngx_pagesize?-?1),?i.e.?4095?on?x86.?
  • 00017:?*?On?Windows?NT?it?decreases?a?number?of?locked?pages?in?a?kernel.?
  • 00018:?*/??
  • 00019:?#define?NGX_MAX_ALLOC_FROM_POOL?(ngx_pagesize?-?1)??//在x86體系結構下,該值一般為4096B,即4K??
  • 00020:??
  • 00021:?#define?NGX_DEFAULT_POOL_SIZE????(16*?1024)??
  • 00022:??
  • 00023:?#define?NGX_POOL_ALIGNMENT????16??
  • 00024:?#define?NGX_MIN_POOL_SIZE????\??
  • 00025:????ngx_align((sizeof(ngx_pool_t)?+?2?*?sizeof(ngx_pool_large_t)),????\??
  • 00026:????NGX_POOL_ALIGNMENT)??
  • 00027:??
  • 00028:??
  • 00029:?typedef?void?(*ngx_pool_cleanup_pt)(void?*data);????//cleanup的callback類型??
  • 00030:??
  • 00031:?typedef?struct?ngx_pool_cleanup_s?ngx_pool_cleanup_t;??
  • 00032:??
  • 00033:?struct?ngx_pool_cleanup_s{??
  • 00034:????ngx_pool_cleanup_pt?handler;??
  • 00035:????void????*data;??????????????//指向要清除的數據??
  • 00036:????ngx_pool_cleanup_t?*next;???//下一個cleanup?callback??
  • 00037:?};??
  • 00038:??
  • 00039:??
  • 00040:?typedef?struct?ngx_pool_large_s?ngx_pool_large_t;??
  • 00041:??
  • 00042:?struct?ngx_pool_large_s{??
  • 00043:????ngx_pool_large_t??*next;????//指向下一塊大塊內存??
  • 00044:????void????*alloc;?????????????//指向分配的大塊內存??
  • 00045:?};??
  • ...??
  • ...??
  • 00067:?typedef?struct?{??
  • 00068:????ngx_fd_t???fd;??
  • 00069:????u_char????*name;??
  • 00070:????ngx_log_t?*log;??
  • 00071:?}?ngx_pool_cleanup_file_t;??
  • 00072:??
  • (gdb) p getpagesize()

    $18 = 4096

    ?

    全局變量ngx_pagesize的初始化是在如下函數中完成的。./src/os/unix/ngx_posix_init.c

    [cpp] view plain copy
  • ngx_int_t??
  • ngx_os_init(ngx_log_t?*log)??
  • {??
  • ????ngx_uint_t??n;??
  • ??
  • #if?(NGX_HAVE_OS_SPECIFIC_INIT)??
  • ????if?(ngx_os_specific_init(log)?!=?NGX_OK)?{??
  • ????????return?NGX_ERROR;??
  • ????}??
  • #endif??
  • ??
  • ????ngx_init_setproctitle(log);??
  • ??
  • ????/**?該函數為glibc的庫函數,由系統調用實現,返回內核中的PAGE_SIZE,該值依賴體系結構*/??
  • ????ngx_pagesize?=?getpagesize();????????
  • ????ngx_cacheline_size?=?NGX_CPU_CACHE_LINE;??
  • ????...??
  • }??
  • 這些數據結構之間的關系,請參考后面的圖。

    ?

    1.3 ngx_pool_t的邏輯結構

    ?

    這些數據結構邏輯結構圖如下。注:本文采用UML的方式畫出該圖。

    2.內存池操作

    2.1創建內存池

    創建內存池有ngx_create_pool()函數完成,代碼如下。

    [cpp] view plain copy
  • 00015:?ngx_pool_t?*??
  • 00016:?ngx_create_pool(size_t?size,?ngx_log_t?*log)??
  • 00017:?{??
  • 00018:????ngx_pool_t?*p;??
  • 00019:??
  • 00020:????p?=?ngx_memalign(NGX_POOL_ALIGNMENT,?size,?log);??
  • 00021:????if?(p?==?NULL)?{??
  • 00022:???????return?NULL;??
  • 00023:????}??
  • 00024:??
  • 00025:????p->d.last?=?(u_char?*)?p?+?sizeof(ngx_pool_t);??//last指向ngx_pool_t結構體之后數據取起始位置??
  • 00026:????p->d.end?=?(u_char?*)?p?+?size;??//end指向分配的整個size大小的內存的末尾??
  • 00027:????p->d.next?=?NULL;??
  • 00028:????p->d.failed?=?0;??
  • 00029:??
  • 00030:????size?=?size?-?sizeof(ngx_pool_t);??
  • 00031:????p->max?=?(size?<?NGX_MAX_ALLOC_FROM_POOL)???size?:?NGX_MAX_ALLOC_FROM_POOL;??//最大不超過4095B??
  • 00032:??
  • 00033:????p->current?=?p;??
  • 00034:????p->chain?=?NULL;??
  • 00035:????p->large?=?NULL;??
  • 00036:????p->cleanup?=?NULL;??
  • 00037:????p->log?=?log;??
  • 00038:??
  • 00039:????return?p;??
  • 00040:?}??
  • 例如,調用ngx_create_pool(1024, 0x80d1c4c)后,創建的內存池物理結構如下圖。

    2.2銷毀內存池

    ?

    銷毀內存池由如下函數完成。

    void ngx_destroy_pool(ngx_pool_t *pool)

    該函數將遍歷內存池鏈表,所有釋放內存,如果注冊了clenup(也是一個鏈表結構)亦將遍歷該cleanup鏈表結構依次調用clenuphandler清理。同時,還將遍歷large鏈表,釋放大塊內存。

    ?

    2.3重置內存池

    ?

    重置內存池由下面的函數完成。

    void ngx_reset_pool(ngx_pool_t *pool);

    該函數將釋放所有large內存,并且將d->last指針重新指向ngx_pool_t結構之后數據區的開始位置,同剛創建后的位置相同。

    ?

    2.4分配內存

    ?

    內存分配的函數如下。

    void *ngx_palloc(ngx_pool_t *pool, size_t size);

    void *ngx_pnalloc(ngx_pool_t *pool, size_t size);

    void *ngx_pcalloc(ngx_pool_t *pool, size_t size);

    void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment);

    ?

    返回值為分配的內存起始地址。選擇其中的兩個函數進行分析,其他的也很好理解,省略。

    ?

    2.4.1 ngx_palloc()函數分析

    ?

    ngx_palloc()代碼如下,分析請參考筆者所加的注釋。

    [cpp] view plain copy
  • 00115:?void?*??
  • 00116:?ngx_palloc(ngx_pool_t?*pool,?size_t?size)??
  • 00117:?{??
  • 00118:????u_char????*m;??
  • 00119:????ngx_pool_t?*p;??
  • 00120:??
  • 00121:????if?(size?<=?pool->max)?{//判斷待分配內存與max值??
  • 00122:??
  • 00123:???????p?=?pool->current;???//小于max值,則從current節點開始遍歷pool鏈表??
  • 00124:??
  • 00125:???????do?{??
  • 00126:??????????m?=?ngx_align_ptr(p->d.last,?NGX_ALIGNMENT);??
  • 00127:??
  • 00128:??????????if?((size_t)?(p->d.end?-?m)?>=?size)?{??
  • 00129:?????????????p->d.last?=?m?+?size;??//在該節點指向的內存塊中分配size大小的內存??
  • 00130:??
  • 00131:?????????????return?m;??
  • 00132:??????????}??
  • 00133:??
  • 00134:??????????p?=?p->d.next;??
  • 00135:??
  • 00136:???????}?while?(p);??
  • 00137:??
  • 00138:???????return?ngx_palloc_block(pool,?size);?//鏈表里沒有能分配size大小內存的節點,則生成一個新的節點并在其中分配內存??
  • 00139:????}??
  • 00140:??
  • 00141:????return?ngx_palloc_large(pool,?size);??//大于max值,則在large鏈表里分配內存??
  • 00142:?}??
  • 例如,在2.1節中創建的內存池中分配200B的內存,調用ngx_palloc(pool, 200)后,該內存池物理結構如下圖。

    2.4.2 ngx_palloc_block()函數分析

    ?

    ngx_palloc_block函數代碼如下,分析請參考筆者所加的注釋。

    [cpp] view plain copy
  • 00175:?static?void?*??
  • 00176:?ngx_palloc_block(ngx_pool_t?*pool,?size_t?size)??
  • 00177:?{??
  • 00178:????u_char????*m;??
  • 00179:????size_t????psize;??
  • 00180:????ngx_pool_t?*p,?*new,?*current;??
  • 00181:??
  • 00182:????psize?=?(size_t)?(pool->d.end?-?(u_char?*)?pool);??????//計算pool的大小??
  • 00183:??
  • 00184:????m?=?ngx_memalign(NGX_POOL_ALIGNMENT,?psize,?pool->log);//分配一塊與pool大小相同的內存??
  • 00185:????if?(m?==?NULL)?{??
  • 00186:???????return?NULL;??
  • 00187:????}??
  • 00188:??
  • 00189:????new?=?(ngx_pool_t?*)?m;??
  • 00190:??
  • 00191:????new->d.end?=?m?+?psize;?//設置end指針??
  • 00192:????new->d.next?=?NULL;??
  • 00193:????new->d.failed?=?0;??
  • 00194:??
  • 00195:????m?+=?sizeof(ngx_pool_data_t);?//讓m指向該塊內存ngx_pool_data_t結構體之后數據區起始位置??
  • 00196:????m?=?ngx_align_ptr(m,?NGX_ALIGNMENT);?//按4字節對齊??
  • 00197:????new->d.last?=?m?+?size;???????//在數據區分配size大小的內存并設置last指針??
  • 00198:??
  • 00199:????current?=?pool->current;??
  • 00200:??
  • 00201:????for?(p?=?current;?p->d.next;?p?=?p->d.next)?{??
  • 00202:???????if?(p->d.failed++?>?4)?{???//failed的值只在此處被修改??
  • 00203:??????????current?=?p->d.next;????//失敗4次以上移動current指針??
  • 00204:???????}??
  • 00205:????}??
  • 00206:??
  • 00207:????p->d.next?=?new;??//將這次分配的內存塊new加入該內存池??
  • 00208:??
  • 00209:????pool->current?=?current???current?:?new;??
  • 00210:??
  • 00211:????return?m;??
  • 00212:?}??
  • 注意:該函數分配一塊內存后,last指針指向的是ngx_pool_data_t結構體(大小16B)之后數據區的起始位置。而創建內存池時時,last指針指向的是ngx_pool_t結構體(大小40B)之后數據區的起始位置。

    ?

    結合2.7節的內存池的物理結構,更容易理解。

    ?

    2.5釋放內存

    ?

    請參考如下函數,不再贅述。?

    ngx_int_tngx_pfree(ngx_pool_t *pool, void *p)

    需要注意的是該函數只釋放large鏈表中注冊的內存,普通內存在ngx_destroy_pool中統一釋放。

    ?

    2.6注冊cleanup

    ?

    請參考如下函數,該函數實現也很簡單,此處不再贅述。

    ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)

    ?

    2.7內存池的物理結構

    ?

    針對本文第3節的例子,畫出的內存池的物理結構如下圖。

    ??

    從該圖也能看出2.4節的結論,即內存池第一塊內存前40字節為ngx_pool_t結構,后續加入的內存塊前16個字節為ngx_pool_data_t結構,這兩個結構之后便是真正可以分配內存區域。

    ?

    因此,本文Reference中的內存分配相關中的圖是有一點點小問題的,并不是每一個節點的前面都是ngx_pool_t結構。

    ?

    3.一個例子

    ?

    理解并掌握開源軟件的最好方式莫過于自己寫一些測試代碼,或者改寫軟件本身,并進行調試來進一步理解開源軟件的原理和設計方法。本節給出一個創建內存池并從中分配內存的簡單例子。

    ?

    3.1代碼

    [cpp] view plain copy
  • /**?
  • ?*?ngx_pool_t?test,?to?test?ngx_palloc,?ngx_palloc_block,?ngx_palloc_large?
  • ?*/??
  • ??
  • #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"??
  • ??
  • 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);??
  • ????????pool?=?pool->d.next;??
  • ????}??
  • }??
  • ??
  • 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);??
  • ??
  • ????printf("--------------------------------\n");??
  • ????printf("alloc?block?1?from?the?pool:\n");??
  • ????printf("--------------------------------\n");??
  • ????ngx_palloc(pool,?512);??
  • ????dump_pool(pool);??
  • ??
  • ????printf("--------------------------------\n");??
  • ????printf("alloc?block?2?from?the?pool:\n");??
  • ????printf("--------------------------------\n");??
  • ????ngx_palloc(pool,?512);??
  • ????dump_pool(pool);??
  • ??
  • ????printf("--------------------------------\n");??
  • ????printf("alloc?block?3?from?the?pool?:\n");??
  • ????printf("--------------------------------\n");??
  • ????ngx_palloc(pool,?512);??
  • ????dump_pool(pool);??
  • ??
  • ????ngx_destroy_pool(pool);??
  • ????return?0;??
  • }??
  • 3.2如何編譯

    ?

    這個問題是編寫測試代碼或者改寫軟件本身最迫切需要解決的問題,否則,編寫的代碼無從編譯或運行,那也無從進行調試并理解軟件了。

    ?

    如何對自己編寫的測試代碼進行編譯,可參考Linux平臺代碼覆蓋率測試-編譯過程自動化及對鏈接的解釋Linux平臺如何編譯使用Google test寫的單元測試?。我們要做的是學習這種編譯工程的方法,針對該例子,筆者編寫的makefile文件如下。——這便是本節的主要目的。

    [plain] view plain copy
  • CXX?=?gcc??
  • CXXFLAGS?+=?-g?-Wall?-Wextra??
  • ??
  • NGX_ROOT?=?/usr/src/nginx-1.0.4??
  • ??
  • TARGETS?=?ngx_pool_t_test??
  • TARGETS_C_FILE?=?$(TARGETS).c??
  • ??
  • CLEANUP?=?rm?-f?$(TARGETS)?*.o??
  • ??
  • all:?$(TARGETS)??
  • ??
  • clean:??
  • ????$(CLEANUP)??
  • ??
  • CORE_INCS?=?-I.?\??
  • ????-I$(NGX_ROOT)/src/core?\??
  • ????-I$(NGX_ROOT)/src/event?\??
  • ????-I$(NGX_ROOT)/src/event/modules?\??
  • ????-I$(NGX_ROOT)/src/os/unix?\??
  • ????-I$(NGX_ROOT)/objs?\??
  • ??
  • NGX_PALLOC?=?$(NGX_ROOT)/objs/src/core/ngx_palloc.o??
  • NGX_STRING?=?$(NGX_ROOT)/objs/src/core/ngx_string.o??
  • NGX_ALLOC?=?$(NGX_ROOT)/objs/src/os/unix/ngx_alloc.o??
  • ??
  • $(TARGETS):?$(TARGETS_C_FILE)??
  • ????$(CXX)?$(CXXFLAGS)?$(CORE_INCS)?$(NGX_PALLOC)?$(NGX_STRING)?$(NGX_ALLOC)?$^?-o?$@??
  • 3.3運行運行結果

    [plain] view plain copy
  • #?./ngx_pool_t_test??
  • --------------------------------??
  • create?a?new?pool:??
  • --------------------------------??
  • pool?=?0x8922020??
  • ??.d??
  • ????.last?=?0x8922048??
  • ????.end?=?0x8922420??
  • ????.next?=?0x0??
  • ????.failed?=?0??
  • ??.max?=?984??
  • ??.current?=?0x8922020??
  • ??.chain?=?0x0??
  • ??.large?=?0x0??
  • ??.cleanup?=?0x0??
  • ??.log?=?0x0??
  • available?pool?memory?=?984??
  • ??
  • --------------------------------??
  • alloc?block?1?from?the?pool:??
  • --------------------------------??
  • pool?=?0x8922020??
  • ??.d??
  • ????.last?=?0x8922248??
  • ????.end?=?0x8922420??
  • ????.next?=?0x0??
  • ????.failed?=?0??
  • ??.max?=?984??
  • ??.current?=?0x8922020??
  • ??.chain?=?0x0??
  • ??.large?=?0x0??
  • ??.cleanup?=?0x0??
  • ??.log?=?0x0??
  • available?pool?memory?=?472??
  • ??
  • --------------------------------??
  • alloc?block?2?from?the?pool:??
  • --------------------------------??
  • pool?=?0x8922020??
  • ??.d??
  • ????.last?=?0x8922248??
  • ????.end?=?0x8922420??
  • ????.next?=?0x8922450??
  • ????.failed?=?0??
  • ??.max?=?984??
  • ??.current?=?0x8922020??
  • ??.chain?=?0x0??
  • ??.large?=?0x0??
  • ??.cleanup?=?0x0??
  • ??.log?=?0x0??
  • available?pool?memory?=?472??
  • ??
  • pool?=?0x8922450??
  • ??.d??
  • ????.last?=?0x8922660??
  • ????.end?=?0x8922850??
  • ????.next?=?0x0??
  • ????.failed?=?0??
  • ??.max?=?0??
  • ??.current?=?0x0??
  • ??.chain?=?0x0??
  • ??.large?=?0x0??
  • ??.cleanup?=?0x0??
  • ??.log?=?0x0??
  • available?pool?memory?=?496??
  • ??
  • --------------------------------??
  • alloc?block?3?from?the?pool?:??
  • --------------------------------??
  • pool?=?0x8922020??
  • ??.d??
  • ????.last?=?0x8922248??
  • ????.end?=?0x8922420??
  • ????.next?=?0x8922450??
  • ????.failed?=?1??
  • ??.max?=?984??
  • ??.current?=?0x8922020??
  • ??.chain?=?0x0??
  • ??.large?=?0x0??
  • ??.cleanup?=?0x0??
  • ??.log?=?0x0??
  • available?pool?memory?=?472??
  • ??
  • pool?=?0x8922450??
  • ??.d??
  • ????.last?=?0x8922660??
  • ????.end?=?0x8922850??
  • ????.next?=?0x8922880??
  • ????.failed?=?0??
  • ??.max?=?0??
  • ??.current?=?0x0??
  • ??.chain?=?0x0??
  • ??.large?=?0x0??
  • ??.cleanup?=?0x0??
  • ??.log?=?0x0??
  • available?pool?memory?=?496??
  • ??
  • pool?=?0x8922880??
  • ??.d??
  • ????.last?=?0x8922a90??
  • ????.end?=?0x8922c80??
  • ????.next?=?0x0??
  • ????.failed?=?0??
  • ??.max?=?0??
  • ??.current?=?0x0??
  • ??.chain?=?0x0??
  • ??.large?=?0x0??
  • ??.cleanup?=?0x0??
  • ??.log?=?0x0??
  • available?pool?memory?=?496??
  • 4. 小結

    ?

    本文針對nginx-1.0.4的內存管理進行了較為全面的分析,包括相關內存池數據結構,內存池的創建、銷毀,以及從內存池中分配內存等。最后通過一個簡單例子向讀者展示nginx內存池的創建和分配操作,同時借此向讀者展示編譯測試代碼的方法。

    ?

    分析完nginx的內存管理,你一定驚嘆于nginx作者的聰明才智。這種內存管理的設計方法小巧、快捷,值得借鑒!

    5. 致謝

    寫作本文,筆者參考了Reference里yixiao的Nginx源碼分析-內存池和RainX1982的Nginx代碼研究計劃。在此給予他們誠摯的感謝!

    Reference

    man posix_memalign (manual頁)(Allocate aligned memory)

    man getpagesize (manual頁)(Get memory page size)

    Nginx源碼分析-內存池 (yixiao)

    Nginx代碼研究計劃 (RainX1982)

    ?

    Appendix: posix_memalign

    ?

    The? function? posix_memalign()? allocates? size? bytes and places theaddress of the allocated memory in*memptr.?The address of the allocated memory will be a multiple of alignment, which must be a? power? oftwo and a multiple of sizeof(void *).

    ?

    the memory is not zeroed.

    posix_memalign() returns zero on success, or one of the error values listed in the next section on?failure.? Note that errno is not set.

    總結

    以上是生活随笔為你收集整理的nginx源码分析—内存池结构ngx_pool_t及内存管理的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。