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);
169 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
171 struct btr_buffer_reference *br;
173 if (list_empty(&btrn->input_queue)) {
177 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
178 return btr_get_buffer_by_reference(br, bufp);
181 void btr_consume(struct btr_node *btrn, size_t numbytes)
183 struct btr_buffer_reference *br;
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);
193 static void flush_input_queue(struct btr_node *btrn)
195 struct btr_buffer_reference *br, *tmp;
196 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
197 btr_drop_buffer_reference(br);
200 void btr_del_node(struct btr_node *btrn)
206 PARA_NOTICE_LOG("deleting %s\n", btrn->name);
207 FOR_EACH_CHILD(ch, btrn)
209 flush_input_queue(btrn);
211 list_del(&btrn->node);
216 size_t btr_get_input_queue_size(struct btr_node *btrn)
218 struct btr_buffer_reference *br;
221 FOR_EACH_BUFFER_REF(br, btrn) {
222 //PARA_CRIT_LOG("size: %zu\n", size);
223 size += br_available_bytes(br);
228 void btr_splice_out_node(struct btr_node *btrn)
230 struct btr_node *ch, *tmp;
233 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
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;
242 list_move(&ch->node, &btrn->parent->children);
244 assert(list_empty(&btrn->children));
251 * Return the size of the largest input queue.
253 * Iterates over all children of the given node.
255 size_t btr_bytes_pending(struct btr_node *btrn)
260 FOR_EACH_CHILD(ch, btrn) {
261 size_t size = btr_get_input_queue_size(ch);
262 max_size = PARA_MAX(max_size, size);
267 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
270 return -ERRNO_TO_PARA_ERROR(EINVAL);
272 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
273 return btrn->execute(btrn, command, value_result);
276 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
280 for (; btrn; btrn = btrn->parent) {
281 struct btr_node *parent = btrn->parent;
283 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
284 if (!parent->execute)
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))
292 if (value_result && *value_result)
293 PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
297 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
300 void *btr_context(struct btr_node *btrn)
302 return btrn->context;
306 * Merge the first two input buffers into one.
308 * This is a quite expensive operation.
310 * \return The number of buffers that have been merged (zero, one or two).
312 int btr_merge(struct btr_node *btrn)
314 struct btr_buffer_reference *brs[2], *br;
319 if (list_empty(&btrn->input_queue))
321 if (list_is_singular(&btrn->input_queue))
324 /* get references to the first two buffers */
325 FOR_EACH_BUFFER_REF(br, btrn) {
327 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
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]);
341 br = para_malloc(sizeof(*br));
342 br->btrb = new_btrb(buf, sz);
343 br->btrb->refcount = 1;
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);