菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t
Author:Echo Chen(陈斌)
Email:chenb19870707@gmail.com
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: * 当前缓冲区的影子缓冲区。该成员非常少用到。当缓冲区转发上游server的响应时才使用了shadow成员,
34: * 这是由于nginx太节约内存了。分配一块内存并使用ngx_buf_t表示接收到的上游server响应后,
35: * 在向下游client转发时可能会把这块内存存储到文件里,也可能直接向下游发送,此时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为缓冲区链表,其结构例如以下:
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
构建例如以下的缓冲区链表,代码比較简单。非常easy理解:
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 flags35: *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_t47: 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: //最后一个结点指向NULL60: *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_t15: cl = ngx_alloc_chain_link(p);16: if (cl == NULL) {17: return NULL;18: }19:20: //并给ngx_chain_t分配buf21: 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: }
-
Echo Chen:Blog.****.net/chen19870707
-