Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ngx_buf.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Nginx, Inc.
5  */
6 
7 
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 
11 
12 ngx_buf_t *
13 ngx_create_temp_buf(ngx_pool_t *pool, size_t size)
14 {
15  ngx_buf_t *b;
16 
17  b = ngx_calloc_buf(pool);
18  if (b == NULL) {
19  return NULL;
20  }
21 
22  b->start = ngx_palloc(pool, size);
23  if (b->start == NULL) {
24  return NULL;
25  }
26 
27  /*
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;
35  * and flags
36  */
37 
38  b->pos = b->start;
39  b->last = b->start;
40  b->end = b->last + size;
41  b->temporary = 1;
42 
43  return b;
44 }
45 
46 
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 
70 {
71  u_char *p;
72  ngx_int_t i;
73  ngx_buf_t *b;
74  ngx_chain_t *chain, *cl, **ll;
75 
76  p = ngx_palloc(pool, bufs->num * bufs->size);
77  if (p == NULL) {
78  return NULL;
79  }
80 
81  ll = &chain;
82 
83  for (i = 0; i < bufs->num; i++) {
84 
85  b = ngx_calloc_buf(pool);
86  if (b == NULL) {
87  return NULL;
88  }
89 
90  /*
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;
98  * and flags
99  *
100  */
101 
102  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 
110  cl = ngx_alloc_chain_link(pool);
111  if (cl == NULL) {
112  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 
126 ngx_int_t
128 {
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) {
138  cl = ngx_alloc_chain_link(pool);
139  if (cl == NULL) {
140  return NGX_ERROR;
141  }
142 
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 
155 ngx_chain_t *
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 
183 void
185  ngx_chain_t **out, ngx_buf_tag_t tag)
186 {
187  ngx_chain_t *cl;
188 
189  if (*busy == NULL) {
190  *busy = *out;
191 
192  } else {
193  for (cl = *busy; cl->next; cl = cl->next) { /* void */ }
194 
195  cl->next = *out;
196  }
197 
198  *out = NULL;
199 
200  while (*busy) {
201  cl = *busy;
202 
203  if (ngx_buf_size(cl->buf) != 0) {
204  break;
205  }
206 
207  if (cl->buf->tag != tag) {
208  *busy = cl->next;
209  ngx_free_chain(p, cl);
210  continue;
211  }
212 
213  cl->buf->pos = cl->buf->start;
214  cl->buf->last = cl->buf->start;
215 
216  *busy = cl->next;
217  cl->next = *free;
218  *free = cl;
219  }
220 }