Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 1 | #ifndef _NGX_BUF_H_INCLUDED_ |
| 2 | #define _NGX_BUF_H_INCLUDED_ |
| 3 | |
| 4 | |
| 5 | #include <ngx_config.h> |
| 6 | #include <ngx_core.h> |
| 7 | |
| 8 | |
| 9 | #if 0 |
| 10 | /* the buf type */ |
| 11 | |
| 12 | /* the buf's content is in memory */ |
| 13 | #define NGX_HUNK_IN_MEMORY 0x0001 |
| 14 | /* the buf's content can be changed */ |
| 15 | #define NGX_HUNK_TEMP 0x0002 |
| 16 | /* the buf's content is in cache and can not be changed */ |
| 17 | #define NGX_HUNK_MEMORY 0x0004 |
| 18 | #define NGX_HUNK_MMAP 0x0008 |
| 19 | |
| 20 | /* the buf's content is recycled */ |
| 21 | #define NGX_HUNK_RECYCLED 0x0010 |
| 22 | |
| 23 | /* the buf's content is in a file */ |
| 24 | #define NGX_HUNK_FILE 0x0020 |
| 25 | |
| 26 | #define NGX_HUNK_STORAGE (NGX_HUNK_IN_MEMORY \ |
| 27 | |NGX_HUNK_TEMP|NGX_HUNK_MEMORY|NGX_HUNK_MMAP \ |
| 28 | |NGX_HUNK_RECYCLED|NGX_HUNK_FILE) |
| 29 | |
| 30 | /* the buf flags */ |
| 31 | |
| 32 | /* in thread state flush means to write the buf completely before return */ |
| 33 | /* in event state flush means to start to write the buf */ |
| 34 | #define NGX_HUNK_FLUSH 0x0100 |
| 35 | |
| 36 | /* the last buf */ |
| 37 | #define NGX_HUNK_LAST 0x0200 |
| 38 | |
| 39 | |
| 40 | #define NGX_HUNK_PREREAD 0x2000 |
| 41 | #define NGX_HUNK_LAST_SHADOW 0x4000 |
| 42 | #define NGX_HUNK_TEMP_FILE 0x8000 |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | typedef void * ngx_buf_tag_t; |
| 47 | |
| 48 | typedef struct ngx_buf_s ngx_buf_t; |
| 49 | |
| 50 | struct ngx_buf_s { |
| 51 | u_char *pos; |
| 52 | u_char *last; |
| 53 | off_t file_pos; |
| 54 | off_t file_last; |
| 55 | |
| 56 | int type; |
| 57 | u_char *start; /* start of buffer */ |
| 58 | u_char *end; /* end of buffer */ |
| 59 | ngx_buf_tag_t tag; |
| 60 | ngx_file_t *file; |
| 61 | ngx_buf_t *shadow; |
| 62 | |
| 63 | |
| 64 | /* the buf's content can be changed */ |
| 65 | unsigned temporary:1; |
| 66 | |
| 67 | /* |
| 68 | * the buf's content is in a memory cache or in a read only memory |
| 69 | * and can not be changed |
| 70 | */ |
| 71 | unsigned memory:1; |
| 72 | |
| 73 | /* the buf's content is mmap()ed and can not be changed */ |
| 74 | unsigned mmap:1; |
| 75 | unsigned recycled:1; |
| 76 | unsigned in_file:1; |
| 77 | unsigned flush:1; |
| 78 | unsigned last_buf:1; |
| 79 | |
| 80 | unsigned last_shadow:1; |
| 81 | unsigned temp_file:1; |
| 82 | |
| 83 | unsigned zerocopy_busy:1; |
| 84 | |
| 85 | /* STUB */ int num; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | typedef struct ngx_chain_s ngx_chain_t; |
| 90 | |
| 91 | struct ngx_chain_s { |
| 92 | ngx_buf_t *buf; |
| 93 | ngx_chain_t *next; |
| 94 | }; |
| 95 | |
| 96 | |
| 97 | typedef struct { |
| 98 | ngx_int_t num; |
| 99 | size_t size; |
| 100 | } ngx_bufs_t; |
| 101 | |
| 102 | |
| 103 | typedef int (*ngx_output_chain_filter_pt)(void *ctx, ngx_chain_t *out); |
| 104 | |
| 105 | typedef struct { |
| 106 | ngx_buf_t *buf; |
| 107 | ngx_chain_t *in; |
| 108 | ngx_chain_t *free; |
| 109 | ngx_chain_t *busy; |
| 110 | |
| 111 | unsigned sendfile; |
| 112 | unsigned need_in_memory; |
| 113 | unsigned need_in_temp; |
| 114 | |
| 115 | ngx_pool_t *pool; |
| 116 | ngx_int_t allocated; |
| 117 | ngx_bufs_t bufs; |
| 118 | ngx_buf_tag_t tag; |
| 119 | |
| 120 | ngx_output_chain_filter_pt output_filter; |
| 121 | void *filter_ctx; |
| 122 | } ngx_output_chain_ctx_t; |
| 123 | |
| 124 | |
| 125 | typedef struct { |
| 126 | ngx_chain_t *out; |
| 127 | ngx_chain_t **last; |
| 128 | ngx_connection_t *connection; |
| 129 | ngx_pool_t *pool; |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 130 | off_t limit; |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 131 | } ngx_chain_writer_ctx_t; |
| 132 | |
| 133 | |
| 134 | #define NGX_CHAIN_ERROR (ngx_chain_t *) NGX_ERROR |
| 135 | |
| 136 | |
| 137 | #define ngx_buf_in_memory(b) (b->temporary || b->memory || b->mmap) |
| 138 | #define ngx_buf_in_memory_only(b) (ngx_buf_in_memory(b) && !b->in_file) |
| 139 | #define ngx_buf_special(b) \ |
Igor Sysoev | ebfd6c8 | 2004-06-09 07:45:27 +0000 | [diff] [blame] | 140 | ((b->flush || b->last_buf) && !ngx_buf_in_memory(b) && !b->in_file) |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 141 | |
| 142 | #define ngx_buf_size(b) \ |
| 143 | (ngx_buf_in_memory(b) ? (size_t) (b->last - b->pos): \ |
| 144 | (size_t) (b->file_last - b->file_pos)) |
| 145 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 146 | ngx_buf_t *ngx_create_temp_buf(ngx_pool_t *pool, size_t size); |
| 147 | ngx_chain_t *ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs); |
| 148 | |
| 149 | |
| 150 | #define ngx_alloc_buf(pool) ngx_palloc(pool, sizeof(ngx_buf_t)) |
| 151 | #define ngx_calloc_buf(pool) ngx_pcalloc(pool, sizeof(ngx_buf_t)) |
| 152 | |
| 153 | |
| 154 | #define ngx_alloc_chain_link(pool) ngx_palloc(pool, sizeof(ngx_chain_t)) |
| 155 | |
| 156 | |
| 157 | #define ngx_alloc_link_and_set_buf(chain, b, pool, error) \ |
| 158 | do { \ |
| 159 | ngx_test_null(chain, ngx_alloc_chain_link(pool), error); \ |
| 160 | chain->buf = b; \ |
| 161 | chain->next = NULL; \ |
| 162 | } while (0); |
| 163 | |
| 164 | |
| 165 | #define ngx_chain_add_link(chain, last, cl) \ |
| 166 | if (chain) { \ |
| 167 | *last = cl; \ |
| 168 | } else { \ |
| 169 | chain = cl; \ |
| 170 | } \ |
| 171 | last = &cl->next |
| 172 | |
| 173 | |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 174 | ngx_int_t ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in); |
| 175 | ngx_int_t ngx_chain_writer(void *data, ngx_chain_t *in); |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 176 | |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 177 | ngx_int_t ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, |
| 178 | ngx_chain_t *in); |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 179 | void ngx_chain_update_chains(ngx_chain_t **free, ngx_chain_t **busy, |
| 180 | ngx_chain_t **out, ngx_buf_tag_t tag); |
| 181 | |
| 182 | |
| 183 | #endif /* _NGX_BUF_H_INCLUDED_ */ |