btr_add_output(): Fix memory leak in case node has no children.
[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         assert(size != 0);
123         if (list_empty(&btrn->children)) {
124                 free(buf);
125                 return;
126         }
127         btrb = new_btrb(buf, size);
128         add_btrb_to_children(btrb, btrn, 0);
129 }
130
131 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
132 {
133         add_btrb_to_children(br->btrb, btrn, br->consumed);
134         btr_drop_buffer_reference(br);
135 }
136
137 void btr_pushdown(struct btr_node *btrn)
138 {
139         struct btr_buffer_reference *br, *tmp;
140
141         FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
142                 btr_pushdown_br(br, btrn);
143 }
144
145 int btr_pushdown_one(struct btr_node *btrn)
146 {
147         struct btr_buffer_reference *br;
148
149         if (list_empty(&btrn->input_queue))
150                 return 0;
151         br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
152         btr_pushdown_br(br, btrn);
153         return 1;
154 }
155
156 /* Return true if this node has no children. */
157 bool btr_no_children(struct btr_node *btrn)
158 {
159         return list_empty(&btrn->children);
160 }
161
162 bool btr_no_parent(struct btr_node *btrn)
163 {
164         return !btrn->parent;
165 }
166
167 bool btr_inplace_ok(struct btr_node *btrn)
168 {
169         if (!btrn->parent)
170                 return true;
171         return list_is_singular(&btrn->parent->children);
172 }
173
174 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
175 {
176         return br->btrb->size - br->consumed;
177 }
178
179 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
180 {
181         *buf = br->btrb->buf + br->consumed;
182         return br_available_bytes(br);
183 }
184
185 /**
186  * \return zero if the input buffer queue is empty.
187  */
188 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
189 {
190         struct btr_buffer_reference *br;
191
192         if (list_empty(&btrn->input_queue)) {
193                 *bufp = NULL;
194                 return 0;
195         }
196         br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
197         return btr_get_buffer_by_reference(br, bufp);
198 }
199
200 void btr_consume(struct btr_node *btrn, size_t numbytes)
201 {
202         struct btr_buffer_reference *br;
203
204         assert(!list_empty(&btrn->input_queue));
205         br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
206         assert(br->consumed + numbytes <= br->btrb->size);
207         br->consumed += numbytes;
208         if (br->consumed == br->btrb->size)
209                 btr_drop_buffer_reference(br);
210 }
211
212 static void flush_input_queue(struct btr_node *btrn)
213 {
214         struct btr_buffer_reference *br, *tmp;
215         FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
216                 btr_drop_buffer_reference(br);
217 }
218
219 void btr_del_node(struct btr_node *btrn)
220 {
221         struct btr_node *ch;
222
223         if (!btrn)
224                 return;
225         PARA_NOTICE_LOG("deleting %s\n", btrn->name);
226         FOR_EACH_CHILD(ch, btrn)
227                 ch->parent = NULL;
228         flush_input_queue(btrn);
229         if (btrn->parent)
230                 list_del(&btrn->node);
231         free(btrn->name);
232         free(btrn);
233 }
234
235 size_t btr_get_input_queue_size(struct btr_node *btrn)
236 {
237         struct btr_buffer_reference *br;
238         size_t size = 0;
239
240         FOR_EACH_BUFFER_REF(br, btrn) {
241                 //PARA_CRIT_LOG("size: %zu\n", size);
242                 size += br_available_bytes(br);
243         }
244         return size;
245 }
246
247 void btr_splice_out_node(struct btr_node *btrn)
248 {
249         struct btr_node *ch, *tmp;
250
251         assert(btrn);
252         PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
253         btr_pushdown(btrn);
254         if (btrn->parent)
255                 list_del(&btrn->node);
256         FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
257                 PARA_INFO_LOG("parent(%s): %s\n", ch->name,
258                         btrn->parent? btrn->parent->name : "NULL");
259                 ch->parent = btrn->parent;
260                 if (btrn->parent)
261                         list_move(&ch->node, &btrn->parent->children);
262         }
263         assert(list_empty(&btrn->children));
264         free(btrn->name);
265         free(btrn);
266 }
267
268 /**
269  * Return the size of the largest input queue.
270  *
271  * Iterates over all children of the given node.
272  */
273 size_t btr_bytes_pending(struct btr_node *btrn)
274 {
275         size_t max_size = 0;
276         struct btr_node *ch;
277
278         FOR_EACH_CHILD(ch, btrn) {
279                 size_t size = btr_get_input_queue_size(ch);
280                 max_size = PARA_MAX(max_size, size);
281         }
282         return max_size;
283 }
284
285 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
286 {
287         if (!btrn)
288                 return -ERRNO_TO_PARA_ERROR(EINVAL);
289         if (!btrn->execute)
290                 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
291         return btrn->execute(btrn, command, value_result);
292 }
293
294 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
295 {
296         int ret;
297
298         for (; btrn; btrn = btrn->parent) {
299                 struct btr_node *parent = btrn->parent;
300                 if (!parent)
301                         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
302                 if (!parent->execute)
303                         continue;
304                 PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
305                 ret = parent->execute(parent, command, value_result);
306                 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
307                         continue;
308                 if (ret < 0)
309                         return ret;
310                 if (value_result && *value_result)
311                         PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
312                                 *value_result);
313                 return 1;
314         }
315         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
316 }
317
318 void *btr_context(struct btr_node *btrn)
319 {
320         return btrn->context;
321 }
322
323 /**
324  * Merge the first two input buffers into one.
325  *
326  * This is a quite expensive operation.
327  *
328  * \return The number of buffers that have been available (zero, one or two).
329  */
330 static int merge_input(struct btr_node *btrn)
331 {
332         struct btr_buffer_reference *brs[2], *br;
333         char *bufs[2], *buf;
334         size_t szs[2], sz;
335         int i;
336
337         if (list_empty(&btrn->input_queue))
338                 return 0;
339         if (list_is_singular(&btrn->input_queue))
340                 return 1;
341         i = 0;
342         /* get references to the first two buffers */
343         FOR_EACH_BUFFER_REF(br, btrn) {
344                 brs[i] = br;
345                 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
346                 i++;
347                 if (i == 2)
348                         break;
349         }
350         /* make a new btrb that combines the two buffers and a br to it. */
351         sz = szs[0] + szs[1];
352         //PARA_CRIT_LOG("merging input buffers: (%zu, %zu) -> %zu\n",
353         //      szs[0], szs[1], sz);
354         buf = para_malloc(sz);
355         /* TODO: Avoid this memcopy by introducing btr buffer pool. */
356         memcpy(buf, bufs[0], szs[0]);
357         memcpy(buf + szs[0], bufs[1], szs[1]);
358
359         br = para_malloc(sizeof(*br));
360         br->btrb = new_btrb(buf, sz);
361         br->btrb->refcount = 1;
362         br->consumed = 0;
363
364         /* replace the first two refs by the new one */
365         btr_drop_buffer_reference(brs[0]);
366         btr_drop_buffer_reference(brs[1]);
367         para_list_add(&br->node, &btrn->input_queue);
368         return 2;
369 }
370
371 void btr_merge(struct btr_node *btrn, size_t dest_size)
372 {
373         for (;;) {
374                 char *buf;
375                 size_t len = btr_next_buffer(btrn, &buf);
376                 if (len >= dest_size)
377                         return;
378                 if (merge_input(btrn) < 2)
379                         return;
380         }
381 }
382
383 bool btr_eof(struct btr_node *btrn)
384 {
385         char *buf;
386         size_t len = btr_next_buffer(btrn, &buf);
387
388         return (len == 0 && btr_no_parent(btrn));
389 }
390
391 void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
392 {
393         struct btr_node *ch;
394         const char spaces[] = "                 ", *space = spaces + 16 - depth;
395
396         if (depth > 16)
397                 return;
398         para_log(loglevel, "%s%s\n", space, btrn->name);
399         FOR_EACH_CHILD(ch, btrn)
400                 log_tree_recursively(ch, loglevel, depth + 1);
401 }
402
403 void btr_log_tree(struct btr_node *btrn, int loglevel)
404 {
405         return log_tree_recursively(btrn, loglevel, 0);
406 }