Nginx原始碼完全註釋(1)ngx_alloc.h / ngx_alloc.c

鍾超發表於2012-07-01

Nginx原始碼完全註釋(1)ngx_alloc.h / ngx_alloc.c


首先看 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(&amp;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

-

相關文章