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;
41 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
42 &((_btrn)->children), node)
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)
49 struct btr_node *btr_new_node(char *name, struct btr_node *parent,
50 btr_command_handler handler)
52 struct btr_node *btrn = para_malloc(sizeof(*btrn));
54 btrn->name = para_strdup(name);
55 btrn->parent = parent;
56 btrn->execute = handler;
58 list_add_tail(&btrn->node, &parent->children);
59 INIT_LIST_HEAD(&btrn->children);
60 INIT_LIST_HEAD(&btrn->input_queue);
65 * Allocate a new btr buffer.
67 * The freshly allocated buffer will have a zero refcount.
69 static struct btr_buffer *new_btrb(char *buf, size_t size)
71 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
80 * Deallocate the reference, release the resources if refcount drops to zero.
82 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
84 struct btr_buffer *btrb = br->btrb;
86 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
90 if (btrb->refcount == 0) {
96 static void add_btrb_to_children(struct btr_buffer *btrb, struct btr_node *btrn)
100 FOR_EACH_CHILD(ch, btrn) {
101 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
104 list_add_tail(&br->node, &ch->input_queue);
109 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
111 struct btr_buffer *btrb;
113 btrb = new_btrb(buf, size);
114 add_btrb_to_children(btrb, btrn);
117 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
119 add_btrb_to_children(br->btrb, btrn);
120 btr_drop_buffer_reference(br);
123 void btr_pushdown(struct btr_node *btrn)
125 struct btr_buffer_reference *br, *tmp;
127 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
128 btr_pushdown_br(br, btrn);
131 /* Return true if this node has no children. */
132 bool btr_no_children(struct btr_node *btrn)
134 return list_empty(&btrn->children);
137 bool btr_no_parent(struct btr_node *btrn)
139 return !btrn->parent;
142 bool btr_inplace_ok(struct btr_node *btrn)
146 return list_is_singular(&btrn->parent->children);
149 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
151 return br->btrb->size - br->consumed;
154 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
156 *buf = br->btrb->buf + br->consumed;
157 return br_available_bytes(br);
160 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
162 struct btr_buffer_reference *br;
164 if (list_empty(&btrn->input_queue)) {
168 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
169 return btr_get_buffer_by_reference(br, bufp);
172 void btr_consume(struct btr_node *btrn, size_t numbytes)
174 struct btr_buffer_reference *br;
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);
184 static void flush_input_queue(struct btr_node *btrn)
186 struct btr_buffer_reference *br, *tmp;
187 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
188 btr_drop_buffer_reference(br);
191 void btr_del_node(struct btr_node *btrn)
197 PARA_NOTICE_LOG("deleting %s\n", btrn->name);
198 FOR_EACH_CHILD(ch, btrn)
200 flush_input_queue(btrn);
202 list_del(&btrn->node);
207 size_t btr_get_input_queue_size(struct btr_node *btrn)
209 struct btr_buffer_reference *br;
212 FOR_EACH_BUFFER_REF(br, btrn) {
213 //PARA_CRIT_LOG("size: %zu\n", size);
214 size += br_available_bytes(br);
219 int btr_splice_out_node(struct btr_node *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);
229 list_del(&btrn->node);
230 FOR_EACH_CHILD(ch, btrn)
231 ch->parent = btrn->parent;
238 * Return the size of the largest input queue.
240 * Iterates over all children of the given node.
242 size_t btr_bytes_pending(struct btr_node *btrn)
247 FOR_EACH_CHILD(ch, btrn) {
248 size_t size = btr_get_input_queue_size(ch);
249 max_size = PARA_MAX(max_size, size);
254 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
257 return -ERRNO_TO_PARA_ERROR(EINVAL);
259 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
260 return btrn->execute(command, value_result);
263 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
267 for (; btrn; btrn = btrn->parent) {
268 struct btr_node *parent = btrn->parent;
269 PARA_CRIT_LOG("parent: %p\n", parent);
271 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
272 if (!parent->execute)
274 ret = parent->execute(command, value_result);
275 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
280 return -ERRNO_TO_PARA_ERROR(ENOTSUP);