comparison src/event/ngx_event_pipe.c @ 343:6bdf858bff8c

nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
author Igor Sysoev <igor@sysoev.ru>
date Fri, 28 May 2004 15:49:23 +0000
parents bcbe876f4262
children e366ba5db8f8
comparison
equal deleted inserted replaced
342:0ee0642af5f1 343:6bdf858bff8c
3 #include <ngx_core.h> 3 #include <ngx_core.h>
4 #include <ngx_event.h> 4 #include <ngx_event.h>
5 #include <ngx_event_pipe.h> 5 #include <ngx_event_pipe.h>
6 6
7 7
8 static int ngx_event_pipe_read_upstream(ngx_event_pipe_t *p); 8 static ngx_int_t ngx_event_pipe_read_upstream(ngx_event_pipe_t *p);
9 static int ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p); 9 static ngx_int_t ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p);
10 10
11 static int ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p); 11 static ngx_int_t ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p);
12 ngx_inline static void ngx_event_pipe_remove_shadow_links(ngx_hunk_t *hunk); 12 ngx_inline static void ngx_event_pipe_remove_shadow_links(ngx_buf_t *buf);
13 ngx_inline static void ngx_event_pipe_free_shadow_raw_hunk(ngx_chain_t **free, 13 ngx_inline static void ngx_event_pipe_free_shadow_raw_buf(ngx_chain_t **free,
14 ngx_hunk_t *h); 14 ngx_buf_t *buf);
15 ngx_inline static void ngx_event_pipe_add_free_hunk(ngx_chain_t **chain, 15 ngx_inline static void ngx_event_pipe_add_free_buf(ngx_chain_t **chain,
16 ngx_chain_t *cl); 16 ngx_chain_t *cl);
17 static int ngx_event_pipe_drain_chains(ngx_event_pipe_t *p); 17 static ngx_int_t ngx_event_pipe_drain_chains(ngx_event_pipe_t *p);
18 18
19 19
20 int ngx_event_pipe(ngx_event_pipe_t *p, int do_write) 20 ngx_int_t ngx_event_pipe(ngx_event_pipe_t *p, int do_write)
21 { 21 {
22 u_int flags; 22 u_int flags;
23 ngx_event_t *rev, *wev; 23 ngx_event_t *rev, *wev;
24 24
25 for ( ;; ) { 25 for ( ;; ) {
71 71
72 return NGX_OK; 72 return NGX_OK;
73 } 73 }
74 74
75 75
76 int ngx_event_pipe_read_upstream(ngx_event_pipe_t *p) 76 ngx_int_t ngx_event_pipe_read_upstream(ngx_event_pipe_t *p)
77 { 77 {
78 int n, rc, size; 78 int n, rc, size;
79 ngx_hunk_t *h; 79 ngx_buf_t *b;
80 ngx_chain_t *chain, *cl, *tl; 80 ngx_chain_t *chain, *cl, *tl;
81 81
82 if (p->upstream_eof || p->upstream_error || p->upstream_done) { 82 if (p->upstream_eof || p->upstream_error || p->upstream_done) {
83 return NGX_OK; 83 return NGX_OK;
84 } 84 }
90 90
91 if (p->upstream_eof || p->upstream_error || p->upstream_done) { 91 if (p->upstream_eof || p->upstream_error || p->upstream_done) {
92 break; 92 break;
93 } 93 }
94 94
95 if (p->preread_hunks == NULL && !p->upstream->read->ready) { 95 if (p->preread_bufs == NULL && !p->upstream->read->ready) {
96 break; 96 break;
97 } 97 }
98 98
99 if (p->preread_hunks) { 99 if (p->preread_bufs) {
100 100
101 /* use the pre-read hunks if they exist */ 101 /* use the pre-read bufs if they exist */
102 102
103 chain = p->preread_hunks; 103 chain = p->preread_bufs;
104 p->preread_hunks = NULL; 104 p->preread_bufs = NULL;
105 n = p->preread_size; 105 n = p->preread_size;
106 106
107 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, 107 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
108 "pipe preread: %d", n); 108 "pipe preread: %d", n);
109 109
113 113
114 } else { 114 } else {
115 115
116 /* 116 /*
117 * kqueue notifies about the end of file or a pending error. 117 * kqueue notifies about the end of file or a pending error.
118 * This test allows not to allocate a hunk on these conditions 118 * This test allows not to allocate a buf on these conditions
119 * and not to call ngx_recv_chain(). 119 * and not to call ngx_recv_chain().
120 */ 120 */
121 121
122 if (p->upstream->read->available == 0 122 if (p->upstream->read->available == 0
123 && (p->upstream->read->kq_eof || p->upstream->read->aio_eof)) 123 && (p->upstream->read->kq_eof || p->upstream->read->aio_eof))
140 #endif 140 #endif
141 141
142 break; 142 break;
143 } 143 }
144 144
145 if (p->free_raw_hunks) { 145 if (p->free_raw_bufs) {
146 146
147 /* use the free hunks if they exist */ 147 /* use the free bufs if they exist */
148 148
149 chain = p->free_raw_hunks; 149 chain = p->free_raw_bufs;
150 if (p->single_buf) { 150 if (p->single_buf) {
151 p->free_raw_hunks = p->free_raw_hunks->next; 151 p->free_raw_bufs = p->free_raw_bufs->next;
152 chain->next = NULL; 152 chain->next = NULL;
153 } else { 153 } else {
154 p->free_raw_hunks = NULL; 154 p->free_raw_bufs = NULL;
155 } 155 }
156 156
157 } else if (p->hunks < p->bufs.num) { 157 } else if (p->allocated < p->bufs.num) {
158 158
159 /* allocate a new hunk if it's still allowed */ 159 /* allocate a new buf if it's still allowed */
160 160
161 ngx_test_null(h, ngx_create_temp_hunk(p->pool, p->bufs.size), 161 if (!(b = ngx_create_temp_buf(p->pool, p->bufs.size))) {
162 NGX_ABORT); 162 return NGX_ABORT;
163 p->hunks++; 163 }
164 164
165 ngx_alloc_link_and_set_hunk(tl, h, p->pool, NGX_ABORT); 165 p->allocated++;
166
167 ngx_alloc_link_and_set_buf(tl, b, p->pool, NGX_ABORT);
166 chain = tl; 168 chain = tl;
167 169
168 } else if (!p->cachable && p->downstream->write->ready) { 170 } else if (!p->cachable && p->downstream->write->ready) {
169 171
170 /* 172 /*
171 * if the hunks are not needed to be saved in a cache and 173 * if the bufs are not needed to be saved in a cache and
172 * a downstream is ready then write the hunks to a downstream 174 * a downstream is ready then write the bufs to a downstream
173 */ 175 */
174 176
175 p->upstream_blocked = 1; 177 p->upstream_blocked = 1;
176 178
177 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0, 179 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
182 } else if (p->cachable 184 } else if (p->cachable
183 || p->temp_file->offset < p->max_temp_file_size) 185 || p->temp_file->offset < p->max_temp_file_size)
184 { 186 {
185 187
186 /* 188 /*
187 * if it's allowed then save some hunks from r->in 189 * if it's allowed then save some bufs from r->in
188 * to a temporary file, and add them to a r->out chain 190 * to a temporary file, and add them to a r->out chain
189 */ 191 */
190 192
191 rc = ngx_event_pipe_write_chain_to_temp_file(p); 193 rc = ngx_event_pipe_write_chain_to_temp_file(p);
192 194
208 210
209 if (rc != NGX_OK) { 211 if (rc != NGX_OK) {
210 return rc; 212 return rc;
211 } 213 }
212 214
213 chain = p->free_raw_hunks; 215 chain = p->free_raw_bufs;
214 if (p->single_buf) { 216 if (p->single_buf) {
215 p->free_raw_hunks = p->free_raw_hunks->next; 217 p->free_raw_bufs = p->free_raw_bufs->next;
216 chain->next = NULL; 218 chain->next = NULL;
217 } else { 219 } else {
218 p->free_raw_hunks = NULL; 220 p->free_raw_bufs = NULL;
219 } 221 }
220 222
221 } else { 223 } else {
222 224
223 /* if there're no hunks to read in then disable a level event */ 225 /* if there're no bufs to read in then disable a level event */
224 226
225 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0, 227 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
226 "no pipe hunks to read in"); 228 "no pipe hunks to read in");
227 229
228 break; 230 break;
231 n = ngx_recv_chain(p->upstream, chain); 233 n = ngx_recv_chain(p->upstream, chain);
232 234
233 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, 235 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
234 "pipe recv chain: %d", n); 236 "pipe recv chain: %d", n);
235 237
236 if (p->free_raw_hunks) { 238 if (p->free_raw_bufs) {
237 chain->next = p->free_raw_hunks; 239 chain->next = p->free_raw_bufs;
238 } 240 }
239 p->free_raw_hunks = chain; 241 p->free_raw_bufs = chain;
240 242
241 if (n == NGX_ERROR) { 243 if (n == NGX_ERROR) {
242 p->upstream_error = 1; 244 p->upstream_error = 1;
243 return NGX_ERROR; 245 return NGX_ERROR;
244 } 246 }
245 247
246 if (n == NGX_AGAIN) { 248 if (n == NGX_AGAIN) {
247 if (p->single_buf) { 249 if (p->single_buf) {
248 ngx_event_pipe_remove_shadow_links(chain->hunk); 250 ngx_event_pipe_remove_shadow_links(chain->buf);
249 } 251 }
250 252
251 break; 253 break;
252 } 254 }
253 255
262 p->read_length += n; 264 p->read_length += n;
263 cl = chain; 265 cl = chain;
264 266
265 while (cl && n > 0) { 267 while (cl && n > 0) {
266 268
267 ngx_event_pipe_remove_shadow_links(cl->hunk); 269 ngx_event_pipe_remove_shadow_links(cl->buf);
268 270
269 size = cl->hunk->end - cl->hunk->last; 271 size = cl->buf->end - cl->buf->last;
270 272
271 if (n >= size) { 273 if (n >= size) {
272 cl->hunk->last = cl->hunk->end; 274 cl->buf->last = cl->buf->end;
273 275
274 /* STUB */ cl->hunk->num = p->num++; 276 /* STUB */ cl->buf->num = p->num++;
275 277
276 if (p->input_filter(p, cl->hunk) == NGX_ERROR) { 278 if (p->input_filter(p, cl->buf) == NGX_ERROR) {
277 return NGX_ABORT; 279 return NGX_ABORT;
278 } 280 }
279 281
280 n -= size; 282 n -= size;
281 cl = cl->next; 283 cl = cl->next;
282 284
283 } else { 285 } else {
284 cl->hunk->last += n; 286 cl->buf->last += n;
285 n = 0; 287 n = 0;
286 } 288 }
287 } 289 }
288 290
289 p->free_raw_hunks = cl; 291 p->free_raw_bufs = cl;
290 } 292 }
291 293
292 #if (NGX_DEBUG0) 294 #if (NGX_DEBUG)
293 295
294 if (p->in || p->busy || p->free_raw_hunks) { 296 if (p->in || p->busy || p->free_raw_bufs) {
295 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0, "pipe buf"); 297 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0, "pipe buf");
296 } 298 }
297 299
298 for (cl = p->in; cl; cl = cl->next) { 300 for (cl = p->in; cl; cl = cl->next) {
299 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0, 301 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
300 "pipe buf in " PTR_FMT ", pos " PTR_FMT ", size: %d", 302 "pipe buf in " PTR_FMT ", pos " PTR_FMT ", size: %d",
301 cl->hunk->start, cl->hunk->pos, 303 cl->buf->start, cl->buf->pos,
302 cl->hunk->last - cl->hunk->pos); 304 cl->buf->last - cl->buf->pos);
303 } 305 }
304 306
305 for (cl = p->busy; cl; cl = cl->next) { 307 for (cl = p->busy; cl; cl = cl->next) {
306 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0, 308 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
307 "pipe buf busy " PTR_FMT ", pos " PTR_FMT ", size: %d", 309 "pipe buf busy " PTR_FMT ", pos " PTR_FMT ", size: %d",
308 cl->hunk->start, cl->hunk->pos, 310 cl->buf->start, cl->buf->pos,
309 cl->hunk->last - cl->hunk->pos); 311 cl->buf->last - cl->buf->pos);
310 } 312 }
311 313
312 for (cl = p->free_raw_hunks; cl; cl = cl->next) { 314 for (cl = p->free_raw_bufs; cl; cl = cl->next) {
313 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0, 315 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
314 "pipe buf free " PTR_FMT ", last " PTR_FMT ", size: %d", 316 "pipe buf free " PTR_FMT ", last " PTR_FMT ", size: %d",
315 cl->hunk->start, cl->hunk->last, 317 cl->buf->start, cl->buf->last,
316 cl->hunk->end - cl->hunk->last); 318 cl->buf->end - cl->buf->last);
317 } 319 }
318 320
319 #endif 321 #endif
320 322
321 if ((p->upstream_eof || p->upstream_error) && p->free_raw_hunks) { 323 if ((p->upstream_eof || p->upstream_error) && p->free_raw_bufs) {
322 324
323 /* STUB */ p->free_raw_hunks->hunk->num = p->num++; 325 /* STUB */ p->free_raw_bufs->buf->num = p->num++;
324 326
325 if (p->input_filter(p, p->free_raw_hunks->hunk) == NGX_ERROR) { 327 if (p->input_filter(p, p->free_raw_bufs->buf) == NGX_ERROR) {
326 return NGX_ABORT; 328 return NGX_ABORT;
327 } 329 }
328 330
329 p->free_raw_hunks = p->free_raw_hunks->next; 331 p->free_raw_bufs = p->free_raw_bufs->next;
330 332
331 if (p->free_bufs) { 333 if (p->free_bufs) {
332 for (cl = p->free_raw_hunks; cl; cl = cl->next) { 334 for (cl = p->free_raw_bufs; cl; cl = cl->next) {
333 ngx_pfree(p->pool, cl->hunk->start); 335 ngx_pfree(p->pool, cl->buf->start);
334 } 336 }
335 } 337 }
336 } 338 }
337 339
338 if (p->cachable && p->in) { 340 if (p->cachable && p->in) {
343 345
344 return NGX_OK; 346 return NGX_OK;
345 } 347 }
346 348
347 349
348 int ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p) 350 ngx_int_t ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p)
349 { 351 {
350 size_t bsize; 352 size_t bsize;
351 ngx_uint_t flush; 353 ngx_uint_t flush;
352 ngx_hunk_t *h; 354 ngx_buf_t *b;
353 ngx_chain_t *out, **ll, *cl, *tl; 355 ngx_chain_t *out, **ll, *cl, *tl;
354 356
355 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, 357 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
356 "pipe write downstream: %d", p->downstream->write->ready); 358 "pipe write downstream: %d", p->downstream->write->ready);
357 359
389 } 391 }
390 392
391 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0, 393 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
392 "pipe write downstream done"); 394 "pipe write downstream done");
393 395
394 /* TODO: free unused hunks */ 396 /* TODO: free unused bufs */
395 397
396 p->downstream_done = 1; 398 p->downstream_done = 1;
397 break; 399 break;
398 } 400 }
399 401
400 if (!p->downstream->write->ready) { 402 if (!p->downstream->write->ready) {
401 break; 403 break;
402 } 404 }
403 405
404 /* bsize is the size of the busy hunks */ 406 /* bsize is the size of the busy bufs */
405 407
406 bsize = 0; 408 bsize = 0;
407 409
408 for (cl = p->busy; cl; cl = cl->next) { 410 for (cl = p->busy; cl; cl = cl->next) {
409 bsize += cl->hunk->end - cl->hunk->start; 411 bsize += cl->buf->end - cl->buf->start;
410 } 412 }
411 413
412 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, 414 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
413 "pipe write busy: " SIZE_T_FMT, bsize); 415 "pipe write busy: " SIZE_T_FMT, bsize);
414 416
418 420
419 for ( ;; ) { 421 for ( ;; ) {
420 if (p->out) { 422 if (p->out) {
421 cl = p->out; 423 cl = p->out;
422 424
423 if (bsize + ngx_hunk_size(cl->hunk) > p->busy_size) { 425 if (bsize + ngx_buf_size(cl->buf) > p->busy_size) {
424 flush = 1; 426 flush = 1;
425 break; 427 break;
426 } 428 }
427 429
428 p->out = p->out->next; 430 p->out = p->out->next;
429 ngx_event_pipe_free_shadow_raw_hunk(&p->free_raw_hunks, 431 ngx_event_pipe_free_shadow_raw_buf(&p->free_raw_bufs,
430 cl->hunk); 432 cl->buf);
431 433
432 } else if (!p->cachable && p->in) { 434 } else if (!p->cachable && p->in) {
433 cl = p->in; 435 cl = p->in;
434 436
435 if (bsize + ngx_hunk_size(cl->hunk) > p->busy_size) { 437 if (bsize + ngx_buf_size(cl->buf) > p->busy_size) {
436 flush = 1; 438 flush = 1;
437 break; 439 break;
438 } 440 }
439 441
440 p->in = p->in->next; 442 p->in = p->in->next;
441 443
442 } else { 444 } else {
443 break; 445 break;
444 } 446 }
445 447
446 bsize += ngx_hunk_size(cl->hunk); 448 bsize += ngx_buf_size(cl->buf);
447 cl->next = NULL; 449 cl->next = NULL;
448 ngx_chain_add_link(out, ll, cl); 450 ngx_chain_add_link(out, ll, cl);
449 } 451 }
450 452
451 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0, 453 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
462 464
463 ngx_chain_update_chains(&p->free, &p->busy, &out, p->tag); 465 ngx_chain_update_chains(&p->free, &p->busy, &out, p->tag);
464 466
465 for (cl = p->free; cl; cl = cl->next) { 467 for (cl = p->free; cl; cl = cl->next) {
466 468
467 if (cl->hunk->type & NGX_HUNK_TEMP_FILE) { 469 if (cl->buf->temp_file) {
468 if (p->cachable || !p->cyclic_temp_file) { 470 if (p->cachable || !p->cyclic_temp_file) {
469 continue; 471 continue;
470 } 472 }
471 473
472 /* reset p->temp_offset if all hunks had been sent */ 474 /* reset p->temp_offset if all bufs had been sent */
473 475
474 if (cl->hunk->file_last == p->temp_file->offset) { 476 if (cl->buf->file_last == p->temp_file->offset) {
475 p->temp_file->offset = 0; 477 p->temp_file->offset = 0;
476 } 478 }
477 } 479 }
478 480
479 /* TODO: free hunk if p->free_bufs && upstream done */ 481 /* TODO: free buf if p->free_bufs && upstream done */
480 482
481 /* add the free shadow raw hunk to p->free_raw_hunks */ 483 /* add the free shadow raw buf to p->free_raw_bufs */
482 484
483 if (cl->hunk->type & NGX_HUNK_LAST_SHADOW) { 485 if (cl->buf->last_shadow) {
484 h = cl->hunk->shadow; 486 b = cl->buf->shadow;
485 h->pos = h->last = h->start; 487 b->pos = b->last = b->start;
486 h->shadow = NULL; 488 b->shadow = NULL;
487 ngx_alloc_link_and_set_hunk(tl, h, p->pool, NGX_ABORT); 489 ngx_alloc_link_and_set_buf(tl, b, p->pool, NGX_ABORT);
488 ngx_event_pipe_add_free_hunk(&p->free_raw_hunks, tl); 490 ngx_event_pipe_add_free_buf(&p->free_raw_bufs, tl);
489 491
490 cl->hunk->type &= ~NGX_HUNK_LAST_SHADOW; 492 cl->buf->last_shadow = 0;
491 } 493 }
492 cl->hunk->shadow = NULL; 494
493 495 cl->buf->shadow = NULL;
494 #if 0
495 if (p->cyclic_temp_file && (cl->hunk->type & NGX_HUNK_TEMP_FILE)) {
496
497 /* reset p->temp_offset if all hunks had been sent */
498
499 if (cl->hunk->file_last == p->temp_file->offset) {
500 p->temp_file->offset = 0;
501 }
502 }
503 #endif
504 } 496 }
505 } 497 }
506 498
507 return NGX_OK; 499 return NGX_OK;
508 } 500 }
509 501
510 502
511 static int ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p) 503 static ngx_int_t ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p)
512 { 504 {
513 int size, hsize; 505 size_t size, bsize;
514 ngx_hunk_t *h; 506 ngx_buf_t *b;
515 ngx_chain_t *cl, *tl, *next, *out, **ll, **last_free, fl; 507 ngx_chain_t *cl, *tl, *next, *out, **ll, **last_free, fl;
516 508
517 if (p->hunk_to_file) { 509 if (p->buf_to_file) {
518 fl.hunk = p->hunk_to_file; 510 fl.buf = p->buf_to_file;
519 fl.next = p->in; 511 fl.next = p->in;
520 out = &fl; 512 out = &fl;
521 513
522 } else { 514 } else {
523 out = p->in; 515 out = p->in;
531 523
532 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, 524 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
533 "pipe offset: %d", p->temp_file->offset); 525 "pipe offset: %d", p->temp_file->offset);
534 526
535 do { 527 do {
536 hsize = cl->hunk->last - cl->hunk->pos; 528 bsize = cl->buf->last - cl->buf->pos;
537 529
538 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0, 530 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
539 "pipe buf " PTR_FMT ", pos " PTR_FMT ", size: %d", 531 "pipe buf " PTR_FMT ", pos " PTR_FMT ", size: %d",
540 cl->hunk->start, cl->hunk->pos, hsize); 532 cl->buf->start, cl->buf->pos, bsize);
541 533
542 if ((size + hsize > p->temp_file_write_size) 534 if ((size + bsize > p->temp_file_write_size)
543 || (p->temp_file->offset + size + hsize > p->max_temp_file_size)) 535 || (p->temp_file->offset + size + bsize > p->max_temp_file_size))
544 { 536 {
545 break; 537 break;
546 } 538 }
547 539
548 size += hsize; 540 size += bsize;
549 ll = &cl->next; 541 ll = &cl->next;
550 cl = cl->next; 542 cl = cl->next;
551 543
552 } while (cl); 544 } while (cl);
553 545
569 561
570 if (ngx_write_chain_to_temp_file(p->temp_file, out) == NGX_ERROR) { 562 if (ngx_write_chain_to_temp_file(p->temp_file, out) == NGX_ERROR) {
571 return NGX_ABORT; 563 return NGX_ABORT;
572 } 564 }
573 565
574 for (last_free = &p->free_raw_hunks; 566 for (last_free = &p->free_raw_bufs;
575 *last_free != NULL; 567 *last_free != NULL;
576 last_free = &(*last_free)->next) 568 last_free = &(*last_free)->next)
577 { 569 {
578 /* void */ 570 /* void */
579 } 571 }
580 572
581 if (p->hunk_to_file) { 573 if (p->buf_to_file) {
582 p->temp_file->offset = p->hunk_to_file->last - p->hunk_to_file->pos; 574 p->temp_file->offset = p->buf_to_file->last - p->buf_to_file->pos;
583 p->hunk_to_file = NULL; 575 p->buf_to_file = NULL;
584 out = out->next; 576 out = out->next;
585 } 577 }
586 578
587 for (cl = out; cl; cl = next) { 579 for (cl = out; cl; cl = next) {
588 next = cl->next; 580 next = cl->next;
589 cl->next = NULL; 581 cl->next = NULL;
590 582
591 h = cl->hunk; 583 b = cl->buf;
592 h->file = &p->temp_file->file; 584 b->file = &p->temp_file->file;
593 h->file_pos = p->temp_file->offset; 585 b->file_pos = p->temp_file->offset;
594 p->temp_file->offset += h->last - h->pos; 586 p->temp_file->offset += b->last - b->pos;
595 h->file_last = p->temp_file->offset; 587 b->file_last = p->temp_file->offset;
596 588
597 h->type |= NGX_HUNK_FILE|NGX_HUNK_TEMP_FILE; 589 b->in_file = 1;
598 590 b->temp_file = 1;
599 #if 0
600 if (p->cachable) {
601 h->type |= NGX_HUNK_FILE;
602 } else {
603 h->type |= NGX_HUNK_FILE|NGX_HUNK_TEMP_FILE;
604 }
605 #endif
606 591
607 ngx_chain_add_link(p->out, p->last_out, cl); 592 ngx_chain_add_link(p->out, p->last_out, cl);
608 593
609 if (h->type & NGX_HUNK_LAST_SHADOW) { 594 if (b->last_shadow) {
610 h->shadow->last = h->shadow->pos = h->shadow->start; 595 b->shadow->last = b->shadow->pos = b->shadow->start;
611 ngx_alloc_link_and_set_hunk(tl, h->shadow, p->pool, NGX_ABORT); 596 ngx_alloc_link_and_set_buf(tl, b->shadow, p->pool, NGX_ABORT);
612 *last_free = tl; 597 *last_free = tl;
613 last_free = &tl->next; 598 last_free = &tl->next;
614 } 599 }
615 } 600 }
616 601
618 } 603 }
619 604
620 605
621 /* the copy input filter */ 606 /* the copy input filter */
622 607
623 int ngx_event_pipe_copy_input_filter(ngx_event_pipe_t *p, ngx_hunk_t *hunk) 608 ngx_int_t ngx_event_pipe_copy_input_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
624 { 609 {
625 ngx_hunk_t *h; 610 ngx_buf_t *b;
626 ngx_chain_t *cl; 611 ngx_chain_t *cl;
627 612
628 if (hunk->pos == hunk->last) { 613 if (buf->pos == buf->last) {
629 return NGX_OK; 614 return NGX_OK;
630 } 615 }
631 616
632 if (p->free) { 617 if (p->free) {
633 h = p->free->hunk; 618 b = p->free->buf;
634 p->free = p->free->next; 619 p->free = p->free->next;
635 620
636 } else { 621 } else {
637 ngx_test_null(h, ngx_alloc_hunk(p->pool), NGX_ERROR); 622 if (!(b = ngx_alloc_buf(p->pool))) {
638 } 623 return NGX_ERROR;
639 624 }
640 ngx_memcpy(h, hunk, sizeof(ngx_hunk_t)); 625 }
641 h->shadow = hunk; 626
642 h->tag = p->tag; 627 ngx_memcpy(b, buf, sizeof(ngx_buf_t));
643 h->type |= NGX_HUNK_LAST_SHADOW|NGX_HUNK_RECYCLED; 628 b->shadow = buf;
644 hunk->shadow = h; 629 b->tag = p->tag;
645 630 b->last_shadow = 1;
646 ngx_alloc_link_and_set_hunk(cl, h, p->pool, NGX_ERROR); 631 b->recycled = 1;
647 632 buf->shadow = b;
648 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "hunk #%d", h->num); 633
634 ngx_alloc_link_and_set_buf(cl, b, p->pool, NGX_ERROR);
635
636 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "buf #%d", b->num);
649 637
650 ngx_chain_add_link(p->in, p->last_in, cl); 638 ngx_chain_add_link(p->in, p->last_in, cl);
651 639
652 return NGX_OK; 640 return NGX_OK;
653 } 641 }
654 642
655 643
656 ngx_inline static void ngx_event_pipe_remove_shadow_links(ngx_hunk_t *hunk) 644 ngx_inline static void ngx_event_pipe_remove_shadow_links(ngx_buf_t *buf)
657 { 645 {
658 ngx_hunk_t *h, *next; 646 ngx_buf_t *b, *next;
659 647
660 if (hunk->shadow == NULL) { 648 if (buf->shadow == NULL) {
661 return; 649 return;
662 } 650 }
663 651
664 h = hunk->shadow; 652 b = buf->shadow;
665 653
666 while (!(h->type & NGX_HUNK_LAST_SHADOW)) { 654 while (!b->last_shadow) {
667 next = h->shadow; 655 next = b->shadow;
668 h->type &= ~(NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY|NGX_HUNK_RECYCLED); 656 #if 0
669 h->shadow = NULL; 657 b->type &= ~(NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY|NGX_HUNK_RECYCLED);
670 h = next; 658 #endif
671 } 659 b->temporary = 0;
672 660 b->recycled = 0;
673 h->type &= ~(NGX_HUNK_TEMP 661 b->shadow = NULL;
662 b = next;
663 }
664
665 #if 0
666 b->type &= ~(NGX_HUNK_TEMP
674 |NGX_HUNK_IN_MEMORY 667 |NGX_HUNK_IN_MEMORY
675 |NGX_HUNK_RECYCLED 668 |NGX_HUNK_RECYCLED
676 |NGX_HUNK_LAST_SHADOW); 669 |NGX_HUNK_LAST_SHADOW);
677 h->shadow = NULL; 670 #endif
678 671
679 hunk->shadow = NULL; 672 b->temporary = 0;
680 } 673 b->recycled = 0;
681 674 b->last_shadow = 0;
682 675
683 ngx_inline static void ngx_event_pipe_free_shadow_raw_hunk(ngx_chain_t **free, 676 b->shadow = NULL;
684 ngx_hunk_t *h) 677
685 { 678 buf->shadow = NULL;
686 ngx_hunk_t *s; 679 }
680
681
682 ngx_inline static void ngx_event_pipe_free_shadow_raw_buf(ngx_chain_t **free,
683 ngx_buf_t *buf)
684 {
685 ngx_buf_t *s;
687 ngx_chain_t *cl, **ll; 686 ngx_chain_t *cl, **ll;
688 687
689 if (h->shadow == NULL) { 688 if (buf->shadow == NULL) {
690 return; 689 return;
691 } 690 }
692 691
693 for (s = h->shadow; !(s->type & NGX_HUNK_LAST_SHADOW); s = s->shadow) { 692 for (s = buf->shadow; !s->last_shadow; s = s->shadow) { /* void */ }
694 /* void */
695 }
696 693
697 ll = free; 694 ll = free;
698 695
699 for (cl = *free ; cl; cl = cl->next) { 696 for (cl = *free ; cl; cl = cl->next) {
700 if (cl->hunk == s) { 697 if (cl->buf == s) {
701 *ll = cl->next; 698 *ll = cl->next;
702 break; 699 break;
703 } 700 }
704 701
705 if (cl->hunk->shadow) { 702 if (cl->buf->shadow) {
706 break; 703 break;
707 } 704 }
708 705
709 ll = &cl->next; 706 ll = &cl->next;
710 } 707 }
711 } 708 }
712 709
713 710
714 ngx_inline static void ngx_event_pipe_add_free_hunk(ngx_chain_t **chain, 711 ngx_inline static void ngx_event_pipe_add_free_buf(ngx_chain_t **chain,
715 ngx_chain_t *cl) 712 ngx_chain_t *cl)
716 { 713 {
717 if (*chain == NULL) { 714 if (*chain == NULL) {
718 *chain = cl; 715 *chain = cl;
719 return; 716 return;
720 } 717 }
721 718
722 if ((*chain)->hunk->pos != (*chain)->hunk->last) { 719 if ((*chain)->buf->pos != (*chain)->buf->last) {
723 cl->next = (*chain)->next; 720 cl->next = (*chain)->next;
724 (*chain)->next = cl; 721 (*chain)->next = cl;
725 722
726 } else { 723 } else {
727 cl->next = (*chain); 724 cl->next = (*chain);
728 (*chain) = cl; 725 (*chain) = cl;
729 } 726 }
730 } 727 }
731 728
732 729
733 static int ngx_event_pipe_drain_chains(ngx_event_pipe_t *p) 730 static ngx_int_t ngx_event_pipe_drain_chains(ngx_event_pipe_t *p)
734 { 731 {
735 ngx_hunk_t *h; 732 ngx_buf_t *b;
736 ngx_chain_t *cl, *tl; 733 ngx_chain_t *cl, *tl;
737 734
738 for ( ;; ) { 735 for ( ;; ) {
739 if (p->busy) { 736 if (p->busy) {
740 cl = p->busy; 737 cl = p->busy;
751 } else { 748 } else {
752 return NGX_OK; 749 return NGX_OK;
753 } 750 }
754 751
755 while (cl) { 752 while (cl) {
756 if (cl->hunk->type & NGX_HUNK_LAST_SHADOW) { 753 if (cl->buf->last_shadow) {
757 h = cl->hunk->shadow; 754 b = cl->buf->shadow;
758 h->pos = h->last = h->start; 755 b->pos = b->last = b->start;
759 h->shadow = NULL; 756 b->shadow = NULL;
760 ngx_alloc_link_and_set_hunk(tl, h, p->pool, NGX_ABORT); 757 ngx_alloc_link_and_set_buf(tl, b, p->pool, NGX_ABORT);
761 ngx_event_pipe_add_free_hunk(&p->free_raw_hunks, tl); 758 ngx_event_pipe_add_free_buf(&p->free_raw_bufs, tl);
762 759
763 cl->hunk->type &= ~NGX_HUNK_LAST_SHADOW; 760 cl->buf->last_shadow = 0;
764 } 761 }
765 762
766 cl->hunk->shadow = NULL; 763 cl->buf->shadow = NULL;
767 tl = cl->next; 764 tl = cl->next;
768 cl->next = p->free; 765 cl->next = p->free;
769 p->free = cl; 766 p->free = cl;
770 cl = tl; 767 cl = tl;
771 } 768 }