2004-05-28 23:49:23 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
ngx_buf_t *
|
|
|
|
ngx_create_temp_buf(ngx_pool_t *pool, size_t size)
|
2004-05-28 23:49:23 +08:00
|
|
|
{
|
|
|
|
ngx_buf_t *b;
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
b = ngx_calloc_buf(pool);
|
|
|
|
if (b == NULL) {
|
2004-05-28 23:49:23 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
b->start = ngx_palloc(pool, size);
|
|
|
|
if (b->start == NULL) {
|
2004-05-28 23:49:23 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
/*
|
|
|
|
* set by ngx_calloc_buf():
|
|
|
|
*
|
|
|
|
* b->file_pos = 0;
|
|
|
|
* b->file_last = 0;
|
|
|
|
* b->file = NULL;
|
|
|
|
* b->shadow = NULL;
|
|
|
|
* b->tag = 0;
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
b->pos = b->start;
|
|
|
|
b->last = b->start;
|
|
|
|
b->end = b->last + size;
|
|
|
|
b->temporary = 1;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-08 23:18:55 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
ngx_chain_t *
|
|
|
|
ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs)
|
2004-05-28 23:49:23 +08:00
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
ngx_int_t i;
|
|
|
|
ngx_buf_t *b;
|
|
|
|
ngx_chain_t *chain, *cl, **ll;
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
p = ngx_palloc(pool, bufs->num * bufs->size);
|
|
|
|
if (p == NULL) {
|
2004-05-28 23:49:23 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ll = &chain;
|
|
|
|
|
|
|
|
for (i = 0; i < bufs->num; i++) {
|
2005-03-19 20:38:37 +08:00
|
|
|
|
|
|
|
b = ngx_calloc_buf(pool);
|
|
|
|
if (b == NULL) {
|
2004-05-28 23:49:23 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
/*
|
|
|
|
* set by ngx_calloc_buf():
|
|
|
|
*
|
|
|
|
* b->file_pos = 0;
|
|
|
|
* b->file_last = 0;
|
|
|
|
* b->file = NULL;
|
|
|
|
* b->shadow = NULL;
|
|
|
|
* b->tag = 0;
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
b->pos = p;
|
|
|
|
b->last = p;
|
|
|
|
b->temporary = 1;
|
|
|
|
|
|
|
|
b->start = p;
|
|
|
|
p += bufs->size;
|
|
|
|
b->end = p;
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
cl = ngx_alloc_chain_link(pool);
|
|
|
|
if (cl == NULL) {
|
2004-05-28 23:49:23 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cl->buf = b;
|
|
|
|
*ll = cl;
|
|
|
|
ll = &cl->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ll = NULL;
|
|
|
|
|
|
|
|
return chain;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, ngx_chain_t *in)
|
2004-05-28 23:49:23 +08:00
|
|
|
{
|
|
|
|
ngx_chain_t *cl, **ll;
|
|
|
|
|
|
|
|
ll = chain;
|
|
|
|
|
|
|
|
for (cl = *chain; cl; cl = cl->next) {
|
|
|
|
ll = &cl->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (in) {
|
2005-03-19 20:38:37 +08:00
|
|
|
cl = ngx_alloc_chain_link(pool);
|
|
|
|
if (cl == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-05-28 23:49:23 +08:00
|
|
|
|
|
|
|
cl->buf = in->buf;
|
|
|
|
*ll = cl;
|
|
|
|
ll = &cl->next;
|
|
|
|
in = in->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ll = NULL;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
void
|
|
|
|
ngx_chain_update_chains(ngx_chain_t **free, ngx_chain_t **busy,
|
|
|
|
ngx_chain_t **out, ngx_buf_tag_t tag)
|
2004-05-28 23:49:23 +08:00
|
|
|
{
|
2005-03-04 22:06:57 +08:00
|
|
|
ngx_chain_t *cl;
|
2004-05-28 23:49:23 +08:00
|
|
|
|
|
|
|
if (*busy == NULL) {
|
|
|
|
*busy = *out;
|
|
|
|
|
|
|
|
} else {
|
2005-03-04 22:06:57 +08:00
|
|
|
for (cl = *busy; cl->next; cl = cl->next) { /* void */ }
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
cl->next = *out;
|
2004-05-28 23:49:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*out = NULL;
|
|
|
|
|
|
|
|
while (*busy) {
|
|
|
|
if (ngx_buf_size((*busy)->buf) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_HAVE_WRITE_ZEROCOPY)
|
2004-05-28 23:49:23 +08:00
|
|
|
if ((*busy)->buf->zerocopy_busy) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((*busy)->buf->tag != tag) {
|
|
|
|
*busy = (*busy)->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
(*busy)->buf->pos = (*busy)->buf->start;
|
|
|
|
(*busy)->buf->last = (*busy)->buf->start;
|
2004-05-28 23:49:23 +08:00
|
|
|
|
2005-03-04 22:06:57 +08:00
|
|
|
cl = *busy;
|
|
|
|
*busy = cl->next;
|
|
|
|
cl->next = *free;
|
|
|
|
*free = cl;
|
2004-05-28 23:49:23 +08:00
|
|
|
}
|
|
|
|
}
|