前言
在Nginx的源码的src/core/ngx_buf.c|h
文件中我们可以看到两中数据结构,一个是ngx_buf_t
,另一个是ngx_chain_t
。ngx_buf_t
是一个数据存储的结构体,在链表中用来保存节点的数据。这个数据可以是内存中的某一段,也可以是文件中的某一段内容。ngx_chain_t
结构体则表示链表。
储备知识
数据结构定义
ngx_buf_t
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| typedef struct ngx_buf_s ngx_buf_t;
struct ngx_buf_s { u_char *pos;
u_char *last;
off_t file_pos;
off_t file_last;
u_char *start;
u_char *end; ngx_buf_tag_t tag;
ngx_file_t *file; ngx_buf_t *shadow;
unsigned temporary:1;
unsigned memory:1;
unsigned mmap:1;
unsigned recycled:1;
unsigned in_file:1; unsigned flush:1; unsigned sync:1; unsigned last_buf:1; unsigned last_in_chain:1;
unsigned last_shadow:1; unsigned temp_file:1;
int num; };
|
ngx_chain_t
1 2 3 4 5 6
| typedef struct ngx_chain_s ngx_chain_t;
struct ngx_chain_s { ngx_buf_t *buf; ngx_chain_t *next; };
|
主要方法
ngx_buf_size
获取buf所指向数据的大小
1 2 3
| #define ngx_buf_size(b) \ (ngx_buf_in_memory(b) ? (off_t) (b->last - b->pos): \ (b->file_last - b->file_pos))
|
ngx_free_chain
释放链表,将链表放到内存池的空闲链表上,后续可以复用
1 2 3
| #define ngx_free_chain(pool, cl) \ cl->next = pool->chain; \ pool->chain = cl
|
ngx_create_temp_buf
创建并返回一个buf,数据是在内存中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| ngx_buf_t * ngx_create_temp_buf(ngx_pool_t *pool, size_t size) { ngx_buf_t *b;
b = ngx_calloc_buf(pool); if (b == NULL) { return NULL; }
b->start = ngx_palloc(pool, size); if (b->start == NULL) { return NULL; }
b->pos = b->start; b->last = b->start; b->end = b->last + size; b->temporary = 1;
return b; }
|
ngx_alloc_chain_link
创建链表,优先从内存池的可复用链表中取,这里说明了ngx_free_chain
中为什么把要释放的链表挂载到内存池的可复用链表中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| ngx_chain_t * ngx_alloc_chain_link(ngx_pool_t *pool) { ngx_chain_t *cl;
cl = pool->chain;
if (cl) { pool->chain = cl->next; return cl; }
cl = ngx_palloc(pool, sizeof(ngx_chain_t)); if (cl == NULL) { return NULL; }
return cl; }
|
ngx_create_chain_of_bufs
创建多个节点的链表,bufs结构中指定了节点的数量和每个节点buf的大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| ngx_chain_t * ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs) { u_char *p; ngx_int_t i; ngx_buf_t *b; ngx_chain_t *chain, *cl, **ll;
p = ngx_palloc(pool, bufs->num * bufs->size); if (p == NULL) { return NULL; }
ll = &chain;
for (i = 0; i < bufs->num; i++) {
b = ngx_calloc_buf(pool); if (b == NULL) { return NULL; }
b->pos = p; b->last = p; b->temporary = 1;
b->start = p; p += bufs->size; b->end = p;
cl = ngx_alloc_chain_link(pool); if (cl == NULL) { return NULL; }
cl->buf = b; *ll = cl; ll = &cl->next; }
*ll = NULL;
return chain; }
|
ngx_chain_add_copy
拷贝链表(in)并将其增加到另一个链表(chain)的尾端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| ngx_int_t ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, ngx_chain_t *in) { ngx_chain_t *cl, **ll;
ll = chain;
for (cl = *chain; cl; cl = cl->next) { ll = &cl->next; }
while (in) {
cl = ngx_alloc_chain_link(pool); if (cl == NULL) { return NGX_ERROR; }
cl->buf = in->buf; *ll = cl; ll = &cl->next; in = in->next; }
*ll = NULL;
return NGX_OK; }
|
ngx_chain_get_free_buf
获取一个buf指向内容为空的链表,会先从free(已经用完的链表)链表中去拿,否则新建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| ngx_chain_t * ngx_chain_get_free_buf(ngx_pool_t *p, ngx_chain_t **free) { ngx_chain_t *cl;
if (*free) { cl = *free; *free = cl->next; cl->next = NULL; return cl; }
cl = ngx_alloc_chain_link(p); if (cl == NULL) { return NULL; }
cl->buf = ngx_calloc_buf(p); if (cl->buf == NULL) { return NULL; }
cl->next = NULL;
return cl; }
|
ngx_chain_update_chains
清理链表,释放buf已经处理完成的链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| void ngx_chain_update_chains(ngx_pool_t *p, ngx_chain_t **free, ngx_chain_t **busy, ngx_chain_t **out, ngx_buf_tag_t tag) { ngx_chain_t *cl;
if (*out) { if (*busy == NULL) { *busy = *out;
} else { for (cl = *busy; cl->next; cl = cl->next) { }
cl->next = *out; }
*out = NULL; }
while (*busy) { cl = *busy;
if (ngx_buf_size(cl->buf) != 0) { break; }
if (cl->buf->tag != tag) { *busy = cl->next; ngx_free_chain(p, cl); continue; }
cl->buf->pos = cl->buf->start; cl->buf->last = cl->buf->start;
*busy = cl->next; cl->next = *free; *free = cl; } }
|
ngx_chain_update_sent
更新链表中的每个节点buf所指向的内容区域,移动buf指向的开始指针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| ngx_chain_t * ngx_chain_update_sent(ngx_chain_t *in, off_t sent) { off_t size;
for ( ; in; in = in->next) {
if (ngx_buf_special(in->buf)) { continue; }
if (sent == 0) { break; }
size = ngx_buf_size(in->buf);
if (sent >= size) { sent -= size;
if (ngx_buf_in_memory(in->buf)) { in->buf->pos = in->buf->last; }
if (in->buf->in_file) { in->buf->file_pos = in->buf->file_last; }
continue; }
if (ngx_buf_in_memory(in->buf)) { in->buf->pos += (size_t) sent; }
if (in->buf->in_file) { in->buf->file_pos += sent; }
break; }
return in; }
|