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