nginx源碼分析—鏈表結構ngx_list_t

Content

1.鏈表結構

1.2 ngx_list_t的邏輯結構

2.1創建鏈表

3.一個例子

3.2如何編譯

4.小結

0. 序

 

本文繼續介紹nginx的容器——鏈表。

鏈表實現文件:文件:./src/core/ngx_list.h/.c。.表示nginx-1.0.4代碼目錄,本文爲/usr/src/nginx-1.0.4。

1. 鏈表結構

 

1.1 ngx_list_t結構

 

nginx的鏈表(頭)結構爲ngx_list_t,鏈表節點結構爲ngx_list_part_t,定義如下。

typedef struct ngx_list_part_s ngx_list_part_t;
 
struct ngx_list_part_s {      //鏈表節點結構
    void             *elts;   //指向該節點實際的數據區(該數據區中可以存放nalloc個大小爲size的元素)
    ngx_uint_t        nelts;  //實際存放的元素個數
    ngx_list_part_t  *next;   //指向下一個節點
};
 
typedef struct{              //鏈表頭結構
    ngx_list_part_t  *last;   //指向鏈表最後一個節點(part)
    ngx_list_part_t   part;   //鏈表頭中包含的第一個節點(part)
    size_t            size;   //每個元素大小
    ngx_uint_t        nalloc; //鏈表所含空間個數,即實際分配的小空間的個數
    ngx_pool_t       *pool;   //該鏈表節點空間在此內存池中分配
}ngx_list_t;

其中,sizeof(ngx_list_t)=28,sizeof(ngx_list_part_t)=12。

 

由此可見,nginx的鏈表也要從內存池中分配。對於每一個節點(list part)將分配nalloc個大小爲size的小空間,實際分配的大小爲(nalloc * size)。詳見下文的分析。

 

1.2 ngx_list_t的邏輯結構

 

ngx_list_t結構引用了ngx_pool_t結構,因此本文參考nginx-1.0.4源碼分析—內存池結構ngx_pool_t及內存管理一文畫出相關結構的邏輯圖,如下。注:本文采用UML的方式畫出該圖。

2. 鏈表操作

 

鏈表操作共3個,如下。

//創建鏈表
ngx_list_t* ngx_list_create(ngx_pool_t *pool, ngx_uint_t n, size_t size);
 
//初始化鏈表
static ngx_inline ngx_int_t ngx_list_init(ngx_list_t *list, ngx_pool_t *pool,
    ngx_uint_tn, size_t size);
 
//添加元素
void* ngx_list_push(ngx_list_t *l);

他們的實現都很簡單,本文只分析創建鏈表和添加元素操作。
 

2.1創建鏈表

 

創建鏈表的操作實現如下,首先分配鏈表頭(28B),然後分配頭節點(即鏈表頭中包含的part)數據區,兩次分配均在傳入的內存池(pool指向的內存池)中進行。然後簡單初始化鏈表頭並返回鏈表頭的起始位置。

ngx_list_t *
ngx_list_create(ngx_pool_t*pool, ngx_uint_t n, size_t size)
{
    ngx_list_t *list;
 
    list = ngx_palloc(pool,sizeof(ngx_list_t));  //從內存池中分配鏈表頭
    if (list == NULL) {
        return NULL;
    }
 
    list->part.elts =ngx_palloc(pool, n * size); //接着分配n*size大小的區域作爲鏈表數據區
    if (list->part.elts == NULL) {
        return NULL;
    }
 
    list->part.nelts = 0;     //初始化
    list->part.next = NULL;
    list->last = &list->part;
    list->size = size;
    list->nalloc = n;
    list->pool = pool;
 
    return list;    //返回鏈表頭的起始位置
}


創建鏈表後內存池的物理結構圖如下。


2.2添加元素

 

添加元素操作實現如下,同nginx數組實現類似,其實際的添加操作並不在該函數中完成。函數ngx_list_push返回可以在該鏈表數據區中放置元素(元素可以是1個或多個)的位置,而添加操作即在獲得添加位置之後進行,如後文的例子。

void *
ngx_list_push(ngx_list_t*l)
{
    void             *elt;
    ngx_list_part_t  *last;
 
    last = l->last;
 
    if (last->nelts ==l->nalloc) {  //鏈表數據區滿
 
        /* the last part is full, allocate anew list part */
 
        last =ngx_palloc(l->pool, sizeof(ngx_list_part_t));  //分配節點(list part)
        if (last == NULL) {
            return NULL;
        }
 
        last->elts =ngx_palloc(l->pool, l->nalloc * l->size);//分配該節點(part)的數據區
        if (last->elts == NULL) {
            return NULL;
        }
 
        last->nelts = 0;
        last->next = NULL;
 
        l->last->next =last;  //將分配的list part插入鏈表
        l->last = last;        //並修改list頭的last指針
    }
 
    elt = (char *)last->elts + l->size * last->nelts; //計算下一個數據在鏈表數據區中的位置
    last->nelts++;  //實際存放的數據個數加1
 
    return elt;  //返回該位置
}

由此可見,向鏈表中添加元素實際上就是從內存池中分配鏈表節點(part)及其該節點的實際數據區,並修改鏈表節點(part)信息。
 

注1:與數組的區別,數組數據區滿時要擴充數據區空間;而鏈表每次要分配節點及其數據區。

注2:鏈表的每個節點(part)的數據區中可以放置1個或多個元素,這裏的元素可以是一個整數,也可以是一個結構。

 

下圖是一個有3個節點的鏈表的邏輯結構圖。

圖中的線太多,容易眼暈,下面這個圖可能好一些。

3. 一個例子

 

理解並掌握開源軟件的最好方式莫過於自己寫一些測試代碼,或者改寫軟件本身,並進行調試來進一步理解開源軟件的原理和設計方法。本節給出一個創建內存池並從中分配一個鏈表的簡單例子。在該例中,鏈表的每個節點(part)可存放5個元素,每個元素4字節大小,創建鏈表後,要向鏈表添加15個整型元素。

 

3.1代碼

/**
 * ngx_list_t test, to test ngx_list_create, ngx_list_push
 */
 
#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_list.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;
    }
}
 
void dump_list_part(ngx_list_t* list, ngx_list_part_t* part)
{
    int *ptr = (int*)(part->elts);
    int loop = 0;
 
    printf("  .part = 0x%x\n", &(list->part));
    printf("    .elts = 0x%x  ", part->elts);
    printf("(");
    for (; loop < list->nalloc - 1; loop++)
    {
        printf("0x%x, ", ptr[loop]);
    }
    printf("0x%x)\n", ptr[loop]);
    printf("    .nelts = %d\n", part->nelts);
    printf("    .next = 0x%x", part->next);
    if (part->next)
        printf(" -->\n");
    printf(" \n");
}
 
void dump_list(ngx_list_t* list)
{
    if (list == NULL)
        return;
 
    printf("list = 0x%x\n", list);
    printf("  .last = 0x%x\n", list->last);
    printf("  .part = 0x%x\n", &(list->part));
    printf("  .size = %d\n", list->size);
    printf("  .nalloc = %d\n", list->nalloc);
    printf("  .pool = 0x%x\n\n", list->pool);
 
    printf("elements:\n");
 
    ngx_list_part_t *part = &(list->part);
    while (part)
    {
        dump_list_part(list, part);
        part = part->next;
    }
    printf("\n");
}
 
int main()
{
    ngx_pool_t *pool;
    int i;
 
    printf("--------------------------------\n");
    printf("create a new pool:\n");
    printf("--------------------------------\n");
    pool = ngx_create_pool(1024, NULL);
    dump_pool(pool);
 
    printf("--------------------------------\n");
    printf("alloc an list from the pool:\n");
    printf("--------------------------------\n");
    ngx_list_t *list = ngx_list_create(pool, 5, sizeof(int));
    dump_pool(pool);
 
    for (i = 0; i < 15; i++)
    {
        int *ptr = ngx_list_push(list);
        *ptr = i + 1;
    }
 
    printf("--------------------------------\n");
    printf("the list information:\n");
    printf("--------------------------------\n");
    dump_list(list);
 
    printf("--------------------------------\n");
    printf("the pool at the end:\n");
    printf("--------------------------------\n");
    dump_pool(pool);
 
    ngx_destroy_pool(pool);
    return 0;
}

3.2如何編譯
 

請參考nginx-1.0.4源碼分析—內存池結構ngx_pool_t及內存管理一文。本文編寫的makefile文件如下。

CXX = gcc
CXXFLAGS +=-g -Wall -Wextra
 
NGX_ROOT =/usr/src/nginx-1.0.4
 
TARGETS =ngx_list_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
NGX_LIST =$(NGX_ROOT)/objs/src/core/ngx_list.o
 
$(TARGETS):$(TARGETS_C_FILE)
$(CXX) $(CXXFLAGS) $(CORE_INCS) $(NGX_PALLOC) $(NGX_STRING)$(NGX_ALLOC) $(NGX_LIST) $^ -o $@

3.3運行結果
# ./ngx_list_t_test
-------------------------------- create a new pool:
-------------------------------- pool = 0x9208020 .d .last = 0x9208048
    .end = 0x9208420
    .next = 0x0
    .failed = 0 .max = 984
  .current = 0x9208020
  .chain = 0x0
  .large = 0x0
  .cleanup = 0x0
  .log = 0x0 available pool memory = 984
-------------------------------- alloc an list from the pool:
-------------------------------- pool = 0x9208020 .d .last = 0x9208078
    .end = 0x9208420
    .next = 0x0
    .failed = 0 .max = 984
  .current = 0x9208020
  .chain = 0x0
  .large = 0x0
  .cleanup = 0x0
  .log = 0x0 available pool memory = 936
-------------------------------- the list information:
-------------------------------- list = 0x9208048 .last = 0x9208098
  .part = 0x920804c
  .size = 4
  .nalloc = 5
  .pool = 0x9208020
elements: .part = 0x920804c .elts = 0x9208064  (0x1, 0x2, 0x3, 0x4, 0x5)
    .nelts = 5
    .next = 0x9208078 -->
  .part = 0x920804c .elts = 0x9208084  (0x6, 0x7, 0x8, 0x9, 0xa)
    .nelts = 5
    .next = 0x9208098 -->
  .part = 0x920804c .elts = 0x92080a4  (0xb, 0xc, 0xd, 0xe, 0xf)
    .nelts = 5
    .next = 0x0 
-------------------------------- the pool at the end:
-------------------------------- pool = 0x9208020 .d .last = 0x92080b8
    .end = 0x9208420
    .next = 0x0
    .failed = 0 .max = 984
  .current = 0x9208020
  .chain = 0x0
  .large = 0x0
  .cleanup = 0x0
  .log = 0x0 available pool memory = 872
該例子中內存池和數組的(內存)物理結構可參考2.3節的圖。

 

4. 小結

 

本文針對nginx-1.0.4的容器——鏈表結構進行了較爲全面的分析,包括鏈表相關數據結構,鏈表創建和向鏈表中添加元素等。最後通過一個簡單例子向讀者展示nginx鏈表創建和添加元素操作,同時藉此向讀者展示編譯測試代碼的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章