Nginx原始碼完全註釋(1)ngx_alloc.h / ngx_alloc.c
Nginx原始碼完全註釋(1)ngx_alloc.h / ngx_alloc.c
- 作者:柳大·Poechant
- 時間:2012年7月1日
- 部落格:Blog.CSDN.net/Poechant
首先看 ngx_alloc.h 檔案,主要宣告或巨集定義了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free。
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGX_ALLOC_H_INCLUDED_
#define _NGX_ALLOC_H_INCLUDED_
#include <ngx_config h=""><span class="preprocessor" style="color: rgb(136, 0, 0); ">#include </span><ngx_core h=""><span class="keyword" style="font-weight: bold; ">void</span> *ngx_alloc(size_t size, ngx_log_t *log);
<span class="keyword" style="font-weight: bold; ">void</span> *ngx_calloc(size_t size, ngx_log_t *log);
<span class="comment" style="color: rgb(136, 136, 136); ">// 巨集命名 free 為 ngx_free,Nginx 的習慣</span>
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#define ngx_free free</span>
<span class="comment" style="color: rgb(136, 136, 136); ">/*
* Linux has memalign() or posix_memalign()
* Solaris has memalign()
* FreeBSD 7.0 has posix_memalign(), besides, early version's malloc()
* aligns allocations bigger than page size at the page boundary
*/</span>
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#if (NGX_HAVE_POSIX_MEMALIGN || NGX_HAVE_MEMALIGN)</span>
<span class="keyword" style="font-weight: bold; ">void</span> *ngx_memalign(size_t alignment, size_t size, ngx_log_t *log);
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#else</span>
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#define ngx_memalign(alignment, size, log) ngx_alloc(size, log)</span>
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#endif</span>
<span class="comment" style="color: rgb(136, 136, 136); ">// 宣告三個可以被外部使用的變數</span>
<span class="keyword" style="font-weight: bold; ">extern</span> ngx_uint_t ngx_pagesize;
<span class="keyword" style="font-weight: bold; ">extern</span> ngx_uint_t ngx_pagesize_shift;
<span class="keyword" style="font-weight: bold; ">extern</span> ngx_uint_t ngx_cacheline_size;
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#endif /* _NGX_ALLOC_H_INCLUDED_ */</span>
</ngx_core></ngx_config>
再來看 ngx_alloc.c,實現了記憶體分配函式 ngx_alloc,ngx_calloc,ngx_
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config h=""><span class="preprocessor" style="color: rgb(136, 0, 0); ">#include </span><ngx_core h="">
ngx_uint_t ngx_pagesize;
ngx_uint_t ngx_pagesize_shift;
ngx_uint_t ngx_cacheline_size;
<span class="comment" style="color: rgb(136, 136, 136); ">/*
* 封裝malloc,增加分配失敗判斷及除錯日誌
*/</span>
<span class="keyword" style="font-weight: bold; ">void</span> *
ngx_alloc(size_t size, ngx_log_t *log)
{
<span class="keyword" style="font-weight: bold; ">void</span> *p;
p = malloc(size);
<span class="keyword" style="font-weight: bold; ">if</span> (p == NULL) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
<span class="string" style="color: rgb(136, 0, 0); ">"malloc(%uz) failed"</span>, size);
}
<span class="comment" style="color: rgb(136, 136, 136); ">/* 在編譯時指定debug模式是否開啟,如果不開啟則此句僅是括號中的逗號表示式 */</span>
ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, log, <span class="number" style="color: rgb(0, 136, 0); ">0</span>, <span class="string" style="color: rgb(136, 0, 0); ">"malloc: %p:%uz"</span>, p, size);
<span class="keyword" style="font-weight: bold; ">return</span> p;
}
<span class="comment" style="color: rgb(136, 136, 136); ">/*
* 封裝ngx_alloc,如果分配成功,初始化為0
*/</span>
<span class="keyword" style="font-weight: bold; ">void</span> *
ngx_calloc(size_t size, ngx_log_t *log)
{
<span class="keyword" style="font-weight: bold; ">void</span> *p;
p = ngx_alloc(size, log);
<span class="comment" style="color: rgb(136, 136, 136); ">/* 初始化為 0 */</span>
<span class="keyword" style="font-weight: bold; ">if</span> (p) {
ngx_memzero(p, size);
}
<span class="keyword" style="font-weight: bold; ">return</span> p;
}
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#if (NGX_HAVE_POSIX_MEMALIGN)</span>
<span class="comment" style="color: rgb(136, 136, 136); ">// 封裝 posix_memalign,如果是 Solaris 則封裝 memalign</span>
<span class="keyword" style="font-weight: bold; ">void</span> *
ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
{
<span class="keyword" style="font-weight: bold; ">void</span> *p;
<span class="keyword" style="font-weight: bold; ">int</span> err;
<span class="comment" style="color: rgb(136, 136, 136); ">/*
* 背景:
* 1)POSIX 1003.1d
* 2)POSIX 標明瞭通過malloc( ), calloc( ), 和 realloc( ) 返回的地址對於
* 任何的C型別來說都是對齊的
* 功能:由posix_memalign分配的記憶體空間,需要由free釋放。
* 引數:
* p 分配好的記憶體空間的首地址
* alignment 對齊邊界,Linux中,32位系統是8位元組,64位系統是16位元組
* size 指定分配size位元組大小的記憶體
*
* 要求:
* 1)要求alignment是2的冪,並且是p指標大小的倍數
* 2)要求size是alignment的倍數
* 返回:
* 0 成功
* EINVAL 引數不滿足要求
* ENOMEM 記憶體分配失敗
* 注意:
* 1)該函式不影響errno,只能通過返回值判斷
*
*/</span>
err = posix_memalign(&p, alignment, size);
<span class="keyword" style="font-weight: bold; ">if</span> (err) {
ngx_log_error(NGX_LOG_EMERG, log, err,
<span class="string" style="color: rgb(136, 0, 0); ">"posix_memalign(%uz, %uz) failed"</span>, alignment, size);
p = NULL;
}
ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, <span class="number" style="color: rgb(0, 136, 0); ">0</span>,
<span class="string" style="color: rgb(136, 0, 0); ">"posix_memalign: %p:%uz @%uz"</span>, p, size, alignment);
<span class="keyword" style="font-weight: bold; ">return</span> p;
}
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#elif (NGX_HAVE_MEMALIGN)</span>
<span class="keyword" style="font-weight: bold; ">void</span> *
ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
{
<span class="keyword" style="font-weight: bold; ">void</span> *p;
<span class="comment" style="color: rgb(136, 136, 136); ">// 與 posix_memalign 的不同是其將分配好的記憶體塊首地址做為返回值</span>
p = memalign(alignment, size);
<span class="keyword" style="font-weight: bold; ">if</span> (p == NULL) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
<span class="string" style="color: rgb(136, 0, 0); ">"memalign(%uz, %uz) failed"</span>, alignment, size);
}
ngx_log_debug3(NGX_LOG_DEBUG_ALLOC, log, <span class="number" style="color: rgb(0, 136, 0); ">0</span>,
<span class="string" style="color: rgb(136, 0, 0); ">"memalign: %p:%uz @%uz"</span>, p, size, alignment);
<span class="keyword" style="font-weight: bold; ">return</span> p;
}
<span class="preprocessor" style="color: rgb(136, 0, 0); ">#endif</span>
</ngx_core></ngx_config>
-
轉載請註明來自柳大的CSDN部落格:Blog.CSDN.net/Poechant
-
相關文章
- Nginx原始碼完全註釋(6)core/murmurhashNginx原始碼
- Nginx原始碼完全註釋(8)ngx_errno.cNginx原始碼
- Nginx原始碼完全註釋(9)nginx.c: ngx_get_optionsNginx原始碼
- Nginx原始碼完全註釋(5)core/ngx_cpuinfo.cNginx原始碼UI
- 原始碼完全註釋:socket select原始碼
- Nginx原始碼完全註釋(7)ngx_palloc.h/ngx_palloc.cNginx原始碼
- Nginx原始碼完全註釋(4)ngx_queue.h / ngx_queue.cNginx原始碼
- Nginx原始碼完全註釋(3)ngx_list.h / ngx_list.cNginx原始碼
- Nginx原始碼完全註釋(2)ngx_array.h / ngx_array.cNginx原始碼
- Nginx 原始碼完全剖析(11)ngx_spinlockNginx原始碼
- Nginx 原始碼完全剖析(10)ngx_radix_treeNginx原始碼
- C231n-KNN-assignment1-完全程式碼及註釋KNN
- C231n-SVM-assignment1-完全程式碼及註釋
- EventBus原始碼解讀詳細註釋(1)register的幕後黑手原始碼
- 《Linux核心完全註釋》學習筆記:2.7 Linux核心原始碼的目錄結構Linux筆記原始碼
- HashMap原始碼(JDK1.8)-手動註釋HashMap原始碼JDK
- Bootstrap的Model原始碼詳細註釋 (轉)boot原始碼
- Nginx篇--Nginx原始碼搭建Nginx原始碼
- Redux原始碼完全解讀Redux原始碼
- bootstrap-modal.js學習筆記(原始碼註釋)bootJS筆記原始碼
- Dubbo原始碼解析之服務釋出與註冊原始碼
- 從Python原始碼註釋,自動生成API文件Python原始碼API
- 使用ehcache元註釋提高Spring 效能原始碼案例Spring原始碼
- nginx原始碼安裝Nginx原始碼
- NGINX原始碼閱讀Nginx原始碼
- Nginx 原始碼安裝Nginx原始碼
- 原始碼安裝Nginx原始碼Nginx
- Nginx的nginx.conf配置檔案中文註釋說明Nginx
- CSS程式碼註釋CSS
- php程式碼註釋PHP
- maven下載原始碼,解決中文註釋為亂碼的問題Maven原始碼
- DialogFragment使用到原始碼完全解析Fragment原始碼
- docker原始碼安裝NginxDocker原始碼Nginx
- macbook 原始碼安裝 nginxMac原始碼Nginx
- Centos原始碼安裝NginxCentOS原始碼Nginx
- ArcGIS VBA - VBA+AO入門15例完全註釋版
- snownlp類庫(中文情感分析)原始碼註釋及使用原始碼
- NumPyCookbook帶註釋原始碼四、連線NumPy與剩餘世界原始碼