]> git.tuebingen.mpg.de Git - paraslash.git/blob - buffer_tree.c
buffer_tree: Add code to splice out a node of the buffer tree.
[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 };
40
41 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
42         &((_btrn)->children), node)
43
44 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
45         list_for_each_entry((_br), &(_btrn)->input_queue, node)
46 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
47         list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
48
49 struct btr_node *btr_new_node(char *name, struct btr_node *parent,
50                 btr_command_handler handler)
51 {
52         struct btr_node *btrn = para_malloc(sizeof(*btrn));
53
54         btrn->name = para_strdup(name);
55         btrn->parent = parent;
56         btrn->execute = handler;
57         if (parent)
58                 list_add_tail(&btrn->node, &parent->children);
59         INIT_LIST_HEAD(&btrn->children);
60         INIT_LIST_HEAD(&btrn->input_queue);
61         return btrn;
62 }
63
64 /*
65  * Allocate a new btr buffer.
66  *
67  * The freshly allocated buffer will have a zero refcount.
68  */
69 static struct btr_buffer *new_btrb(char *buf, size_t size)
70 {
71         struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
72
73         btrb->buf = buf;
74         btrb->size = size;
75         btrb->refcount = 0;
76         return btrb;
77 }
78
79 /*
80  * Deallocate the reference, release the resources if refcount drops to zero.
81  */
82 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
83 {
84         struct btr_buffer *btrb = br->btrb;
85
86         //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
87         list_del(&br->node);
88         free(br);
89         btrb->refcount--;
90         if (btrb->refcount == 0) {
91                 free(btrb->buf);
92                 free(btrb);
93         }
94 }
95
96 static void add_btrb_to_children(struct btr_buffer *btrb, struct btr_node *btrn)
97 {
98         struct btr_node *ch;
99
100         FOR_EACH_CHILD(ch, btrn) {
101                 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
102                 br->btrb = btrb;
103                 br->consumed = 0;
104                 list_add_tail(&br->node, &ch->input_queue);
105                 btrb->refcount++;
106         }
107 }
108
109 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
110 {
111         struct btr_buffer *btrb;
112
113         btrb = new_btrb(buf, size);
114         add_btrb_to_children(btrb, btrn);
115 }
116
117 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
118 {
119         add_btrb_to_children(br->btrb, btrn);
120         btr_drop_buffer_reference(br);
121 }
122
123 void btr_pushdown(struct btr_node *btrn)
124 {
125         struct btr_buffer_reference *br, *tmp;
126
127         FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
128                 btr_pushdown_br(br, btrn);
129 }
130
131 /* Return true if this node has no children. */
132 bool btr_no_children(struct btr_node *btrn)
133 {
134         return list_empty(&btrn->children);
135 }
136
137 bool btr_no_parent(struct btr_node *btrn)
138 {
139         return !btrn->parent;
140 }
141
142 bool btr_inplace_ok(struct btr_node *btrn)
143 {
144         if (!btrn->parent)
145                 return true;
146         return list_is_singular(&btrn->parent->children);
147 }
148
149 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
150 {
151         return br->btrb->size - br->consumed;
152 }
153
154 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
155 {
156         *buf = br->btrb->buf + br->consumed;
157         return br_available_bytes(br);
158 }
159
160 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
161 {
162         struct btr_buffer_reference *br;
163
164         if (list_empty(&btrn->input_queue)) {
165                 *bufp = NULL;
166                 return 0;
167         }
168         br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
169         return btr_get_buffer_by_reference(br, bufp);
170 }
171
172 void btr_consume(struct btr_node *btrn, size_t numbytes)
173 {
174         struct btr_buffer_reference *br;
175
176         assert(!list_empty(&btrn->input_queue));
177         br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
178         assert(br->consumed + numbytes <= br->btrb->size);
179         br->consumed += numbytes;
180         if (br->consumed == br->btrb->size)
181                 btr_drop_buffer_reference(br);
182 }
183
184 static void flush_input_queue(struct btr_node *btrn)
185 {
186         struct btr_buffer_reference *br, *tmp;
187         FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
188                 btr_drop_buffer_reference(br);
189 }
190
191 void btr_del_node(struct btr_node *btrn)
192 {
193         struct btr_node *ch;
194
195         if (!btrn)
196                 return;
197         PARA_NOTICE_LOG("deleting %s\n", btrn->name);
198         FOR_EACH_CHILD(ch, btrn)
199                 ch->parent = NULL;
200         flush_input_queue(btrn);
201         if (btrn->parent)
202                 list_del(&btrn->node);
203         free(btrn->name);
204         free(btrn);
205 }
206
207 size_t btr_get_input_queue_size(struct btr_node *btrn)
208 {
209         struct btr_buffer_reference *br;
210         size_t size = 0;
211
212         FOR_EACH_BUFFER_REF(br, btrn) {
213                 //PARA_CRIT_LOG("size: %zu\n", size);
214                 size += br_available_bytes(br);
215         }
216         return size;
217 }
218
219 int btr_splice_out_node(struct btr_node *btrn)
220 {
221         struct btr_node *ch;
222
223         if (!btrn)
224                 return -ERRNO_TO_PARA_ERROR(EINVAL);
225         if (btr_get_input_queue_size(btrn) != 0)
226                 return -ERRNO_TO_PARA_ERROR(EINVAL);
227         PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
228         if (btrn->parent)
229                 list_del(&btrn->node);
230         FOR_EACH_CHILD(ch, btrn)
231                 ch->parent = btrn->parent;
232         free(btrn->name);
233         free(btrn);
234         return 1;
235 }
236
237 /**
238  * Return the size of the largest input queue.
239  *
240  * Iterates over all children of the given node.
241  */
242 size_t btr_bytes_pending(struct btr_node *btrn)
243 {
244         size_t max_size = 0;
245         struct btr_node *ch;
246
247         FOR_EACH_CHILD(ch, btrn) {
248                 size_t size = btr_get_input_queue_size(ch);
249                 max_size = PARA_MAX(max_size, size);
250         }
251         return max_size;
252 }
253
254 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
255 {
256         if (!btrn)
257                 return -ERRNO_TO_PARA_ERROR(EINVAL);
258         if (!btrn->execute)
259                 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
260         return btrn->execute(command, value_result);
261 }
262
263 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
264 {
265         int ret;
266
267         for (; btrn; btrn = btrn->parent) {
268                 struct btr_node *parent = btrn->parent;
269                 PARA_CRIT_LOG("parent: %p\n", parent);
270                 if (!parent)
271                         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
272                 if (!parent->execute)
273                         continue;
274                 ret = parent->execute(command, value_result);
275                 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
276                         continue;
277                 if (ret < 0)
278                         return ret;
279         }
280         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
281 }