7 #include "buffer_tree.h"
14 /** The number of references to this buffer. */
18 struct btr_buffer_reference {
19 struct btr_buffer *btrb;
21 /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
22 struct list_head node;
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;
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.
37 struct list_head input_queue;
38 btr_command_handler execute;
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)
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)
52 struct btr_node *btr_new_node(const char *name, struct btr_node *parent,
53 btr_command_handler handler, void *context)
55 struct btr_node *btrn = para_malloc(sizeof(*btrn));
57 btrn->name = para_strdup(name);
58 btrn->parent = parent;
59 btrn->execute = handler;
60 btrn->context = context;
62 list_add_tail(&btrn->node, &parent->children);
63 INIT_LIST_HEAD(&btrn->children);
64 INIT_LIST_HEAD(&btrn->input_queue);
66 PARA_INFO_LOG("added %s as child of %s\n", name, parent->name);
68 PARA_INFO_LOG("added %s as btr root\n", name);
73 * Allocate a new btr buffer.
75 * The freshly allocated buffer will have a zero refcount.
77 static struct btr_buffer *new_btrb(char *buf, size_t size)
79 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
88 * Deallocate the reference, release the resources if refcount drops to zero.
90 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
92 struct btr_buffer *btrb = br->btrb;
94 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
98 if (btrb->refcount == 0) {
104 static void add_btrb_to_children(struct btr_buffer *btrb,
105 struct btr_node *btrn, size_t consumed)
109 FOR_EACH_CHILD(ch, btrn) {
110 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
112 br->consumed = consumed;
113 list_add_tail(&br->node, &ch->input_queue);
118 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
120 struct btr_buffer *btrb;
122 btrb = new_btrb(buf, size);
123 add_btrb_to_children(btrb, btrn, 0);
126 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
128 add_btrb_to_children(br->btrb, btrn, br->consumed);
129 btr_drop_buffer_reference(br);
132 void btr_pushdown(struct btr_node *btrn)
134 struct btr_buffer_reference *br, *tmp;
136 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
137 btr_pushdown_br(br, btrn);
140 /* Return true if this node has no children. */
141 bool btr_no_children(struct btr_node *btrn)
143 return list_empty(&btrn->children);
146 bool btr_no_parent(struct btr_node *btrn)
148 return !btrn->parent;
151 bool btr_inplace_ok(struct btr_node *btrn)
155 return list_is_singular(&btrn->parent->children);
158 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
160 return br->btrb->size - br->consumed;
163 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
165 *buf = br->btrb->buf + br->consumed;
166 return br_available_bytes(br);
170 * \return zero if the input buffer queue is empty.
172 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
174 struct btr_buffer_reference *br;
176 if (list_empty(&btrn->input_queue)) {
180 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
181 return btr_get_buffer_by_reference(br, bufp);
184 void btr_consume(struct btr_node *btrn, size_t numbytes)
186 struct btr_buffer_reference *br;
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);
196 static void flush_input_queue(struct btr_node *btrn)
198 struct btr_buffer_reference *br, *tmp;
199 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
200 btr_drop_buffer_reference(br);
203 void btr_del_node(struct btr_node *btrn)
209 PARA_NOTICE_LOG("deleting %s\n", btrn->name);
210 FOR_EACH_CHILD(ch, btrn)
212 flush_input_queue(btrn);
214 list_del(&btrn->node);
219 size_t btr_get_input_queue_size(struct btr_node *btrn)
221 struct btr_buffer_reference *br;
224 FOR_EACH_BUFFER_REF(br, btrn) {
225 //PARA_CRIT_LOG("size: %zu\n", size);
226 size += br_available_bytes(br);
231 void btr_splice_out_node(struct btr_node *btrn)
233 struct btr_node *ch, *tmp;
236 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
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;
245 list_move(&ch->node, &btrn->parent->children);
247 assert(list_empty(&btrn->children));
253 * Return the size of the largest input queue.
255 * Iterates over all children of the given node.
257 size_t btr_bytes_pending(struct btr_node *btrn)
262 FOR_EACH_CHILD(ch, btrn) {
263 size_t size = btr_get_input_queue_size(ch);
264 max_size = PARA_MAX(max_size, size);
269 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
272 return -ERRNO_TO_PARA_ERROR(EINVAL);
274 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
275 return btrn->execute(btrn, command, value_result);
278 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
282 for (; btrn; btrn = btrn->parent) {
283 struct btr_node *parent = btrn->parent;
285 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
286 if (!parent->execute)
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))
294 if (value_result && *value_result)
295 PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
299 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
302 void *btr_context(struct btr_node *btrn)
304 return btrn->context;
308 * Merge the first two input buffers into one.
310 * This is a quite expensive operation.
312 * \return The number of buffers that have been merged (zero, one or two).
314 int btr_merge(struct btr_node *btrn)
316 struct btr_buffer_reference *brs[2], *br;
321 if (list_empty(&btrn->input_queue))
323 if (list_is_singular(&btrn->input_queue))
326 /* get references to the first two buffers */
327 FOR_EACH_BUFFER_REF(br, btrn) {
329 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
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]);
343 br = para_malloc(sizeof(*br));
344 br->btrb = new_btrb(buf, sz);
345 br->btrb->refcount = 1;
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);