Add btr support for the amp filter.
[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 #define FOR_EACH_CHILD_SAFE(_tn, _tmp, _btrn) \
45 list_for_each_entry_safe((_tn), (_tmp), &((_btrn)->children), node)
46
47 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
48 list_for_each_entry((_br), &(_btrn)->input_queue, node)
49 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
50 list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
51
52 struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
53 btr_command_handler handler, void *context)
54 {
55 struct btr_node *btrn = para_malloc(sizeof(*btrn));
56
57 btrn->name = para_strdup(name);
58 btrn->parent = parent;
59 btrn->execute = handler;
60 btrn->context = context;
61 if (parent)
62 list_add_tail(&btrn->node, &parent->children);
63 INIT_LIST_HEAD(&btrn->children);
64 INIT_LIST_HEAD(&btrn->input_queue);
65 if (parent)
66 PARA_INFO_LOG("added %s as child of %s\n", name, parent->name);
67 else
68 PARA_INFO_LOG("added %s as btr root\n", name);
69 return btrn;
70 }
71
72 /*
73 * Allocate a new btr buffer.
74 *
75 * The freshly allocated buffer will have a zero refcount.
76 */
77 static struct btr_buffer *new_btrb(char *buf, size_t size)
78 {
79 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
80
81 btrb->buf = buf;
82 btrb->size = size;
83 btrb->refcount = 0;
84 return btrb;
85 }
86
87 /*
88 * Deallocate the reference, release the resources if refcount drops to zero.
89 */
90 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
91 {
92 struct btr_buffer *btrb = br->btrb;
93
94 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
95 list_del(&br->node);
96 free(br);
97 btrb->refcount--;
98 if (btrb->refcount == 0) {
99 free(btrb->buf);
100 free(btrb);
101 }
102 }
103
104 static void add_btrb_to_children(struct btr_buffer *btrb,
105 struct btr_node *btrn, size_t consumed)
106 {
107 struct btr_node *ch;
108
109 FOR_EACH_CHILD(ch, btrn) {
110 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
111 br->btrb = btrb;
112 br->consumed = consumed;
113 list_add_tail(&br->node, &ch->input_queue);
114 btrb->refcount++;
115 }
116 }
117
118 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
119 {
120 struct btr_buffer *btrb;
121
122 btrb = new_btrb(buf, size);
123 add_btrb_to_children(btrb, btrn, 0);
124 }
125
126 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
127 {
128 add_btrb_to_children(br->btrb, btrn, br->consumed);
129 btr_drop_buffer_reference(br);
130 }
131
132 void btr_pushdown(struct btr_node *btrn)
133 {
134 struct btr_buffer_reference *br, *tmp;
135
136 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
137 btr_pushdown_br(br, btrn);
138 }
139
140 int btr_pushdown_one(struct btr_node *btrn)
141 {
142 struct btr_buffer_reference *br;
143
144 if (list_empty(&btrn->input_queue))
145 return 0;
146 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
147 btr_pushdown_br(br, btrn);
148 return 1;
149 }
150
151 /* Return true if this node has no children. */
152 bool btr_no_children(struct btr_node *btrn)
153 {
154 return list_empty(&btrn->children);
155 }
156
157 bool btr_no_parent(struct btr_node *btrn)
158 {
159 return !btrn->parent;
160 }
161
162 bool btr_inplace_ok(struct btr_node *btrn)
163 {
164 if (!btrn->parent)
165 return true;
166 return list_is_singular(&btrn->parent->children);
167 }
168
169 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
170 {
171 return br->btrb->size - br->consumed;
172 }
173
174 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
175 {
176 *buf = br->btrb->buf + br->consumed;
177 return br_available_bytes(br);
178 }
179
180 /**
181 * \return zero if the input buffer queue is empty.
182 */
183 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
184 {
185 struct btr_buffer_reference *br;
186
187 if (list_empty(&btrn->input_queue)) {
188 *bufp = NULL;
189 return 0;
190 }
191 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
192 return btr_get_buffer_by_reference(br, bufp);
193 }
194
195 void btr_consume(struct btr_node *btrn, size_t numbytes)
196 {
197 struct btr_buffer_reference *br;
198
199 assert(!list_empty(&btrn->input_queue));
200 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
201 assert(br->consumed + numbytes <= br->btrb->size);
202 br->consumed += numbytes;
203 if (br->consumed == br->btrb->size)
204 btr_drop_buffer_reference(br);
205 }
206
207 static void flush_input_queue(struct btr_node *btrn)
208 {
209 struct btr_buffer_reference *br, *tmp;
210 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
211 btr_drop_buffer_reference(br);
212 }
213
214 void btr_del_node(struct btr_node *btrn)
215 {
216 struct btr_node *ch;
217
218 if (!btrn)
219 return;
220 PARA_NOTICE_LOG("deleting %s\n", btrn->name);
221 FOR_EACH_CHILD(ch, btrn)
222 ch->parent = NULL;
223 flush_input_queue(btrn);
224 if (btrn->parent)
225 list_del(&btrn->node);
226 free(btrn->name);
227 free(btrn);
228 }
229
230 size_t btr_get_input_queue_size(struct btr_node *btrn)
231 {
232 struct btr_buffer_reference *br;
233 size_t size = 0;
234
235 FOR_EACH_BUFFER_REF(br, btrn) {
236 //PARA_CRIT_LOG("size: %zu\n", size);
237 size += br_available_bytes(br);
238 }
239 return size;
240 }
241
242 void btr_splice_out_node(struct btr_node *btrn)
243 {
244 struct btr_node *ch, *tmp;
245
246 assert(btrn);
247 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
248 btr_pushdown(btrn);
249 if (btrn->parent)
250 list_del(&btrn->node);
251 FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
252 PARA_INFO_LOG("parent(%s): %s\n", ch->name,
253 btrn->parent? btrn->parent->name : "NULL");
254 ch->parent = btrn->parent;
255 if (btrn->parent)
256 list_move(&ch->node, &btrn->parent->children);
257 }
258 assert(list_empty(&btrn->children));
259 free(btrn->name);
260 free(btrn);
261 }
262
263 /**
264 * Return the size of the largest input queue.
265 *
266 * Iterates over all children of the given node.
267 */
268 size_t btr_bytes_pending(struct btr_node *btrn)
269 {
270 size_t max_size = 0;
271 struct btr_node *ch;
272
273 FOR_EACH_CHILD(ch, btrn) {
274 size_t size = btr_get_input_queue_size(ch);
275 max_size = PARA_MAX(max_size, size);
276 }
277 return max_size;
278 }
279
280 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
281 {
282 if (!btrn)
283 return -ERRNO_TO_PARA_ERROR(EINVAL);
284 if (!btrn->execute)
285 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
286 return btrn->execute(btrn, command, value_result);
287 }
288
289 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
290 {
291 int ret;
292
293 for (; btrn; btrn = btrn->parent) {
294 struct btr_node *parent = btrn->parent;
295 if (!parent)
296 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
297 if (!parent->execute)
298 continue;
299 PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
300 ret = parent->execute(parent, command, value_result);
301 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
302 continue;
303 if (ret < 0)
304 return ret;
305 if (value_result && *value_result)
306 PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
307 *value_result);
308 return 1;
309 }
310 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
311 }
312
313 void *btr_context(struct btr_node *btrn)
314 {
315 return btrn->context;
316 }
317
318 /**
319 * Merge the first two input buffers into one.
320 *
321 * This is a quite expensive operation.
322 *
323 * \return The number of buffers that have been available (zero, one or two).
324 */
325 static int merge_input(struct btr_node *btrn)
326 {
327 struct btr_buffer_reference *brs[2], *br;
328 char *bufs[2], *buf;
329 size_t szs[2], sz;
330 int i;
331
332 if (list_empty(&btrn->input_queue))
333 return 0;
334 if (list_is_singular(&btrn->input_queue))
335 return 1;
336 i = 0;
337 /* get references to the first two buffers */
338 FOR_EACH_BUFFER_REF(br, btrn) {
339 brs[i] = br;
340 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
341 i++;
342 if (i == 2)
343 break;
344 }
345 /* make a new btrb that combines the two buffers and a br to it. */
346 sz = szs[0] + szs[1];
347 //PARA_CRIT_LOG("merging input buffers: (%zu, %zu) -> %zu\n",
348 // szs[0], szs[1], sz);
349 buf = para_malloc(sz);
350 /* TODO: Avoid this memcopy by introducing btr buffer pool. */
351 memcpy(buf, bufs[0], szs[0]);
352 memcpy(buf + szs[0], bufs[1], szs[1]);
353
354 br = para_malloc(sizeof(*br));
355 br->btrb = new_btrb(buf, sz);
356 br->btrb->refcount = 1;
357 br->consumed = 0;
358
359 /* replace the first two refs by the new one */
360 btr_drop_buffer_reference(brs[0]);
361 btr_drop_buffer_reference(brs[1]);
362 para_list_add(&br->node, &btrn->input_queue);
363 return 2;
364 }
365
366 void btr_merge(struct btr_node *btrn, size_t dest_size)
367 {
368 for (;;) {
369 char *buf;
370 size_t len = btr_next_buffer(btrn, &buf);
371 if (len >= dest_size)
372 return;
373 if (merge_input(btrn) < 2)
374 return;
375 }
376 }
377
378 bool btr_eof(struct btr_node *btrn)
379 {
380 char *buf;
381 size_t len = btr_next_buffer(btrn, &buf);
382
383 return (len == 0 && btr_no_parent(btrn));
384 }
385
386 void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
387 {
388 struct btr_node *ch;
389 const char spaces[] = " ", *space = spaces + 16 - depth;
390
391 if (depth > 16)
392 return;
393 para_log(loglevel, "%s%s\n", space, btrn->name);
394 FOR_EACH_CHILD(ch, btrn)
395 log_tree_recursively(ch, loglevel, depth + 1);
396 }
397
398 void btr_log_tree(struct btr_node *btrn, int loglevel)
399 {
400 return log_tree_recursively(btrn, loglevel, 0);
401 }