write.c: Fix cut'n'paste typo.
[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 /* Return true if this node has no children. */
141 bool btr_no_children(struct btr_node *btrn)
142 {
143         return list_empty(&btrn->children);
144 }
145
146 bool btr_no_parent(struct btr_node *btrn)
147 {
148         return !btrn->parent;
149 }
150
151 bool btr_inplace_ok(struct btr_node *btrn)
152 {
153         if (!btrn->parent)
154                 return true;
155         return list_is_singular(&btrn->parent->children);
156 }
157
158 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
159 {
160         return br->btrb->size - br->consumed;
161 }
162
163 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
164 {
165         *buf = br->btrb->buf + br->consumed;
166         return br_available_bytes(br);
167 }
168
169 /**
170  * \return zero if the input buffer queue is empty.
171  */
172 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
173 {
174         struct btr_buffer_reference *br;
175
176         if (list_empty(&btrn->input_queue)) {
177                 *bufp = NULL;
178                 return 0;
179         }
180         br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
181         return btr_get_buffer_by_reference(br, bufp);
182 }
183
184 void btr_consume(struct btr_node *btrn, size_t numbytes)
185 {
186         struct btr_buffer_reference *br;
187
188         assert(!list_empty(&btrn->input_queue));
189         br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
190         assert(br->consumed + numbytes <= br->btrb->size);
191         br->consumed += numbytes;
192         if (br->consumed == br->btrb->size)
193                 btr_drop_buffer_reference(br);
194 }
195
196 static void flush_input_queue(struct btr_node *btrn)
197 {
198         struct btr_buffer_reference *br, *tmp;
199         FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
200                 btr_drop_buffer_reference(br);
201 }
202
203 void btr_del_node(struct btr_node *btrn)
204 {
205         struct btr_node *ch;
206
207         if (!btrn)
208                 return;
209         PARA_NOTICE_LOG("deleting %s\n", btrn->name);
210         FOR_EACH_CHILD(ch, btrn)
211                 ch->parent = NULL;
212         flush_input_queue(btrn);
213         if (btrn->parent)
214                 list_del(&btrn->node);
215         free(btrn->name);
216         free(btrn);
217 }
218
219 size_t btr_get_input_queue_size(struct btr_node *btrn)
220 {
221         struct btr_buffer_reference *br;
222         size_t size = 0;
223
224         FOR_EACH_BUFFER_REF(br, btrn) {
225                 //PARA_CRIT_LOG("size: %zu\n", size);
226                 size += br_available_bytes(br);
227         }
228         return size;
229 }
230
231 void btr_splice_out_node(struct btr_node *btrn)
232 {
233         struct btr_node *ch, *tmp;
234
235         assert(btrn);
236         PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
237         btr_pushdown(btrn);
238         if (btrn->parent)
239                 list_del(&btrn->node);
240         FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
241                 PARA_INFO_LOG("parent(%s): %s\n", ch->name,
242                         btrn->parent? btrn->parent->name : "NULL");
243                 ch->parent = btrn->parent;
244                 if (btrn->parent)
245                         list_move(&ch->node, &btrn->parent->children);
246         }
247         assert(list_empty(&btrn->children));
248         free(btrn->name);
249         free(btrn);
250 }
251
252 /**
253  * Return the size of the largest input queue.
254  *
255  * Iterates over all children of the given node.
256  */
257 size_t btr_bytes_pending(struct btr_node *btrn)
258 {
259         size_t max_size = 0;
260         struct btr_node *ch;
261
262         FOR_EACH_CHILD(ch, btrn) {
263                 size_t size = btr_get_input_queue_size(ch);
264                 max_size = PARA_MAX(max_size, size);
265         }
266         return max_size;
267 }
268
269 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
270 {
271         if (!btrn)
272                 return -ERRNO_TO_PARA_ERROR(EINVAL);
273         if (!btrn->execute)
274                 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
275         return btrn->execute(btrn, command, value_result);
276 }
277
278 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
279 {
280         int ret;
281
282         for (; btrn; btrn = btrn->parent) {
283                 struct btr_node *parent = btrn->parent;
284                 if (!parent)
285                         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
286                 if (!parent->execute)
287                         continue;
288                 PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
289                 ret = parent->execute(parent, command, value_result);
290                 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
291                         continue;
292                 if (ret < 0)
293                         return ret;
294                 if (value_result && *value_result)
295                         PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
296                                 *value_result);
297                 return 1;
298         }
299         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
300 }
301
302 void *btr_context(struct btr_node *btrn)
303 {
304         return btrn->context;
305 }
306
307 /**
308  * Merge the first two input buffers into one.
309  *
310  * This is a quite expensive operation.
311  *
312  * \return The number of buffers that have been merged (zero, one or two).
313  */
314 int btr_merge(struct btr_node *btrn)
315 {
316         struct btr_buffer_reference *brs[2], *br;
317         char *bufs[2], *buf;
318         size_t szs[2], sz;
319         int i;
320
321         if (list_empty(&btrn->input_queue))
322                 return 0;
323         if (list_is_singular(&btrn->input_queue))
324                 return 1;
325         i = 0;
326         /* get references to the first two buffers */
327         FOR_EACH_BUFFER_REF(br, btrn) {
328                 brs[i] = br;
329                 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
330                 i++;
331                 if (i == 2)
332                         break;
333         }
334         /* make a new btrb that combines the two buffers and a br to it. */
335         sz = szs[0] + szs[1];
336         //PARA_CRIT_LOG("merging input buffers: (%zu, %zu) -> %zu\n",
337         //      szs[0], szs[1], sz);
338         buf = para_malloc(sz);
339         /* TODO: Avoid this memcopy by introducing btr buffer pool. */
340         memcpy(buf, bufs[0], szs[0]);
341         memcpy(buf + szs[0], bufs[1], szs[1]);
342
343         br = para_malloc(sizeof(*br));
344         br->btrb = new_btrb(buf, sz);
345         br->btrb->refcount = 1;
346         br->consumed = 0;
347
348         /* replace the first two refs by the new one */
349         btr_drop_buffer_reference(brs[0]);
350         btr_drop_buffer_reference(brs[1]);
351         para_list_add(&br->node, &btrn->input_queue);
352         return 2;
353 }