Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
| 9 | |
| 10 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 11 | ngx_buf_t * |
| 12 | ngx_create_temp_buf(ngx_pool_t *pool, size_t size) |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 13 | { |
| 14 | ngx_buf_t *b; |
| 15 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 16 | b = ngx_calloc_buf(pool); |
| 17 | if (b == NULL) { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 18 | return NULL; |
| 19 | } |
| 20 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 21 | b->start = ngx_palloc(pool, size); |
| 22 | if (b->start == NULL) { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 23 | return NULL; |
| 24 | } |
| 25 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 26 | /* |
| 27 | * set by ngx_calloc_buf(): |
| 28 | * |
| 29 | * b->file_pos = 0; |
| 30 | * b->file_last = 0; |
| 31 | * b->file = NULL; |
| 32 | * b->shadow = NULL; |
| 33 | * b->tag = 0; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 34 | * and flags |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 35 | */ |
| 36 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 37 | b->pos = b->start; |
| 38 | b->last = b->start; |
| 39 | b->end = b->last + size; |
| 40 | b->temporary = 1; |
| 41 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 42 | return b; |
| 43 | } |
| 44 | |
| 45 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 46 | ngx_chain_t * |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 47 | ngx_alloc_chain_link(ngx_pool_t *pool) |
| 48 | { |
| 49 | ngx_chain_t *cl; |
| 50 | |
| 51 | cl = pool->chain; |
| 52 | |
| 53 | if (cl) { |
| 54 | pool->chain = cl->next; |
| 55 | return cl; |
| 56 | } |
| 57 | |
| 58 | cl = ngx_palloc(pool, sizeof(ngx_chain_t)); |
| 59 | if (cl == NULL) { |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | return cl; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | ngx_chain_t * |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 68 | ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs) |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 69 | { |
| 70 | u_char *p; |
| 71 | ngx_int_t i; |
| 72 | ngx_buf_t *b; |
| 73 | ngx_chain_t *chain, *cl, **ll; |
| 74 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 75 | p = ngx_palloc(pool, bufs->num * bufs->size); |
| 76 | if (p == NULL) { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | ll = &chain; |
| 81 | |
| 82 | for (i = 0; i < bufs->num; i++) { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 83 | |
| 84 | b = ngx_calloc_buf(pool); |
| 85 | if (b == NULL) { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 86 | return NULL; |
| 87 | } |
| 88 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 89 | /* |
| 90 | * set by ngx_calloc_buf(): |
| 91 | * |
| 92 | * b->file_pos = 0; |
| 93 | * b->file_last = 0; |
| 94 | * b->file = NULL; |
| 95 | * b->shadow = NULL; |
| 96 | * b->tag = 0; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 97 | * and flags |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 98 | * |
| 99 | */ |
| 100 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 101 | b->pos = p; |
| 102 | b->last = p; |
| 103 | b->temporary = 1; |
| 104 | |
| 105 | b->start = p; |
| 106 | p += bufs->size; |
| 107 | b->end = p; |
| 108 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 109 | cl = ngx_alloc_chain_link(pool); |
| 110 | if (cl == NULL) { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | cl->buf = b; |
| 115 | *ll = cl; |
| 116 | ll = &cl->next; |
| 117 | } |
| 118 | |
| 119 | *ll = NULL; |
| 120 | |
| 121 | return chain; |
| 122 | } |
| 123 | |
| 124 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 125 | ngx_int_t |
| 126 | ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, ngx_chain_t *in) |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 127 | { |
| 128 | ngx_chain_t *cl, **ll; |
| 129 | |
| 130 | ll = chain; |
| 131 | |
| 132 | for (cl = *chain; cl; cl = cl->next) { |
| 133 | ll = &cl->next; |
| 134 | } |
| 135 | |
| 136 | while (in) { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 137 | cl = ngx_alloc_chain_link(pool); |
| 138 | if (cl == NULL) { |
| 139 | return NGX_ERROR; |
| 140 | } |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 141 | |
| 142 | cl->buf = in->buf; |
| 143 | *ll = cl; |
| 144 | ll = &cl->next; |
| 145 | in = in->next; |
| 146 | } |
| 147 | |
| 148 | *ll = NULL; |
| 149 | |
| 150 | return NGX_OK; |
| 151 | } |
| 152 | |
| 153 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 154 | ngx_chain_t * |
| 155 | ngx_chain_get_free_buf(ngx_pool_t *p, ngx_chain_t **free) |
| 156 | { |
| 157 | ngx_chain_t *cl; |
| 158 | |
| 159 | if (*free) { |
| 160 | cl = *free; |
| 161 | *free = cl->next; |
| 162 | cl->next = NULL; |
| 163 | return cl; |
| 164 | } |
| 165 | |
| 166 | cl = ngx_alloc_chain_link(p); |
| 167 | if (cl == NULL) { |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | cl->buf = ngx_calloc_buf(p); |
| 172 | if (cl->buf == NULL) { |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | cl->next = NULL; |
| 177 | |
| 178 | return cl; |
| 179 | } |
| 180 | |
| 181 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 182 | void |
| 183 | ngx_chain_update_chains(ngx_chain_t **free, ngx_chain_t **busy, |
| 184 | ngx_chain_t **out, ngx_buf_tag_t tag) |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 185 | { |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 186 | ngx_chain_t *cl; |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 187 | |
| 188 | if (*busy == NULL) { |
| 189 | *busy = *out; |
| 190 | |
| 191 | } else { |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 192 | for (cl = *busy; cl->next; cl = cl->next) { /* void */ } |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 193 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 194 | cl->next = *out; |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | *out = NULL; |
| 198 | |
| 199 | while (*busy) { |
| 200 | if (ngx_buf_size((*busy)->buf) != 0) { |
| 201 | break; |
| 202 | } |
| 203 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 204 | if ((*busy)->buf->tag != tag) { |
| 205 | *busy = (*busy)->next; |
| 206 | continue; |
| 207 | } |
| 208 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 209 | (*busy)->buf->pos = (*busy)->buf->start; |
| 210 | (*busy)->buf->last = (*busy)->buf->start; |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 211 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 212 | cl = *busy; |
| 213 | *busy = cl->next; |
| 214 | cl->next = *free; |
| 215 | *free = cl; |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 216 | } |
| 217 | } |