菜鳥nginx源碼剖析資料結構篇(八) 緩沖區連結清單 ngx_chain_t
- Author:Echo Chen(陳斌)
- Email:[email protected]
- Blog:Blog.csdn.net/chen19870707
- Date:Nov 6th, 2014
1.緩沖區連結清單結構ngx_chain_t和ngx_buf_t
nginx的緩沖區連結清單如下圖所示,ngx_chain_t為連結清單,ngx_buf_t為緩沖區結點:
2.源代碼位置
頭檔案:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_buf.h
源檔案:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_buf.c
3.資料結構定義
ngx_buf_t為緩沖區結點,其定義如下:
1: typedef struct ngx_buf_s ngx_buf_t;
2: typedef void *ngx_buf_tag_t;
3: struct ngx_buf_s {
4: /*
5: * pos通常是用來告訴使用者本次應該從pos這個位置開始處理記憶體中的資料,這樣設定是因為同一個
6: * ngx_buf_t可能被多次反複處理。當然,pos的含義是由使用它的模闆定義的
7: */
8: u_char *pos;
9:
10: /* last通常表示有效的内容到此為止,注意,pos與last之間的記憶體是希望nginx處理的内容 */
11: u_char *last;
12:
13: /*
14: * 處理檔案時,file_pos與file_last的含義與處理記憶體時的pos與last相同,
15: * file_pos表示将要處理的檔案位置,file_last表示截至的檔案位置。
16: */
17: off_t file_pos;
18: off_t file_last;
19:
20: /* 如果ngx_buf_t緩沖區用于記憶體,那麼start指向這段記憶體的起始位址 */
21: u_char *start;
22:
23: /* 與start成員對應,指向緩沖區記憶體的末尾 */
24: u_char *end;
25:
26: /* 表示目前緩沖區的類型,例如由哪個子產品使用就指向這個子產品ngx_module_t變量的位址 */
27: ngx_buf_tag_t tag;
28:
29: /* 引用的檔案 */
30: ngx_file_t *file;
31:
32: /*
33: * 目前緩沖區的影子緩沖區,該成員很少用到。當緩沖區轉發上遊伺服器的響應時才使用了shadow成員,
34: * 這是因為nginx太節約記憶體了,配置設定一塊記憶體并使用ngx_buf_t表示接收到的上遊伺服器響應後,
35: * 在向下遊用戶端轉發時可能會把這塊記憶體存儲到檔案中,也可能直接向下遊發送,此時nginx絕對不會
36: * 重新複制一份記憶體用于新的目的,而是再次建立一個ngx_buf_t結構體指向原記憶體,這樣多個ngx_buf_t
37: * 結構體指向了同一份記憶體,它們之間的關系就通過shadow成員來引用,一般不建議使用。
38: */
39: ngx_buf_t *shadow;
40:
41: /* 臨時記憶體标志位,為1時表示資料在記憶體中且這段記憶體可以修改 */
42: unsigned temporay:1;
43:
44: /* 标志位,為1時表示資料在記憶體中且這段記憶體不可以修改 */
45: unsigned memory:1;
46:
47: /* 标志位,為1時表示這段記憶體是用nmap系統調用映射過來的,不可以修改 */
48: unsigned mmap:1;
49:
50: /* 标志位,為1時表示可回收 */
51: unsigned recycled:1;
52:
53: /* 标志位,為1時表示這段緩沖區處理的是檔案而不是記憶體 */
54: unsigned in_file:1;
55:
56: /* 标志位,為1時表示需要執行flush操作 */
57: unsigned flush:1;
58:
59: /*
60: * 标志位,對于操作這塊緩沖區時是否使用同步方式,需謹慎考慮,這可能會阻塞nginx程序,nginx中所有
61: * 操作幾乎都是異步的,這是它支援高并發的關鍵。有些架構代碼在sync為1時可能會有阻塞的方式進行I/O
62: * 操作,它的意義視使用它的nginx子產品而定。
63: */
64: unsigned sync:1;
65:
66: /*
67: * 标志位,表示是否是最後一塊緩沖區,因為ngx_buf_t可以由ngx_chain_t連結清單串聯起來,是以為1時,
68: * 表示目前是最後一塊待處理的緩沖區。
69: */
70: unsigned last_buf:1;
71:
72: /* 标志位,表示是否是ngx_chain_t中的最後一塊緩沖區 */
73: unsigned last_in_chain:1;
74:
75: /* 标志位,表示是否是最後一個影子緩沖區,與shadow域配合使用。通常不建議使用它 */
76: unsigned last_shadow:1;
77:
78: /* 标志位,表示目前緩沖區是否屬于臨時檔案 */
79: unsigned temp_file:1;
ngx_chain_t為緩沖區連結清單,其結構如下:80: }
1: typedef struct ngx_chain_s ngx_chain_t;
2: struct ngx_chain_s {
3: ngx_buf_t *buf; //buf指向目前的ngx_buf_t緩沖區
4: ngx_chain_t *next; //next則用來指向下一個ngx_chain_t,如果這是最後一個ngx_chain_t,則需要把next置為NULL。
5: };
4.臨時buffer建立ngx_create_tmp_buf
1: ngx_buf_t *ngx_create_temp_buf(ngx_pool_t *pool, size_t size)
2: {
3: ngx_buf_t *b;
4:
5: b = ngx_calloc_buf(pool); //配置設定ngx_buf_t
6: if (b == NULL) {
7: return NULL;
8: }
9:
10: b->start = ngx_palloc(pool, size); //給ngx_buf_t配置設定buffer
11: if (b->start == NULL) {
12: return NULL;
13: }
14:
15: /*
16: * set by ngx_calloc_buf():
17: *
18: * b->file_pos = 0;
19: * b->file_last = 0;
20: * b->file = NULL;
21: * b->shadow = NULL;
22: * b->tag = 0;
23: * and flags
24: */
25:
26: //設定起始位置pos和結束位置last,end指向緩沖區的末尾,臨時标志設定為1
27: b->pos = b->start;
28: b->last = b->start;
29: b->end = b->last + size;
30: b->temporary = 1;
31:
32: return b;
33: }
5.建立緩沖區連結清單ngx_alloc_chain_link
1: ngx_chain_t *ngx_alloc_chain_link(ngx_pool_t *pool)
2: {
3: ngx_chain_t *cl;
4:
5: cl = pool->chain;
6:
7: if (cl)
8: {
9: pool->chain = cl->next;
10: return cl;
11: }
12:
13: cl = ngx_palloc(pool, sizeof(ngx_chain_t));
14: if (cl == NULL)
15: {
16: return NULL;
17: }
18:
19: return cl;
20: }
6.緩沖區連結清單建構ngx_create_chain_of_bufs
建構如下的緩沖區連結清單,代碼比較簡單,很容易了解:
1: ngx_chain_t *ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs)
2: {
3: u_char *p;
4: ngx_int_t i;
5: ngx_buf_t *b;
6: ngx_chain_t *chain, *cl, **ll;
7:
8: //配置設定buf記憶體
9: p = ngx_palloc(pool, bufs->num * bufs->size);
10: if (p == NULL)
11: {
12: return NULL;
13: }
14:
15: ll = &chain;
16:
17: for (i = 0; i < bufs->num; i++)
18: {
19: //配置設定ngx_buf_t記憶體
20: b = ngx_calloc_buf(pool);
21: if (b == NULL)
22: {
23: return NULL;
24: }
25:
26: /*
27: * set by ngx_calloc_buf():
28: *
29: * b->file_pos = 0;
30: * b->file_last = 0;
31: * b->file = NULL;
32: * b->shadow = NULL;
33: * b->tag = 0;
34: * and flags
35: *
36: */
37:
38: b->pos = p;
39: b->last = p;
40: b->temporary = 1;
41:
42: b->start = p;
43: p += bufs->size;
44: b->end = p;
45:
46: //配置設定ngx_chain_t
47: cl = ngx_alloc_chain_link(pool);
48: if (cl == NULL)
49: {
50: return NULL;
51: }
52:
53: //
54: cl->buf = b;
55: *ll = cl;
56: ll = &cl->next;
57: }
58:
59: //最後一個結點指向NULL
60: *ll = NULL;
61:
62: return chain;
63: }
7.将其它緩沖區拷貝增加到已有緩沖區末尾ngx_chain_add_copy
1: ngx_int_t ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, ngx_chain_t *in)
2: {
3: ngx_chain_t *cl, **ll;
4:
5: ll = chain;
6:
7: //找到緩沖區末尾,即為NULL
8: for (cl = *chain; cl; cl = cl->next)
9: {
10: ll = &cl->next;
11: }
12:
13: while (in)
14: {
15: //周遊in,依次拷貝每一個結點
16: cl = ngx_alloc_chain_link(pool);
17: if (cl == NULL)
18: {
19: return NGX_ERROR;
20: }
21:
22: cl->buf = in->buf;
23: *ll = cl;
24: ll = &cl->next;
25: in = in->next;
26: }
27:
28: //緩沖區末尾指派為NULL
29: *ll = NULL;
30:
31: return NGX_OK;
32: }
8.從空閑緩沖區清單中擷取一個未使用的buf ngx_get_free_buf
ngx_chain_get_free_buf 得到連結清單中未使用的buf,如果沒有,則配置設定一個。
1: ngx_chain_t *ngx_chain_get_free_buf(ngx_pool_t *p, ngx_chain_t **free)
2: {
3: ngx_chain_t *cl;
4:
5: //若空閑連結清單中有結點,直接傳回
6: if (*free)
7: {
8: cl = *free;
9: *free = cl->next;
10: cl->next = NULL;
11: return cl;
12: }
13:
14: //否則配置設定ngx_chain_t
15: cl = ngx_alloc_chain_link(p);
16: if (cl == NULL) {
17: return NULL;
18: }
19:
20: //并給ngx_chain_t配置設定buf
21: cl->buf = ngx_calloc_buf(p);
22: if (cl->buf == NULL)
23: {
24: return NULL;
25: }
26:
27: cl->next = NULL;
28:
29: return cl;
30: }
9. buf釋放 ngx_chain_update_chains
1:
2: void ngx_chain_update_chains(ngx_pool_t *p, ngx_chain_t **free, ngx_chain_t **busy,
3: ngx_chain_t **out, ngx_buf_tag_t tag)
4: {
5: ngx_chain_t *cl;
6:
7: //讓busy指向out
8: if (*busy == NULL)
9: {
10: *busy = *out;
11:
12: }
13: else
14: {
15: for (cl = *busy; cl->next; cl = cl->next) { /* void */ }
16:
17: cl->next = *out;
18: }
19:
20: *out = NULL;
21:
22: while (*busy)
23: {
24: cl = *busy;
25:
26: //這個結點記憶體有占用,不滿足釋放條件,跳出
27: if (ngx_buf_size(cl->buf) != 0)
28: {
29: break;
30: }
31:
32: //緩沖區類型不同,直接釋放
33: if (cl->buf->tag != tag)
34: {
35: *busy = cl->next;
36: ngx_free_chain(p, cl);
37: continue;
38: }
39:
40: //将該結點放入free
41: cl->buf->pos = cl->buf->start;
42: cl->buf->last = cl->buf->start;
43:
44: *busy = cl->next;
45: cl->next = *free;
46: *free = cl;
47: }
48: }