Add btr support to filter code.
[paraslash.git] / buffer_tree.c
1 #include <regex.h>
2 #include <stdbool.h>
3
4 #include "para.h"
5 #include "list.h"
6 #include "string.h"
7 #include "buffer_tree.h"
8 #include "error.h"
9
10
11 struct btr_buffer {
12 char *buf;
13 size_t size;
14 /** The number of references to this buffer. */
15 int refcount;
16 };
17
18 struct btr_buffer_reference {
19 struct btr_buffer *btrb;
20 size_t consumed;
21 /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
22 struct list_head node;
23 };
24
25 struct btr_node {
26 char *name;
27 struct btr_node *parent;
28 /* The position of this btr node in the buffer tree. */
29 struct list_head node;
30 /* The children nodes of this btr node are linked together in a list. */
31 struct list_head children;
32 /**
33 * The input queue is a list of references to btr buffers. Each item on
34 * the list represents an input buffer which has not been completely
35 * used by this btr node.
36 */
37 struct list_head input_queue;
38 btr_command_handler execute;
39 void *context;
40 };
41
42 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
43 &((_btrn)->children), node)
44
45 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
46 list_for_each_entry((_br), &(_btrn)->input_queue, node)
47 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
48 list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
49
50 struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
51 btr_command_handler handler, void *context)
52 {
53 struct btr_node *btrn = para_malloc(sizeof(*btrn));
54
55 btrn->name = para_strdup(name);
56 btrn->parent = parent;
57 btrn->execute = handler;
58 btrn->context = context;
59 if (parent)
60 list_add_tail(&btrn->node, &parent->children);
61 INIT_LIST_HEAD(&btrn->children);
62 INIT_LIST_HEAD(&btrn->input_queue);
63 return btrn;
64 }
65
66 /*
67 * Allocate a new btr buffer.
68 *
69 * The freshly allocated buffer will have a zero refcount.
70 */
71 static struct btr_buffer *new_btrb(char *buf, size_t size)
72 {
73 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
74
75 btrb->buf = buf;
76 btrb->size = size;
77 btrb->refcount = 0;
78 return btrb;
79 }
80
81 /*
82 * Deallocate the reference, release the resources if refcount drops to zero.
83 */
84 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
85 {
86 struct btr_buffer *btrb = br->btrb;
87
88 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
89 list_del(&br->node);
90 free(br);
91 btrb->refcount--;
92 if (btrb->refcount == 0) {
93 free(btrb->buf);
94 free(btrb);
95 }
96 }
97
98 static void add_btrb_to_children(struct btr_buffer *btrb,
99 struct btr_node *btrn, size_t consumed)
100 {
101 struct btr_node *ch;
102
103 FOR_EACH_CHILD(ch, btrn) {
104 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
105 br->btrb = btrb;
106 br->consumed = consumed;
107 list_add_tail(&br->node, &ch->input_queue);
108 btrb->refcount++;
109 }
110 }
111
112 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
113 {
114 struct btr_buffer *btrb;
115
116 btrb = new_btrb(buf, size);
117 add_btrb_to_children(btrb, btrn, 0);
118 }
119
120 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
121 {
122 add_btrb_to_children(br->btrb, btrn, br->consumed);
123 btr_drop_buffer_reference(br);
124 }
125
126 void btr_pushdown(struct btr_node *btrn)
127 {
128 struct btr_buffer_reference *br, *tmp;
129
130 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
131 btr_pushdown_br(br, btrn);
132 }
133
134 /* Return true if this node has no children. */
135 bool btr_no_children(struct btr_node *btrn)
136 {
137 return list_empty(&btrn->children);
138 }
139
140 bool btr_no_parent(struct btr_node *btrn)
141 {
142 return !btrn->parent;
143 }
144
145 bool btr_inplace_ok(struct btr_node *btrn)
146 {
147 if (!btrn->parent)
148 return true;
149 return list_is_singular(&btrn->parent->children);
150 }
151
152 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
153 {
154 return br->btrb->size - br->consumed;
155 }
156
157 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
158 {
159 *buf = br->btrb->buf + br->consumed;
160 return br_available_bytes(br);
161 }
162
163 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
164 {
165 struct btr_buffer_reference *br;
166
167 if (list_empty(&btrn->input_queue)) {
168 *bufp = NULL;
169 return 0;
170 }
171 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
172 return btr_get_buffer_by_reference(br, bufp);
173 }
174
175 void btr_consume(struct btr_node *btrn, size_t numbytes)
176 {
177 struct btr_buffer_reference *br;
178
179 assert(!list_empty(&btrn->input_queue));
180 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
181 assert(br->consumed + numbytes <= br->btrb->size);
182 br->consumed += numbytes;
183 if (br->consumed == br->btrb->size)
184 btr_drop_buffer_reference(br);
185 }
186
187 static void flush_input_queue(struct btr_node *btrn)
188 {
189 struct btr_buffer_reference *br, *tmp;
190 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
191 btr_drop_buffer_reference(br);
192 }
193
194 void btr_del_node(struct btr_node *btrn)
195 {
196 struct btr_node *ch;
197
198 if (!btrn)
199 return;
200 PARA_NOTICE_LOG("deleting %s\n", btrn->name);
201 FOR_EACH_CHILD(ch, btrn)
202 ch->parent = NULL;
203 flush_input_queue(btrn);
204 if (btrn->parent)
205 list_del(&btrn->node);
206 free(btrn->name);
207 free(btrn);
208 }
209
210 size_t btr_get_input_queue_size(struct btr_node *btrn)
211 {
212 struct btr_buffer_reference *br;
213 size_t size = 0;
214
215 FOR_EACH_BUFFER_REF(br, btrn) {
216 //PARA_CRIT_LOG("size: %zu\n", size);
217 size += br_available_bytes(br);
218 }
219 return size;
220 }
221
222 int btr_splice_out_node(struct btr_node *btrn)
223 {
224 struct btr_node *ch;
225
226 if (!btrn)
227 return -ERRNO_TO_PARA_ERROR(EINVAL);
228 if (btr_get_input_queue_size(btrn) != 0)
229 return -ERRNO_TO_PARA_ERROR(EINVAL);
230 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
231 if (btrn->parent)
232 list_del(&btrn->node);
233 FOR_EACH_CHILD(ch, btrn)
234 ch->parent = btrn->parent;
235 free(btrn->name);
236 free(btrn);
237 return 1;
238 }
239
240 /**
241 * Return the size of the largest input queue.
242 *
243 * Iterates over all children of the given node.
244 */
245 size_t btr_bytes_pending(struct btr_node *btrn)
246 {
247 size_t max_size = 0;
248 struct btr_node *ch;
249
250 FOR_EACH_CHILD(ch, btrn) {
251 size_t size = btr_get_input_queue_size(ch);
252 max_size = PARA_MAX(max_size, size);
253 }
254 return max_size;
255 }
256
257 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
258 {
259 if (!btrn)
260 return -ERRNO_TO_PARA_ERROR(EINVAL);
261 if (!btrn->execute)
262 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
263 return btrn->execute(btrn, command, value_result);
264 }
265
266 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
267 {
268 int ret;
269
270 for (; btrn; btrn = btrn->parent) {
271 struct btr_node *parent = btrn->parent;
272 PARA_CRIT_LOG("parent: %p\n", parent);
273 if (!parent)
274 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
275 if (!parent->execute)
276 continue;
277 ret = parent->execute(parent, command, value_result);
278 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
279 continue;
280 if (ret < 0)
281 return ret;
282 }
283 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
284 }
285
286 void *btr_context(struct btr_node *btrn)
287 {
288 return btrn->context;
289 }
290
291 /**
292 * Merge the first two input buffers into one.
293 *
294 * This is a quite expensive operation.
295 *
296 * \return The number of buffers that have been merged (zero, one or two).
297 */
298 int btr_merge(struct btr_node *btrn)
299 {
300 struct btr_buffer_reference *brs[2], *br;
301 char *bufs[2], *buf;
302 size_t szs[2], sz;
303 int i;
304
305 if (list_empty(&btrn->input_queue))
306 return 0;
307 if (list_is_singular(&btrn->input_queue))
308 return 1;
309 i = 0;
310 /* get references to the first two buffers */
311 FOR_EACH_BUFFER_REF(br, btrn) {
312 brs[i] = br;
313 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
314 i++;
315 if (i == 2)
316 break;
317 }
318 /* make a new btrb that combines the two buffers and a br to it. */
319 sz = szs[0] + szs[1];
320 //PARA_CRIT_LOG("merging input buffers: (%zu, %zu) -> %zu\n",
321 // szs[0], szs[1], sz);
322 buf = para_malloc(sz);
323 /* TODO: Avoid this memcopy by introducing btr buffer pool. */
324 memcpy(buf, bufs[0], szs[0]);
325 memcpy(buf + szs[0], bufs[1], szs[1]);
326
327 br = para_malloc(sizeof(*br));
328 br->btrb = new_btrb(buf, sz);
329 br->btrb->refcount = 1;
330 br->consumed = 0;
331
332 /* replace the first two refs by the new one */
333 btr_drop_buffer_reference(brs[0]);
334 btr_drop_buffer_reference(brs[1]);
335 para_list_add(&br->node, &btrn->input_queue);
336 return 2;
337 }