nginx 0.5.21
[nginx-catap.git] / src / core / ngx_output_chain.c
blobe2dcc4c527e3758bfd203ac65b625fa7f0fd3d93
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
12 #if 0
13 #define NGX_SENDFILE_LIMIT 4096
14 #endif
17 #define NGX_NONE 1
20 static ngx_inline ngx_int_t
21 ngx_output_chain_need_to_copy(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf);
22 static ngx_int_t ngx_output_chain_add_copy(ngx_pool_t *pool,
23 ngx_chain_t **chain, ngx_chain_t *in);
24 static ngx_int_t ngx_output_chain_copy_buf(ngx_buf_t *dst, ngx_buf_t *src,
25 ngx_uint_t sendfile);
28 ngx_int_t
29 ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in)
31 off_t bsize;
32 size_t size;
33 ngx_int_t rc, last;
34 ngx_uint_t recycled;
35 ngx_chain_t *cl, *out, **last_out;
37 if (ctx->in == NULL && ctx->busy == NULL) {
40 * the short path for the case when the ctx->in and ctx->busy chains
41 * are empty, the incoming chain is empty too or has the single buf
42 * that does not require the copy
45 if (in == NULL) {
46 return ctx->output_filter(ctx->filter_ctx, in);
49 if (in->next == NULL
50 #if (NGX_SENDFILE_LIMIT)
51 && !(in->buf->in_file && in->buf->file_last > NGX_SENDFILE_LIMIT)
52 #endif
53 && !ngx_output_chain_need_to_copy(ctx, in->buf))
55 return ctx->output_filter(ctx->filter_ctx, in);
59 /* add the incoming buf to the chain ctx->in */
61 if (in) {
62 if (ngx_output_chain_add_copy(ctx->pool, &ctx->in, in) == NGX_ERROR) {
63 return NGX_ERROR;
67 out = NULL;
68 last_out = &out;
69 last = NGX_NONE;
71 for ( ;; ) {
73 while (ctx->in) {
76 * cycle while there are the ctx->in bufs
77 * or there are the free output bufs to copy in
80 bsize = ngx_buf_size(ctx->in->buf);
82 if (bsize == 0 && !ngx_buf_special(ctx->in->buf)) {
84 ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0,
85 "zero size buf in output "
86 "t:%d r:%d f:%d %p %p-%p %p %O-%O",
87 ctx->in->buf->temporary,
88 ctx->in->buf->recycled,
89 ctx->in->buf->in_file,
90 ctx->in->buf->start,
91 ctx->in->buf->pos,
92 ctx->in->buf->last,
93 ctx->in->buf->file,
94 ctx->in->buf->file_pos,
95 ctx->in->buf->file_last);
97 ngx_debug_point();
99 ctx->in = ctx->in->next;
101 continue;
104 if (!ngx_output_chain_need_to_copy(ctx, ctx->in->buf)) {
106 /* move the chain link to the output chain */
108 cl = ctx->in;
109 ctx->in = cl->next;
111 *last_out = cl;
112 last_out = &cl->next;
113 cl->next = NULL;
115 continue;
118 if (ctx->buf == NULL) {
120 /* get the free buf */
122 if (ctx->free) {
123 cl = ctx->free;
124 ctx->buf = cl->buf;
125 ctx->free = cl->next;
126 ngx_free_chain(ctx->pool, cl);
128 } else if (out || ctx->allocated == ctx->bufs.num) {
130 break;
132 } else {
134 size = ctx->bufs.size;
135 recycled = 1;
137 if (ctx->in->buf->last_in_chain) {
139 if (bsize < (off_t) ctx->bufs.size) {
142 * allocate small temp buf for the small last buf
143 * or its small last part
146 size = (size_t) bsize;
147 recycled = 0;
149 } else if (ctx->bufs.num == 1
150 && (bsize < (off_t) (ctx->bufs.size
151 + (ctx->bufs.size >> 2))))
154 * allocate a temp buf that equals
155 * to the last buf if the last buf size is lesser
156 * than 1.25 of bufs.size and a temp buf is single
159 size = (size_t) bsize;
160 recycled = 0;
164 ctx->buf = ngx_create_temp_buf(ctx->pool, size);
165 if (ctx->buf == NULL) {
166 return NGX_ERROR;
169 ctx->buf->tag = ctx->tag;
170 ctx->buf->recycled = recycled;
171 ctx->allocated++;
175 rc = ngx_output_chain_copy_buf(ctx->buf, ctx->in->buf,
176 ctx->sendfile);
178 if (rc == NGX_ERROR) {
179 return rc;
182 if (rc == NGX_AGAIN) {
183 if (out) {
184 break;
187 return rc;
190 /* delete the completed buf from the ctx->in chain */
192 if (ngx_buf_size(ctx->in->buf) == 0) {
193 ctx->in = ctx->in->next;
196 cl = ngx_alloc_chain_link(ctx->pool);
197 if (cl == NULL) {
198 return NGX_ERROR;
201 cl->buf = ctx->buf;
202 cl->next = NULL;
203 *last_out = cl;
204 last_out = &cl->next;
205 ctx->buf = NULL;
208 if (out == NULL && last != NGX_NONE) {
210 if (ctx->in) {
211 return NGX_AGAIN;
214 return last;
217 last = ctx->output_filter(ctx->filter_ctx, out);
219 if (last == NGX_ERROR || last == NGX_DONE) {
220 return last;
223 ngx_chain_update_chains(&ctx->free, &ctx->busy, &out, ctx->tag);
224 last_out = &out;
229 static ngx_inline ngx_int_t
230 ngx_output_chain_need_to_copy(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf)
232 ngx_uint_t sendfile;
234 if (ngx_buf_special(buf)) {
235 return 0;
238 sendfile = ctx->sendfile;
240 #if (NGX_SENDFILE_LIMIT)
242 if (buf->in_file && buf->file_pos >= NGX_SENDFILE_LIMIT) {
243 sendfile = 0;
246 #endif
248 if (!sendfile) {
250 if (!ngx_buf_in_memory(buf)) {
251 return 1;
254 buf->in_file = 0;
257 if (ctx->need_in_memory && !ngx_buf_in_memory(buf)) {
258 return 1;
261 if (ctx->need_in_temp && (buf->memory || buf->mmap)) {
262 return 1;
265 return 0;
269 static ngx_int_t
270 ngx_output_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain,
271 ngx_chain_t *in)
273 ngx_chain_t *cl, **ll;
274 #if (NGX_SENDFILE_LIMIT)
275 ngx_buf_t *b, *buf;
276 #endif
278 ll = chain;
280 for (cl = *chain; cl; cl = cl->next) {
281 ll = &cl->next;
284 while (in) {
286 cl = ngx_alloc_chain_link(pool);
287 if (cl == NULL) {
288 return NGX_ERROR;
291 #if (NGX_SENDFILE_LIMIT)
293 buf = in->buf;
295 if (buf->in_file
296 && buf->file_pos < NGX_SENDFILE_LIMIT
297 && buf->file_last > NGX_SENDFILE_LIMIT)
299 b = ngx_calloc_buf(pool);
300 if (b == NULL) {
301 return NGX_ERROR;
304 ngx_memcpy(b, buf, sizeof(ngx_buf_t));
306 if (ngx_buf_in_memory(buf)) {
307 buf->pos += (ssize_t) (NGX_SENDFILE_LIMIT - buf->file_pos);
308 b->last = buf->pos;
311 buf->file_pos = NGX_SENDFILE_LIMIT;
312 b->file_last = NGX_SENDFILE_LIMIT;
314 cl->buf = b;
316 } else {
317 cl->buf = buf;
318 in = in->next;
321 #else
322 cl->buf = in->buf;
323 in = in->next;
325 #endif
327 *ll = cl;
328 ll = &cl->next;
331 *ll = NULL;
333 return NGX_OK;
337 static ngx_int_t
338 ngx_output_chain_copy_buf(ngx_buf_t *dst, ngx_buf_t *src, ngx_uint_t sendfile)
340 off_t size;
341 ssize_t n;
343 size = ngx_buf_size(src);
345 if (size > dst->end - dst->pos) {
346 size = dst->end - dst->pos;
349 #if (NGX_SENDFILE_LIMIT)
351 if (src->in_file && src->file_pos >= NGX_SENDFILE_LIMIT) {
352 sendfile = 0;
355 #endif
357 if (ngx_buf_in_memory(src)) {
358 ngx_memcpy(dst->pos, src->pos, (size_t) size);
359 src->pos += (size_t) size;
360 dst->last += (size_t) size;
362 if (src->in_file) {
364 if (sendfile) {
365 dst->in_file = 1;
366 dst->file = src->file;
367 dst->file_pos = src->file_pos;
368 dst->file_last = src->file_pos + size;
370 } else {
371 dst->in_file = 0;
374 src->file_pos += size;
376 } else {
377 dst->in_file = 0;
380 if (src->last_buf && src->pos == src->last) {
381 dst->last_buf = 1;
384 } else {
385 n = ngx_read_file(src->file, dst->pos, (size_t) size, src->file_pos);
387 if (n == NGX_ERROR) {
388 return (ngx_int_t) n;
391 #if (NGX_FILE_AIO_READ)
392 if (n == NGX_AGAIN) {
393 return (ngx_int_t) n;
395 #endif
397 if (n != size) {
398 ngx_log_error(NGX_LOG_ALERT, src->file->log, 0,
399 ngx_read_file_n " reads only %z of %O from file",
400 n, size);
401 if (n == 0) {
402 return NGX_ERROR;
406 dst->last += n;
408 if (sendfile) {
409 dst->in_file = 1;
410 dst->file = src->file;
411 dst->file_pos = src->file_pos;
412 dst->file_last = src->file_pos + n;
414 } else {
415 dst->in_file = 0;
418 src->file_pos += n;
420 if (src->last_buf && src->file_pos == src->file_last) {
421 dst->last_buf = 1;
425 return NGX_OK;
429 ngx_int_t
430 ngx_chain_writer(void *data, ngx_chain_t *in)
432 ngx_chain_writer_ctx_t *ctx = data;
434 off_t size;
435 ngx_chain_t *cl;
437 for (size = 0; in; in = in->next) {
439 #if 1
440 if (ngx_buf_size(in->buf) == 0 && !ngx_buf_special(in->buf)) {
441 ngx_debug_point();
443 #endif
445 size += ngx_buf_size(in->buf);
447 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->connection->log, 0,
448 "chain writer buf size: %uO", ngx_buf_size(in->buf));
450 cl = ngx_alloc_chain_link(ctx->pool);
451 if (cl == NULL) {
452 return NGX_ERROR;
455 cl->buf = in->buf;
456 cl->next = NULL;
457 *ctx->last = cl;
458 ctx->last = &cl->next;
461 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->connection->log, 0,
462 "chain writer in: %p", ctx->out);
464 for (cl = ctx->out; cl; cl = cl->next) {
466 #if 1
467 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
468 ngx_debug_point();
471 #endif
473 size += ngx_buf_size(cl->buf);
476 if (size == 0 && !ctx->connection->buffered) {
477 return NGX_OK;
480 ctx->out = ctx->connection->send_chain(ctx->connection, ctx->out,
481 ctx->limit);
483 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->connection->log, 0,
484 "chain writer out: %p", ctx->out);
486 if (ctx->out == NGX_CHAIN_ERROR) {
487 return NGX_ERROR;
490 if (ctx->out == NULL) {
491 ctx->last = &ctx->out;
494 if (!ctx->connection->buffered) {
495 return NGX_OK;
498 return NGX_AGAIN;