blob: 8649bdb9e310c63ca8b8e810d047f863ba82f9fe [file] [log] [blame]
Igor Sysoevd90282d2004-09-28 08:34:51 +00001
2/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev369145c2004-05-28 15:49:23 +00007#ifndef _NGX_BUF_H_INCLUDED_
8#define _NGX_BUF_H_INCLUDED_
9
10
11#include <ngx_config.h>
12#include <ngx_core.h>
13
14
Igor Sysoevd90282d2004-09-28 08:34:51 +000015typedef void * ngx_buf_tag_t;
Igor Sysoev369145c2004-05-28 15:49:23 +000016
Igor Sysoevd90282d2004-09-28 08:34:51 +000017typedef struct ngx_buf_s ngx_buf_t;
Igor Sysoev369145c2004-05-28 15:49:23 +000018
19struct ngx_buf_s {
20 u_char *pos;
21 u_char *last;
22 off_t file_pos;
23 off_t file_last;
24
Igor Sysoev369145c2004-05-28 15:49:23 +000025 u_char *start; /* start of buffer */
26 u_char *end; /* end of buffer */
27 ngx_buf_tag_t tag;
28 ngx_file_t *file;
29 ngx_buf_t *shadow;
30
31
Igor Sysoevd90282d2004-09-28 08:34:51 +000032 /* the buf's content could be changed */
Igor Sysoev369145c2004-05-28 15:49:23 +000033 unsigned temporary:1;
34
35 /*
36 * the buf's content is in a memory cache or in a read only memory
Igor Sysoevd90282d2004-09-28 08:34:51 +000037 * and must not be changed
Igor Sysoev369145c2004-05-28 15:49:23 +000038 */
39 unsigned memory:1;
40
Igor Sysoevd90282d2004-09-28 08:34:51 +000041 /* the buf's content is mmap()ed and must not be changed */
Igor Sysoev369145c2004-05-28 15:49:23 +000042 unsigned mmap:1;
Igor Sysoevd90282d2004-09-28 08:34:51 +000043
Igor Sysoev369145c2004-05-28 15:49:23 +000044 unsigned recycled:1;
45 unsigned in_file:1;
46 unsigned flush:1;
Igor Sysoev899b44e2005-05-12 14:58:06 +000047 unsigned sync:1;
Igor Sysoev369145c2004-05-28 15:49:23 +000048 unsigned last_buf:1;
Igor Sysoev899b44e2005-05-12 14:58:06 +000049 unsigned last_in_chain:1;
Igor Sysoev369145c2004-05-28 15:49:23 +000050
51 unsigned last_shadow:1;
52 unsigned temp_file:1;
53
Igor Sysoev369145c2004-05-28 15:49:23 +000054 /* STUB */ int num;
55};
56
57
Igor Sysoev369145c2004-05-28 15:49:23 +000058struct ngx_chain_s {
59 ngx_buf_t *buf;
60 ngx_chain_t *next;
61};
62
63
64typedef struct {
65 ngx_int_t num;
66 size_t size;
67} ngx_bufs_t;
68
69
Igor Sysoev02025fd2005-01-18 13:03:58 +000070typedef ngx_int_t (*ngx_output_chain_filter_pt)(void *ctx, ngx_chain_t *in);
Igor Sysoev369145c2004-05-28 15:49:23 +000071
72typedef struct {
73 ngx_buf_t *buf;
74 ngx_chain_t *in;
75 ngx_chain_t *free;
76 ngx_chain_t *busy;
77
78 unsigned sendfile;
Igor Sysoev8633e1f2008-09-05 14:48:47 +000079 unsigned directio;
Igor Sysoevfae2c002008-09-12 13:50:12 +000080#if (NGX_HAVE_ALIGNED_DIRECTIO)
81 unsigned unaligned;
82#endif
Igor Sysoev369145c2004-05-28 15:49:23 +000083 unsigned need_in_memory;
84 unsigned need_in_temp;
85
86 ngx_pool_t *pool;
87 ngx_int_t allocated;
88 ngx_bufs_t bufs;
89 ngx_buf_tag_t tag;
90
91 ngx_output_chain_filter_pt output_filter;
92 void *filter_ctx;
93} ngx_output_chain_ctx_t;
94
95
96typedef struct {
97 ngx_chain_t *out;
98 ngx_chain_t **last;
99 ngx_connection_t *connection;
100 ngx_pool_t *pool;
Igor Sysoevef066482004-06-21 15:59:32 +0000101 off_t limit;
Igor Sysoev369145c2004-05-28 15:49:23 +0000102} ngx_chain_writer_ctx_t;
103
104
105#define NGX_CHAIN_ERROR (ngx_chain_t *) NGX_ERROR
106
107
108#define ngx_buf_in_memory(b) (b->temporary || b->memory || b->mmap)
109#define ngx_buf_in_memory_only(b) (ngx_buf_in_memory(b) && !b->in_file)
Igor Sysoev3ca233e2005-12-28 14:23:52 +0000110
Igor Sysoev369145c2004-05-28 15:49:23 +0000111#define ngx_buf_special(b) \
Igor Sysoev899b44e2005-05-12 14:58:06 +0000112 ((b->flush || b->last_buf || b->sync) \
113 && !ngx_buf_in_memory(b) && !b->in_file)
Igor Sysoev369145c2004-05-28 15:49:23 +0000114
Igor Sysoev3ca233e2005-12-28 14:23:52 +0000115#define ngx_buf_sync_only(b) \
116 (b->sync \
117 && !ngx_buf_in_memory(b) && !b->in_file && !b->flush && !b->last_buf)
118
Igor Sysoev369145c2004-05-28 15:49:23 +0000119#define ngx_buf_size(b) \
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000120 (ngx_buf_in_memory(b) ? (off_t) (b->last - b->pos): \
121 (b->file_last - b->file_pos))
Igor Sysoev369145c2004-05-28 15:49:23 +0000122
Igor Sysoev369145c2004-05-28 15:49:23 +0000123ngx_buf_t *ngx_create_temp_buf(ngx_pool_t *pool, size_t size);
124ngx_chain_t *ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs);
125
126
127#define ngx_alloc_buf(pool) ngx_palloc(pool, sizeof(ngx_buf_t))
128#define ngx_calloc_buf(pool) ngx_pcalloc(pool, sizeof(ngx_buf_t))
129
Igor Sysoev02f742b2005-04-08 15:18:55 +0000130ngx_chain_t *ngx_alloc_chain_link(ngx_pool_t *pool);
131#define ngx_free_chain(pool, cl) \
132 cl->next = pool->chain; \
133 pool->chain = cl
134
Igor Sysoev369145c2004-05-28 15:49:23 +0000135
136
Igor Sysoev2f657222004-06-16 15:32:11 +0000137ngx_int_t ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in);
Igor Sysoev02025fd2005-01-18 13:03:58 +0000138ngx_int_t ngx_chain_writer(void *ctx, ngx_chain_t *in);
Igor Sysoev369145c2004-05-28 15:49:23 +0000139
Igor Sysoev2f657222004-06-16 15:32:11 +0000140ngx_int_t ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain,
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000141 ngx_chain_t *in);
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000142ngx_chain_t *ngx_chain_get_free_buf(ngx_pool_t *p, ngx_chain_t **free);
Igor Sysoev369145c2004-05-28 15:49:23 +0000143void ngx_chain_update_chains(ngx_chain_t **free, ngx_chain_t **busy,
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000144 ngx_chain_t **out, ngx_buf_tag_t tag);
Igor Sysoev369145c2004-05-28 15:49:23 +0000145
146
147#endif /* _NGX_BUF_H_INCLUDED_ */