菜鳥nginx源碼剖析數據結構篇(八) 緩衝區鏈表 ngx_chain_tnginx
Author:Echo Chen(陳斌)服務器
Email:chenb19870707@gmail.com數據結構
Blog:Blog.csdn.net/chen19870707併發
Date:Nov 6th, 2014app
nginx的緩衝區鏈表以下圖所示,ngx_chain_t爲鏈表,ngx_buf_t爲緩衝區結點:框架
頭文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_buf.h異步
源文件:http://trac.nginx.org/nginx/browser/nginx/src/core/ngx_buf.c高併發
ngx_buf_t爲緩衝區結點,其定義以下:spa
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;
80: }
ngx_chain_t爲緩衝區鏈表,其結構以下:.net
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: };
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: }
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: }
構建以下的緩衝區鏈表,代碼比較簡單,很容易理解:
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: }
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: }
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: }
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: }