0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
48
|
11 ngx_buf_t *
|
|
12 ngx_create_temp_buf(ngx_pool_t *pool, size_t size)
|
0
|
13 {
|
|
14 ngx_buf_t *b;
|
|
15
|
50
|
16 b = ngx_calloc_buf(pool);
|
|
17 if (b == NULL) {
|
0
|
18 return NULL;
|
|
19 }
|
|
20
|
50
|
21 b->start = ngx_palloc(pool, size);
|
|
22 if (b->start == NULL) {
|
0
|
23 return NULL;
|
|
24 }
|
|
25
|
48
|
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;
|
|
34 *
|
|
35 */
|
|
36
|
0
|
37 b->pos = b->start;
|
|
38 b->last = b->start;
|
|
39 b->end = b->last + size;
|
|
40 b->temporary = 1;
|
|
41
|
|
42 return b;
|
|
43 }
|
|
44
|
|
45
|
48
|
46 ngx_chain_t *
|
56
|
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 *
|
48
|
68 ngx_create_chain_of_bufs(ngx_pool_t *pool, ngx_bufs_t *bufs)
|
0
|
69 {
|
|
70 u_char *p;
|
|
71 ngx_int_t i;
|
|
72 ngx_buf_t *b;
|
|
73 ngx_chain_t *chain, *cl, **ll;
|
|
74
|
50
|
75 p = ngx_palloc(pool, bufs->num * bufs->size);
|
|
76 if (p == NULL) {
|
0
|
77 return NULL;
|
|
78 }
|
|
79
|
|
80 ll = &chain;
|
|
81
|
|
82 for (i = 0; i < bufs->num; i++) {
|
50
|
83
|
|
84 b = ngx_calloc_buf(pool);
|
|
85 if (b == NULL) {
|
0
|
86 return NULL;
|
|
87 }
|
|
88
|
48
|
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;
|
|
97 *
|
|
98 */
|
|
99
|
0
|
100 b->pos = p;
|
|
101 b->last = p;
|
|
102 b->temporary = 1;
|
|
103
|
|
104 b->start = p;
|
|
105 p += bufs->size;
|
|
106 b->end = p;
|
|
107
|
50
|
108 cl = ngx_alloc_chain_link(pool);
|
|
109 if (cl == NULL) {
|
0
|
110 return NULL;
|
|
111 }
|
|
112
|
|
113 cl->buf = b;
|
|
114 *ll = cl;
|
|
115 ll = &cl->next;
|
|
116 }
|
|
117
|
|
118 *ll = NULL;
|
|
119
|
|
120 return chain;
|
|
121 }
|
|
122
|
|
123
|
48
|
124 ngx_int_t
|
|
125 ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, ngx_chain_t *in)
|
0
|
126 {
|
|
127 ngx_chain_t *cl, **ll;
|
|
128
|
|
129 ll = chain;
|
|
130
|
|
131 for (cl = *chain; cl; cl = cl->next) {
|
|
132 ll = &cl->next;
|
|
133 }
|
|
134
|
|
135 while (in) {
|
50
|
136 cl = ngx_alloc_chain_link(pool);
|
|
137 if (cl == NULL) {
|
|
138 return NGX_ERROR;
|
|
139 }
|
0
|
140
|
|
141 cl->buf = in->buf;
|
|
142 *ll = cl;
|
|
143 ll = &cl->next;
|
|
144 in = in->next;
|
|
145 }
|
|
146
|
|
147 *ll = NULL;
|
|
148
|
|
149 return NGX_OK;
|
|
150 }
|
|
151
|
|
152
|
48
|
153 void
|
|
154 ngx_chain_update_chains(ngx_chain_t **free, ngx_chain_t **busy,
|
|
155 ngx_chain_t **out, ngx_buf_tag_t tag)
|
0
|
156 {
|
48
|
157 ngx_chain_t *cl;
|
0
|
158
|
|
159 if (*busy == NULL) {
|
|
160 *busy = *out;
|
|
161
|
|
162 } else {
|
48
|
163 for (cl = *busy; cl->next; cl = cl->next) { /* void */ }
|
4
|
164
|
48
|
165 cl->next = *out;
|
0
|
166 }
|
|
167
|
|
168 *out = NULL;
|
|
169
|
|
170 while (*busy) {
|
|
171 if (ngx_buf_size((*busy)->buf) != 0) {
|
|
172 break;
|
|
173 }
|
|
174
|
18
|
175 #if (NGX_HAVE_WRITE_ZEROCOPY)
|
0
|
176 if ((*busy)->buf->zerocopy_busy) {
|
|
177 break;
|
|
178 }
|
|
179 #endif
|
|
180
|
|
181 if ((*busy)->buf->tag != tag) {
|
|
182 *busy = (*busy)->next;
|
|
183 continue;
|
|
184 }
|
|
185
|
2
|
186 (*busy)->buf->pos = (*busy)->buf->start;
|
|
187 (*busy)->buf->last = (*busy)->buf->start;
|
0
|
188
|
48
|
189 cl = *busy;
|
|
190 *busy = cl->next;
|
|
191 cl->next = *free;
|
|
192 *free = cl;
|
0
|
193 }
|
|
194 }
|