Nginx原始碼完全註釋(7)ngx_palloc.h/ngx_palloc.c

鍾超發表於2012-09-28

Nginx原始碼完全註釋(7)ngx_palloc.h/ngx_palloc.c

  • 作者:鍾超
  • 郵箱:zhongchao.ustc#gmail.com (# -> @)
  • 部落格:Blog.CSDN.net/Poechant
  • 日期:2012年9月28日

ngx_palloc.h


/*
 * NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86.
 * On Windows NT it decreases a number of locked pages in a kernel.
 */
#define NGX_MAX_ALLOC_FROM_POOL  (ngx_pagesize - 1)

#define NGX_DEFAULT_POOL_SIZE    (16 * 1024)

#define NGX_POOL_ALIGNMENT       16
#define NGX_MIN_POOL_SIZE                                                     \
    ngx_align((sizeof(ngx_pool_t) + 2 * sizeof(ngx_pool_large_t)),            \
              NGX_POOL_ALIGNMENT)


typedef void (*ngx_pool_cleanup_pt)(void *data);

typedef struct ngx_pool_cleanup_s  ngx_pool_cleanup_t;

struct ngx_pool_cleanup_s {
    ngx_pool_cleanup_pt   handler;
    void                 *data;
    ngx_pool_cleanup_t   *next;
};


typedef struct ngx_pool_large_s  ngx_pool_large_t;

struct ngx_pool_large_s {
    ngx_pool_large_t     *next;
    void                 *alloc;
};


typedef struct {
    u_char               *last;     // 資料儲存的已用區尾地址
    u_char               *end;      // 資料儲存區的尾地址
    ngx_pool_t           *next;     // 下一個記憶體池地址
    ngx_uint_t            failed;   // 失敗次數
} ngx_pool_data_t;


struct ngx_pool_s {
    ngx_pool_data_t       d;            // 資料區
    size_t                max;          // 記憶體池的最大儲存空間
    ngx_pool_t           *current;      // 記憶體池
    ngx_chain_t          *chain;
    ngx_pool_large_t     *large;        // 用於儲存大資料,連結串列結構
    ngx_pool_cleanup_t   *cleanup;      // 用於清理,連結串列結構
    ngx_log_t            *log;
};


typedef struct {
    ngx_fd_t              fd;           // 檔案描述符,用於 ngx_pool_cleanup_file
    u_char               *name;         // 檔名,用於 ngx_pool_delete_file
    ngx_log_t            *log;
} ngx_pool_cleanup_file_t;


void *ngx_alloc(size_t size, ngx_log_t *log);
void *ngx_calloc(size_t size, ngx_log_t *log);

ngx_pool_t *ngx_create_pool(size_t size, ngx_log_t *log);
void ngx_destroy_pool(ngx_pool_t *pool);
void ngx_reset_pool(ngx_pool_t *pool);

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);
ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p);


ngx_pool_cleanup_t *ngx_pool_cleanup_add(ngx_pool_t *p, size_t size);
void ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd);
void ngx_pool_cleanup_file(void *data);
void ngx_pool_delete_file(void *data);

ngx_palloc.c



static void *ngx_palloc_block(ngx_pool_t *pool, size_t size);
static void *ngx_palloc_large(ngx_pool_t *pool, size_t size);

// 建立 size 大小的記憶體池
ngx_pool_t *
ngx_create_pool(size_t size, ngx_log_t *log)
{
    ngx_pool_t  *p;

    p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);
    if (p == NULL) {
        return NULL;
    }

    p->d.last = (u_char *) p + sizeof(ngx_pool_t);
    p->d.end = (u_char *) p + size;
    p->d.next = NULL;
    p->d.failed = 0;

    size = size - sizeof(ngx_pool_t);
    p->max = (size < NGX_MAX_ALLOC_FROM_POOL) ? size : NGX_MAX_ALLOC_FROM_POOL;

    p->current = p;
    p->chain = NULL;
    p->large = NULL;
    p->cleanup = NULL;
    p->log = log;

    return p;
}

// 銷燬記憶體池 pool
void
ngx_destroy_pool(ngx_pool_t *pool)
{
    ngx_pool_t          *p, *n;
    ngx_pool_large_t    *l;
    ngx_pool_cleanup_t  *c;

    // 處理 pool->cleanup 連結串列,處理函式由此前賦值到 pool->cleanup->handler 的函式指標確定
    for (c = pool->cleanup; c; c = c->next) {
        if (c->handler) {
            ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
                           "run cleanup: %p", c);
            c->handler(c->data);
        }
    }

    // 釋放 pool->large 連結串列
    for (l = pool->large; l; l = l->next) {

        ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0, "free: %p", l->alloc);

        if (l->alloc) {
            ngx_free(l->alloc);
        }
    }

#if (NGX_DEBUG)

    /*
     * we could allocate the pool->log from this pool
     * so we cannot use this log while free()ing the pool
     */

    for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
        ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
                       "free: %p, unused: %uz", p, p->d.end - p->d.last);

        if (n == NULL) {
            break;
        }
    }

#endif

    // 釋放 pool->d 連結串列
    for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
        ngx_free(p);

        if (n == NULL) {
            break;
        }
    }
}

// 重置記憶體池
void
ngx_reset_pool(ngx_pool_t *pool)
{
    ngx_pool_t        *p;
    ngx_pool_large_t  *l;

    // 釋放 large 鏈的每個節點的記憶體
    for (l = pool->large; l; l = l->next) {
        if (l->alloc) {
            ngx_free(l->alloc);
        }
    }

    pool->large = NULL;

    // 重置資料 d 鏈的每個節點,即重置每個節點的可用區首地址 d.last
    for (p = pool; p; p = p->d.next) {
        p->d.last = (u_char *) p + sizeof(ngx_pool_t);
    }
}

// 從記憶體池 pool 分配大小為 size 的記憶體塊,並返回其地址
// 是被外部使用最多的記憶體池相關 API,並且考慮對齊問題
void *
ngx_palloc(ngx_pool_t *pool, size_t size)
{
    u_char      *m;
    ngx_pool_t  *p;

    // 如果還未超出記憶體池的 max 值,超過了則用 large
    if (size <= pool->max) {

        p = pool->current;

        do {
        
            // 對齊記憶體
            m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);

            // 該節點剩餘可用空間夠用
            if ((size_t) (p->d.end - m) >= size) {
                p->d.last = m + size;

                return m;
            }

            // 該節點剩餘空間不夠用,看下一個節點
            p = p->d.next;

        } while (p);

        // 現有節點都不給力,重新分配一個 d 節點
        return ngx_palloc_block(pool, size);
    }

    // size 超過 pool->max,從 large 取
    return ngx_palloc_large(pool, size);
}

// 類似 ngx_palloc,不考慮對齊問題
void *
ngx_pnalloc(ngx_pool_t *pool, size_t size)
{
    u_char      *m;
    ngx_pool_t  *p;

    if (size <= pool->max) {

        p = pool->current;

        do {
            m = p->d.last;

            if ((size_t) (p->d.end - m) >= size) {
                p->d.last = m + size;

                return m;
            }

            p = p->d.next;

        } while (p);

        return ngx_palloc_block(pool, size);
    }

    return ngx_palloc_large(pool, size);
}


static void *
ngx_palloc_block(ngx_pool_t *pool, size_t size)
{
    u_char      *m;
    size_t       psize;
    ngx_pool_t  *p, *new, *current;

    // pool 結構定義區和 pool->d 資料區的總大小
    psize = (size_t) (pool->d.end - (u_char *) pool);

    // 分配 psize 大小的記憶體
    m = ngx_memalign(NGX_POOL_ALIGNMENT, psize, pool->log);
    if (m == NULL) {
        return NULL;
    }

    // 用 new 來表示上面分配的新記憶體塊
    new = (ngx_pool_t *) m;

    // 初始化這個 new,設定 new 的 d.end、d.next、d.failed
    new->d.end = m + psize;
    new->d.next = NULL;
    new->d.failed = 0;

    // m 加上記憶體池資料定義結構體的大小
    m += sizeof(ngx_pool_data_t);
    // 記憶體對齊 m
    m = ngx_align_ptr(m, NGX_ALIGNMENT);
    // 設定 new 的 d.last
    new->d.last = m + size;

    current = pool->current;

    // TODO
    for (p = current; p->d.next; p = p->d.next) {
        if (p->d.failed++ > 4) {
            current = p->d.next;
        }
    }

    // new 節點放入記憶體池資料鏈
    p->d.next = new;

    pool->current = current ? current : new;

    return m;
}


static void *
ngx_palloc_large(ngx_pool_t *pool, size_t size)
{
    void              *p;
    ngx_uint_t         n;
    ngx_pool_large_t  *large;

    // 分配 size 大小的記憶體
    p = ngx_alloc(size, pool->log);
    if (p == NULL) {
        return NULL;
    }

    n = 0;

    // 在 pool 的 large 鏈中尋找儲存區為空的節點,把新分配的記憶體區首地址賦給它
    for (large = pool->large; large; large = large->next) {
    
        // 找到 large 鏈末尾,在其後插入之,並返回給外部使用
        if (large->alloc == NULL) {
            large->alloc = p;
            return p;
        }

        // 檢視的 large 節點超過 3 個,不再嘗試和尋找,由下面程式碼實現建立新 large 節點的邏輯
        if (n++ > 3) {
            break;
        }
    }

    // 建立 large 鏈的一個新節點,如果失敗則釋放剛才建立的 size 大小的記憶體,並返回 NULL
    large = ngx_palloc(pool, sizeof(ngx_pool_large_t));
    if (large == NULL) {
        ngx_free(p);
        return NULL;
    }

    // 一切順利,善後工作
    large->alloc = p;
    large->next = pool->large;
    pool->large = large;

    return p;
}


void *
ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment)
{
    void              *p;
    ngx_pool_large_t  *large;

    // 建立一塊 size 大小的記憶體,記憶體以 alignment 位元組對齊
    p = ngx_memalign(alignment, size, pool->log);
    if (p == NULL) {
        return NULL;
    }

    // 建立一個 large 節點
    large = ngx_palloc(pool, sizeof(ngx_pool_large_t));
    if (large == NULL) {
        ngx_free(p);
        return NULL;
    }

    // 將這個新的 large 節點交付給 pool 的 large 欄位
    large->alloc = p;
    large->next = pool->large;
    pool->large = large;

    return p;
}


ngx_int_t
ngx_pfree(ngx_pool_t *pool, void *p)
{
    ngx_pool_large_t  *l;

    // 逐一釋放 large 連結串列的每一個節點
    for (l = pool->large; l; l = l->next) {
        if (p == l->alloc) {
            ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, pool->log, 0,
                           "free: %p", l->alloc);
            ngx_free(l->alloc);
            l->alloc = NULL;

            return NGX_OK;
        }
    }

    return NGX_DECLINED;
}

// 封裝 palloc 為 pcalloc,實現分配記憶體並初始化為 0
void *
ngx_pcalloc(ngx_pool_t *pool, size_t size)
{
    void *p;

    p = ngx_palloc(pool, size);
    if (p) {
        ngx_memzero(p, size);
    }

    return p;
}

// 向 cleanup 鏈新增 p->cleanup 這個節點
ngx_pool_cleanup_t *
ngx_pool_cleanup_add(ngx_pool_t *p, size_t size)
{
    ngx_pool_cleanup_t  *c;

    // 建立一個 cleanup 節點
    c = ngx_palloc(p, sizeof(ngx_pool_cleanup_t));
    if (c == NULL) {
        return NULL;
    }

    if (size) {
        // cleanup 節點資料區
        c->data = ngx_palloc(p, size);
        if (c->data == NULL) {
            return NULL;
        }

    } else {
        c->data = NULL;
    }

    // 善後
    c->handler = NULL;
    c->next = p->cleanup;

    p->cleanup = c;

    ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, p->log, 0, "add cleanup: %p", c);

    return c;
}

// 查詢指定的 fd,且其 handler 為 ngx_pool_cleanup_file,執行相應動作
// 這裡面有一個遍歷的操作
void
ngx_pool_run_cleanup_file(ngx_pool_t *p, ngx_fd_t fd)
{
    ngx_pool_cleanup_t       *c;
    ngx_pool_cleanup_file_t  *cf;

    for (c = p->cleanup; c; c = c->next) {
        if (c->handler == ngx_pool_cleanup_file) {

            cf = c->data;

            if (cf->fd == fd) {
                c->handler(cf);
                c->handler = NULL;
                return;
            }
        }
    }
}

// 釋放檔案描述符
void
ngx_pool_cleanup_file(void *data)
{
    ngx_pool_cleanup_file_t  *c = data;

    ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d",
                   c->fd);

    if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
        ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
                      ngx_close_file_n " \"%s\" failed", c->name);
    }
}

// 從檔案系統刪除檔案,data 指標指向一個 ngx_pool_cleanup_file_t 型別的資料
void
ngx_pool_delete_file(void *data)
{
    ngx_pool_cleanup_file_t  *c = data;

    ngx_err_t  err;

    ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, c->log, 0, "file cleanup: fd:%d %s",
                   c->fd, c->name);

    // 刪除檔案
    if (ngx_delete_file(c->name) == NGX_FILE_ERROR) {
        err = ngx_errno;

        if (err != NGX_ENOENT) {
            ngx_log_error(NGX_LOG_CRIT, c->log, err,
                          ngx_delete_file_n " \"%s\" failed", c->name);
        }
    }

    // 關閉對應的檔案描述符
    if (ngx_close_file(c->fd) == NGX_FILE_ERROR) {
        ngx_log_error(NGX_LOG_ALERT, c->log, ngx_errno,
                      ngx_close_file_n " \"%s\" failed", c->name);
    }
}


#if 0

static void *
ngx_get_cached_block(size_t size)
{
    void                     *p;
    ngx_cached_block_slot_t  *slot;

    if (ngx_cycle->cache == NULL) {
        return NULL;
    }

    slot = &ngx_cycle->cache[(size + ngx_pagesize - 1) / ngx_pagesize];

    slot->tries++;

    if (slot->number) {
        p = slot->block;
        slot->block = slot->block->next;
        slot->number--;
        return p;
    }

    return NULL;
}

-

轉載請註明來自柳大·Poechant(鍾超)的CSDN部落格

-

相關文章