blob: 835c76ced6b5d8a9cb188f89805a1d99b842fdf9 [file] [log] [blame]
Igor Sysoev369145c2004-05-28 15:49:23 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Maxim Konovalovf8d59e32012-01-18 15:07:43 +00004 * Copyright (C) Nginx, Inc.
Igor Sysoevd90282d2004-09-28 08:34:51 +00005 */
6
7
Igor Sysoev369145c2004-05-28 15:49:23 +00008#include <ngx_config.h>
9#include <ngx_core.h>
10
11
Igor Sysoev8184d1b2005-03-04 14:06:57 +000012ngx_buf_t *
13ngx_create_temp_buf(ngx_pool_t *pool, size_t size)
Igor Sysoev369145c2004-05-28 15:49:23 +000014{
15 ngx_buf_t *b;
16
Igor Sysoevc1571722005-03-19 12:38:37 +000017 b = ngx_calloc_buf(pool);
18 if (b == NULL) {
Igor Sysoev369145c2004-05-28 15:49:23 +000019 return NULL;
20 }
21
Igor Sysoevc1571722005-03-19 12:38:37 +000022 b->start = ngx_palloc(pool, size);
23 if (b->start == NULL) {
Igor Sysoev369145c2004-05-28 15:49:23 +000024 return NULL;
25 }
26
Igor Sysoev8184d1b2005-03-04 14:06:57 +000027 /*
28 * set by ngx_calloc_buf():
29 *
30 * b->file_pos = 0;
31 * b->file_last = 0;
32 * b->file = NULL;
33 * b->shadow = NULL;
34 * b->tag = 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +000035 * and flags
Igor Sysoev8184d1b2005-03-04 14:06:57 +000036 */
37
Igor Sysoev369145c2004-05-28 15:49:23 +000038 b->pos = b->start;
39 b->last = b->start;
40 b->end = b->last + size;
41 b->temporary = 1;
42
Igor Sysoev369145c2004-05-28 15:49:23 +000043 return b;
44}
45
46
Igor Sysoev8184d1b2005-03-04 14:06:57 +000047ngx_chain_t *
Igor Sysoev02f742b2005-04-08 15:18:55 +000048ngx_alloc_chain_link(ngx_pool_t *pool)
49{
50 ngx_chain_t *cl;
51
52 cl = pool->chain;
53
54 if (cl) {
55 pool->chain = cl->next;
56 return cl;
57 }
58
59 cl = ngx_palloc(pool, sizeof(ngx_chain_t));
60 if (cl == NULL) {
61 return NULL;
62 }
63
64 return cl;
65}
66
67
68ngx_chain_t *
Igor Sysoev8184d1b2005-03-04 14:06:57 +000069ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs)
Igor Sysoev369145c2004-05-28 15:49:23 +000070{
71 u_char *p;
72 ngx_int_t i;
73 ngx_buf_t *b;
74 ngx_chain_t *chain, *cl, **ll;
75
Igor Sysoevc1571722005-03-19 12:38:37 +000076 p = ngx_palloc(pool, bufs->num * bufs->size);
77 if (p == NULL) {
Igor Sysoev369145c2004-05-28 15:49:23 +000078 return NULL;
79 }
80
81 ll = &chain;
82
83 for (i = 0; i < bufs->num; i++) {
Igor Sysoevc1571722005-03-19 12:38:37 +000084
85 b = ngx_calloc_buf(pool);
86 if (b == NULL) {
Igor Sysoev369145c2004-05-28 15:49:23 +000087 return NULL;
88 }
89
Igor Sysoev8184d1b2005-03-04 14:06:57 +000090 /*
91 * set by ngx_calloc_buf():
92 *
93 * b->file_pos = 0;
94 * b->file_last = 0;
95 * b->file = NULL;
96 * b->shadow = NULL;
97 * b->tag = 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +000098 * and flags
Igor Sysoev8184d1b2005-03-04 14:06:57 +000099 *
100 */
101
Igor Sysoev369145c2004-05-28 15:49:23 +0000102 b->pos = p;
103 b->last = p;
104 b->temporary = 1;
105
106 b->start = p;
107 p += bufs->size;
108 b->end = p;
109
Igor Sysoevc1571722005-03-19 12:38:37 +0000110 cl = ngx_alloc_chain_link(pool);
111 if (cl == NULL) {
Igor Sysoev369145c2004-05-28 15:49:23 +0000112 return NULL;
113 }
114
115 cl->buf = b;
116 *ll = cl;
117 ll = &cl->next;
118 }
119
120 *ll = NULL;
121
122 return chain;
123}
124
125
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000126ngx_int_t
127ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, ngx_chain_t *in)
Igor Sysoev369145c2004-05-28 15:49:23 +0000128{
129 ngx_chain_t *cl, **ll;
130
131 ll = chain;
132
133 for (cl = *chain; cl; cl = cl->next) {
134 ll = &cl->next;
135 }
136
137 while (in) {
Igor Sysoevc1571722005-03-19 12:38:37 +0000138 cl = ngx_alloc_chain_link(pool);
139 if (cl == NULL) {
140 return NGX_ERROR;
141 }
Igor Sysoev369145c2004-05-28 15:49:23 +0000142
143 cl->buf = in->buf;
144 *ll = cl;
145 ll = &cl->next;
146 in = in->next;
147 }
148
149 *ll = NULL;
150
151 return NGX_OK;
152}
153
154
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000155ngx_chain_t *
156ngx_chain_get_free_buf(ngx_pool_t *p, ngx_chain_t **free)
157{
158 ngx_chain_t *cl;
159
160 if (*free) {
161 cl = *free;
162 *free = cl->next;
163 cl->next = NULL;
164 return cl;
165 }
166
167 cl = ngx_alloc_chain_link(p);
168 if (cl == NULL) {
169 return NULL;
170 }
171
172 cl->buf = ngx_calloc_buf(p);
173 if (cl->buf == NULL) {
174 return NULL;
175 }
176
177 cl->next = NULL;
178
179 return cl;
180}
181
182
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000183void
Maxim Dounind7c26732011-09-15 16:03:17 +0000184ngx_chain_update_chains(ngx_pool_t *p, ngx_chain_t **free, ngx_chain_t **busy,
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000185 ngx_chain_t **out, ngx_buf_tag_t tag)
Igor Sysoev369145c2004-05-28 15:49:23 +0000186{
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000187 ngx_chain_t *cl;
Igor Sysoev369145c2004-05-28 15:49:23 +0000188
189 if (*busy == NULL) {
190 *busy = *out;
191
192 } else {
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000193 for (cl = *busy; cl->next; cl = cl->next) { /* void */ }
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000194
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000195 cl->next = *out;
Igor Sysoev369145c2004-05-28 15:49:23 +0000196 }
197
198 *out = NULL;
199
200 while (*busy) {
Maxim Dounind7c26732011-09-15 16:03:17 +0000201 cl = *busy;
202
203 if (ngx_buf_size(cl->buf) != 0) {
Igor Sysoev369145c2004-05-28 15:49:23 +0000204 break;
205 }
206
Maxim Dounind7c26732011-09-15 16:03:17 +0000207 if (cl->buf->tag != tag) {
208 *busy = cl->next;
209 ngx_free_chain(p, cl);
Igor Sysoev369145c2004-05-28 15:49:23 +0000210 continue;
211 }
212
Maxim Dounind7c26732011-09-15 16:03:17 +0000213 cl->buf->pos = cl->buf->start;
214 cl->buf->last = cl->buf->start;
Igor Sysoev369145c2004-05-28 15:49:23 +0000215
Igor Sysoev8184d1b2005-03-04 14:06:57 +0000216 *busy = cl->next;
217 cl->next = *free;
218 *free = cl;
Igor Sysoev369145c2004-05-28 15:49:23 +0000219 }
220}