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)
45 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
46 list_for_each_entry((_br), &(_btrn)->input_queue, node)
47 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
48 list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
50 struct btr_node *btr_new_node(char *name, struct btr_node *parent,
51 btr_command_handler handler, void *context)
53 struct btr_node *btrn = para_malloc(sizeof(*btrn));
55 btrn->name = para_strdup(name);
56 btrn->parent = parent;
57 btrn->execute = handler;
58 btrn->context = context;
60 list_add_tail(&btrn->node, &parent->children);
61 INIT_LIST_HEAD(&btrn->children);
62 INIT_LIST_HEAD(&btrn->input_queue);
67 * Allocate a new btr buffer.
69 * The freshly allocated buffer will have a zero refcount.
71 static struct btr_buffer *new_btrb(char *buf, size_t size)
73 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
82 * Deallocate the reference, release the resources if refcount drops to zero.
84 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
86 struct btr_buffer *btrb = br->btrb;
88 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
92 if (btrb->refcount == 0) {
98 static void add_btrb_to_children(struct btr_buffer *btrb,
99 struct btr_node *btrn, size_t consumed)
103 FOR_EACH_CHILD(ch, btrn) {
104 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
106 br->consumed = consumed;
107 list_add_tail(&br->node, &ch->input_queue);
112 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
114 struct btr_buffer *btrb;
116 btrb = new_btrb(buf, size);
117 add_btrb_to_children(btrb, btrn, 0);
120 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
122 add_btrb_to_children(br->btrb, btrn, br->consumed);
123 btr_drop_buffer_reference(br);
126 void btr_pushdown(struct btr_node *btrn)
128 struct btr_buffer_reference *br, *tmp;
130 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
131 btr_pushdown_br(br, btrn);
134 /* Return true if this node has no children. */
135 bool btr_no_children(struct btr_node *btrn)
137 return list_empty(&btrn->children);
140 bool btr_no_parent(struct btr_node *btrn)
142 return !btrn->parent;
145 bool btr_inplace_ok(struct btr_node *btrn)
149 return list_is_singular(&btrn->parent->children);
152 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
154 return br->btrb->size - br->consumed;
157 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
159 *buf = br->btrb->buf + br->consumed;
160 return br_available_bytes(br);
163 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
165 struct btr_buffer_reference *br;
167 if (list_empty(&btrn->input_queue)) {
171 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
172 return btr_get_buffer_by_reference(br, bufp);
175 void btr_consume(struct btr_node *btrn, size_t numbytes)
177 struct btr_buffer_reference *br;
179 assert(!list_empty(&btrn->input_queue));
180 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
181 assert(br->consumed + numbytes <= br->btrb->size);
182 br->consumed += numbytes;
183 if (br->consumed == br->btrb->size)
184 btr_drop_buffer_reference(br);
187 static void flush_input_queue(struct btr_node *btrn)
189 struct btr_buffer_reference *br, *tmp;
190 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
191 btr_drop_buffer_reference(br);
194 void btr_del_node(struct btr_node *btrn)
200 PARA_NOTICE_LOG("deleting %s\n", btrn->name);
201 FOR_EACH_CHILD(ch, btrn)
203 flush_input_queue(btrn);
205 list_del(&btrn->node);
210 size_t btr_get_input_queue_size(struct btr_node *btrn)
212 struct btr_buffer_reference *br;
215 FOR_EACH_BUFFER_REF(br, btrn) {
216 //PARA_CRIT_LOG("size: %zu\n", size);
217 size += br_available_bytes(br);
222 int btr_splice_out_node(struct btr_node *btrn)
227 return -ERRNO_TO_PARA_ERROR(EINVAL);
228 if (btr_get_input_queue_size(btrn) != 0)
229 return -ERRNO_TO_PARA_ERROR(EINVAL);
230 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
232 list_del(&btrn->node);
233 FOR_EACH_CHILD(ch, btrn)
234 ch->parent = btrn->parent;
241 * Return the size of the largest input queue.
243 * Iterates over all children of the given node.
245 size_t btr_bytes_pending(struct btr_node *btrn)
250 FOR_EACH_CHILD(ch, btrn) {
251 size_t size = btr_get_input_queue_size(ch);
252 max_size = PARA_MAX(max_size, size);
257 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
260 return -ERRNO_TO_PARA_ERROR(EINVAL);
262 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
263 return btrn->execute(btrn, command, value_result);
266 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
270 for (; btrn; btrn = btrn->parent) {
271 struct btr_node *parent = btrn->parent;
272 PARA_CRIT_LOG("parent: %p\n", parent);
274 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
275 if (!parent->execute)
277 ret = parent->execute(parent, command, value_result);
278 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
283 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
286 void *btr_context(struct btr_node *btrn)
288 return btrn->context;