7 #include "buffer_tree.h"
11 /* whead = NULL means area full */
23 /** The number of references to this buffer. */
25 /* NULL means no buffer pool but a malloced buffer. */
26 struct btr_pool *pool;
29 struct btr_buffer_reference {
30 struct btr_buffer *btrb;
32 /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
33 struct list_head node;
39 struct btr_node *parent;
40 /* The position of this btr node in the buffer tree. */
41 struct list_head node;
42 /* The children nodes of this btr node are linked together in a list. */
43 struct list_head children;
44 /* Time of first data transfer. */
47 * The input queue is a list of references to btr buffers. Each item on
48 * the list represents an input buffer which has not been completely
49 * used by this btr node.
51 struct list_head input_queue;
52 btr_command_handler execute;
57 * Create a new buffer pool.
59 * \param name The name of the new buffer pool.
60 * \param area_size The size in bytes of the pool area.
62 * \return An opaque pointer to the newly created buffer pool. It must be
63 * passed to btr_pool_free() after it is no longer used to deallocate all
66 struct btr_pool *btr_pool_new(const char *name, size_t area_size)
68 struct btr_pool *btrp;
70 PARA_INFO_LOG("%s, %zu bytes\n", name, area_size);
71 btrp = para_malloc(sizeof(*btrp));
72 btrp->area_start = para_malloc(area_size);
73 btrp->area_end = btrp->area_start + area_size;
74 btrp->rhead = btrp->area_start;
75 btrp->whead = btrp->area_start;
76 btrp->name = para_strdup(name);
81 * Deallocate resources used by a buffer pool.
83 * \param btrp A pointer obtained via btr_pool_new().
85 void btr_pool_free(struct btr_pool *btrp)
89 free(btrp->area_start);
95 * Return the size of the buffer pool area.
97 * \param btrp The buffer pool.
99 * \return The same value which was passed during creation time to
102 size_t btr_pool_size(struct btr_pool *btrp)
104 return btrp->area_end - btrp->area_start;
107 size_t btr_pool_filled(struct btr_pool *btrp)
110 return btr_pool_size(btrp);
111 if (btrp->rhead <= btrp->whead)
112 return btrp->whead - btrp->rhead;
113 return btr_pool_size(btrp) - (btrp->rhead - btrp->whead);
117 * Get the number of unused bytes in the buffer pool.
119 * \param btrp The pool.
121 * \return The number of bytes that can currently be allocated.
123 * Note that in general the returned number of bytes is not available as a
124 * single contiguous buffer. Use btr_pool_available() to obtain the length of
125 * the largest contiguous buffer that can currently be allocated from the
128 size_t btr_pool_unused(struct btr_pool *btrp)
130 return btr_pool_size(btrp) - btr_pool_filled(btrp);
134 * Return maximal size available for one read. This is
135 * smaller than the value returned by btr_pool_unused().
137 size_t btr_pool_available(struct btr_pool *btrp)
141 if (btrp->rhead <= btrp->whead)
142 return btrp->area_end - btrp->whead;
143 return btrp->rhead - btrp->whead;
147 * Obtain the current write head.
149 * \param btrp The buffer pool.
150 * \param result The write head is returned here.
152 * \return The maximal amount of bytes that may be written to the returned
155 size_t btr_pool_get_buffer(struct btr_pool *btrp, char **result)
158 *result = btrp->whead;
159 return btr_pool_available(btrp);
163 * Mark a part of the buffer pool area as allocated.
165 * \param btrp The buffer pool.
166 * \param size The amount of bytes to be allocated.
168 * This is usually called after the caller wrote to the buffer obtained by
169 * btr_pool_get_buffer().
171 static void btr_pool_allocate(struct btr_pool *btrp, size_t size)
177 assert(size <= btr_pool_available(btrp));
178 end = btrp->whead + size;
179 assert(end <= btrp->area_end);
181 if (end == btrp->area_end) {
182 PARA_DEBUG_LOG("%s: end of pool area reached\n", btrp->name);
183 end = btrp->area_start;
185 if (end == btrp->rhead) {
186 PARA_DEBUG_LOG("%s btrp buffer full\n", btrp->name);
187 end = NULL; /* buffer full */
192 static void btr_pool_deallocate(struct btr_pool *btrp, size_t size)
194 char *end = btrp->rhead + size;
198 assert(end <= btrp->area_end);
199 assert(size <= btr_pool_filled(btrp));
200 if (end == btrp->area_end)
201 end = btrp->area_start;
203 btrp->whead = btrp->rhead;
205 if (btrp->rhead == btrp->whead)
206 btrp->rhead = btrp->whead = btrp->area_start;
209 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
210 &((_btrn)->children), node)
211 #define FOR_EACH_CHILD_SAFE(_tn, _tmp, _btrn) \
212 list_for_each_entry_safe((_tn), (_tmp), &((_btrn)->children), node)
214 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
215 list_for_each_entry((_br), &(_btrn)->input_queue, node)
216 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
217 list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
220 * Create a new buffer tree node.
222 * \param bnd Specifies how to create the new node.
224 * This function always succeeds (or calls exit()). The returned pointer must
225 * be freed using btr_free_node() after the node has been removed from the
226 * buffer tree via btr_remove_node().
228 struct btr_node *btr_new_node(struct btr_node_description *bnd)
230 struct btr_node *btrn = para_malloc(sizeof(*btrn));
232 btrn->name = para_strdup(bnd->name);
233 btrn->parent = bnd->parent;
234 btrn->execute = bnd->handler;
235 btrn->context = bnd->context;
236 btrn->start.tv_sec = 0;
237 btrn->start.tv_usec = 0;
238 INIT_LIST_HEAD(&btrn->children);
239 INIT_LIST_HEAD(&btrn->input_queue);
242 list_add_tail(&btrn->node, &bnd->parent->children);
243 PARA_INFO_LOG("new leaf node: %s (child of %s)\n",
244 bnd->name, bnd->parent->name);
246 PARA_INFO_LOG("added %s as btr root\n", bnd->name);
250 assert(!bnd->child->parent);
251 PARA_INFO_LOG("new root: %s (was %s)\n",
252 bnd->name, bnd->child->name);
254 list_add_tail(&bnd->child->node, &btrn->children);
256 bnd->child->parent = btrn;
259 PARA_EMERG_LOG("inserting internal nodes not yet supported.\n");
261 assert(bnd->child->parent == bnd->parent);
267 * Allocate a new btr buffer.
269 * The freshly allocated buffer will have a zero refcount and will
270 * not be associated with a btr pool.
272 static struct btr_buffer *new_btrb(char *buf, size_t size)
274 struct btr_buffer *btrb = para_calloc(sizeof(*btrb));
281 static void dealloc_buffer(struct btr_buffer *btrb)
284 btr_pool_deallocate(btrb->pool, btrb->size);
289 static struct btr_buffer_reference *get_first_input_br(struct btr_node *btrn)
291 if (list_empty(&btrn->input_queue))
293 return list_first_entry(&btrn->input_queue,
294 struct btr_buffer_reference, node);
298 * Deallocate the reference, release the resources if refcount drops to zero.
300 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
302 struct btr_buffer *btrb = br->btrb;
307 if (btrb->refcount == 0) {
308 dealloc_buffer(btrb);
313 static void add_btrb_to_children(struct btr_buffer *btrb,
314 struct btr_node *btrn, size_t consumed)
318 if (btrn->start.tv_sec == 0)
320 FOR_EACH_CHILD(ch, btrn) {
321 struct btr_buffer_reference *br = para_calloc(sizeof(*br));
323 br->consumed = consumed;
324 list_add_tail(&br->node, &ch->input_queue);
326 if (ch->start.tv_sec == 0)
332 * Insert a malloced buffer into the buffer tree.
334 * \param buf The buffer to insert.
335 * \param size The size of \a buf in bytes.
336 * \param btrn Position in the buffer tree to create the output.
338 * This creates references to \a buf and adds these references to each child of
339 * \a btrn. The buffer will be freed using standard free() once no buffer tree
340 * node is referencing it any more.
342 * Note that this function must not be used if \a buf was obtained from a
343 * buffer pool. Use btr_add_output_pool() in this case.
345 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
347 struct btr_buffer *btrb;
350 if (list_empty(&btrn->children)) {
354 btrb = new_btrb(buf, size);
355 add_btrb_to_children(btrb, btrn, 0);
359 * Feed data to child nodes of a buffer tree node.
361 * \param btrp The buffer pool.
362 * \param size The number of bytes to be allocated and fed to each child.
363 * \param btrn The node whose children are to be fed.
365 * This function allocates the amount of bytes from the buffer pool area,
366 * starting at the current value of the write head, and creates buffer
367 * references to the resulting part of the buffer pool area, one for each child
368 * of \a btrn. The references are then fed into the input queue of each child.
370 void btr_add_output_pool(struct btr_pool *btrp, size_t size,
371 struct btr_node *btrn)
373 struct btr_buffer *btrb;
378 if (list_empty(&btrn->children))
380 avail = btr_pool_get_buffer(btrp, &buf);
381 assert(avail >= size);
382 btr_pool_allocate(btrp, size);
383 btrb = new_btrb(buf, size);
385 add_btrb_to_children(btrb, btrn, 0);
389 * Copy data to write head of a buffer pool and feed it to all children nodes.
391 * \param src The source buffer.
392 * \param n The size of the source buffer in bytes.
393 * \param btrp The destination buffer pool.
394 * \param btrn Add the data as output of this node.
396 * This is expensive. The caller must make sure the data fits into the buffer
399 void btr_copy(const void *src, size_t n, struct btr_pool *btrp,
400 struct btr_node *btrn)
407 assert(n <= btr_pool_unused(btrp));
408 sz = btr_pool_get_buffer(btrp, &buf);
409 copy = PARA_MIN(sz, n);
410 memcpy(buf, src, copy);
411 btr_add_output_pool(btrp, copy, btrn);
414 sz = btr_pool_get_buffer(btrp, &buf);
415 assert(sz >= n - copy);
416 memcpy(buf, src + copy, n - copy);
417 btr_add_output_pool(btrp, n - copy, btrn);
420 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
422 add_btrb_to_children(br->btrb, btrn, br->consumed);
423 btr_drop_buffer_reference(br);
427 * Feed all buffer references of the input queue through the output channel.
429 * \param btrn The node whose buffer references should be pushed down.
431 * This function is useful for filters that do not change the contents of the
432 * buffers at all, like the wav filter or the amp filter if no amplification
433 * was specified. This function is rather cheap.
435 void btr_pushdown(struct btr_node *btrn)
437 struct btr_buffer_reference *br, *tmp;
439 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
440 btr_pushdown_br(br, btrn);
443 int btr_pushdown_one(struct btr_node *btrn)
445 struct btr_buffer_reference *br;
447 if (list_empty(&btrn->input_queue))
449 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
450 btr_pushdown_br(br, btrn);
455 * Find out whether a node is a leaf node.
457 * \param btrn The node to check.
459 * \return True if this node has no children. False otherwise.
461 static bool btr_no_children(struct btr_node *btrn)
463 return list_empty(&btrn->children);
467 * Find out whether a node is an orphan node.
469 * \param btrn The buffer tree node.
471 * \return True if \a btrn has no parent.
473 * This function will always return true for the root node. However in case
474 * nodes have been removed from the tree, other nodes may become orphans too.
476 bool btr_no_parent(struct btr_node *btrn)
478 return !btrn->parent;
481 bool btr_inplace_ok(struct btr_node *btrn)
485 return list_is_singular(&btrn->parent->children);
488 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
490 return br->btrb->size - br->consumed;
493 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
496 *buf = br->btrb->buf + br->consumed;
497 return br_available_bytes(br);
501 * Obtain the next buffer of the input queue of a buffer tree node.
503 * \param btrn The node whose input queue is to be queried.
504 * \param bufp Result pointer.
506 * \return The number of bytes that can be read from buf. Zero if the input
507 * buffer queue is empty. In this case the value of \a bufp is undefined.
509 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
511 struct btr_buffer_reference *br;
512 char *buf, *result = NULL;
515 FOR_EACH_BUFFER_REF(br, btrn) {
516 sz = btr_get_buffer_by_reference(br, &buf);
526 if (result + rv != buf)
536 * Deallocate the given number of bytes from the input queue.
538 * \param btrn The buffer tree node.
539 * \param numbytes The number of bytes to be deallocated.
541 * This function must be used to get rid of existing buffer references in the
542 * node's input queue. If no references to a buffer remain, the underlying
543 * buffers are either freed (in the non-buffer tree case) or the read head of
544 * the buffer pool is being advanced.
546 * Note that \a numbytes may be smaller than the buffer size. In this case the
547 * buffer is not deallocated and subsequent calls to btr_next_buffer() return
548 * the remaining part of the buffer.
550 void btr_consume(struct btr_node *btrn, size_t numbytes)
552 struct btr_buffer_reference *br, *tmp;
557 br = get_first_input_br(btrn);
560 if (br->wrap_count == 0) {
562 * No wrap buffer. Drop buffer references whose buffer
563 * has been fully used. */
564 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn) {
565 if (br->consumed + numbytes <= br->btrb->size) {
566 br->consumed += numbytes;
567 if (br->consumed == br->btrb->size)
568 btr_drop_buffer_reference(br);
571 numbytes -= br->btrb->size - br->consumed;
572 btr_drop_buffer_reference(br);
577 * We have a wrap buffer, consume from it. If in total, i.e. including
578 * previous calls to brt_consume(), less than wrap_count has been
579 * consumed, there's nothing more we can do.
581 * Otherwise we drop the wrap buffer and consume from subsequent
582 * buffers of the input queue the correct amount of bytes. This is the
583 * total number of bytes that have been consumed from the wrap buffer.
585 PARA_DEBUG_LOG("consuming %zu/%zu bytes from wrap buffer\n", numbytes,
586 br_available_bytes(br));
588 assert(numbytes <= br_available_bytes(br));
589 if (br->consumed + numbytes < br->wrap_count) {
590 br->consumed += numbytes;
593 PARA_DEBUG_LOG("dropping wrap buffer (%zu bytes)\n", br->btrb->size);
594 /* get rid of the wrap buffer */
595 sz = br->consumed + numbytes;
596 btr_drop_buffer_reference(br);
597 return btr_consume(btrn, sz);
600 static void flush_input_queue(struct btr_node *btrn)
602 struct btr_buffer_reference *br, *tmp;
603 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
604 btr_drop_buffer_reference(br);
608 * Free all resources allocated by btr_new_node().
610 * Like free(3), it is OK to call this with a \p NULL pointer argument.
612 void btr_free_node(struct btr_node *btrn)
621 * Remove a node from a buffer tree.
623 * \param btrn The node to remove.
625 * This makes all child nodes of \a btrn orphans and removes \a btrn from the
626 * list of children of its parent. Moreover, the input queue of \a btrn is
627 * flushed if it is not empty.
629 * \sa \ref btr_splice_out_node.
631 void btr_remove_node(struct btr_node *btrn)
637 PARA_NOTICE_LOG("removing btr node %s from buffer tree\n", btrn->name);
638 FOR_EACH_CHILD(ch, btrn)
640 flush_input_queue(btrn);
642 list_del(&btrn->node);
646 * Return the amount of available input bytes of a buffer tree node.
648 * \param btrn The node whose input size should be computed.
650 * \return The total number of bytes available in the node's input
653 * This simply iterates over all buffer references in the input queue and
654 * returns the sum of the sizes of all references.
656 size_t btr_get_input_queue_size(struct btr_node *btrn)
658 struct btr_buffer_reference *br;
659 size_t size = 0, wrap_consumed = 0;
661 FOR_EACH_BUFFER_REF(br, btrn) {
662 if (br->wrap_count != 0) {
663 wrap_consumed = br->consumed;
666 size += br_available_bytes(br);
668 assert(wrap_consumed <= size);
669 size -= wrap_consumed;
674 * Remove a node from the buffer tree, reconnecting parent and children.
676 * \param btrn The node to splice out.
678 * This function is used by buffer tree nodes that do not exist during the
679 * whole lifetime of the buffer tree. Unlike btr_remove_node(), calling
680 * btr_splice_out_node() does not split the tree into disconnected components
681 * but reconnects the buffer tree by making all child nodes of \a btrn children
682 * of the parent of \a btrn.
684 void btr_splice_out_node(struct btr_node *btrn)
686 struct btr_node *ch, *tmp;
689 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
692 list_del(&btrn->node);
693 FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
694 PARA_INFO_LOG("parent(%s): %s\n", ch->name,
695 btrn->parent? btrn->parent->name : "NULL");
696 ch->parent = btrn->parent;
698 list_move(&ch->node, &btrn->parent->children);
700 assert(list_empty(&btrn->children));
704 * Return the size of the largest input queue.
706 * Iterates over all children of the given node.
708 static size_t btr_bytes_pending(struct btr_node *btrn)
713 FOR_EACH_CHILD(ch, btrn) {
714 size_t size = btr_get_input_queue_size(ch);
715 max_size = PARA_MAX(max_size, size);
720 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
723 return -ERRNO_TO_PARA_ERROR(EINVAL);
725 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
726 return btrn->execute(btrn, command, value_result);
730 * Execute a inter-node command on a parent node.
732 * \param btrn The node to start looking.
733 * \param command The command to execute.
734 * \param value_result Additional arguments and result value.
736 * This function traverses the buffer tree upwards and looks for parent nodes
737 * of \a btrn that understands \a command. On the first such node the command
738 * is executed, and the result is stored in \a value_result.
740 * \return \p -ENOTSUP if no parent node of \a btrn understands \a command.
741 * Otherwise the return value of the command handler is returned.
743 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
747 for (; btrn; btrn = btrn->parent) {
748 struct btr_node *parent = btrn->parent;
750 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
751 if (!parent->execute)
753 PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
754 ret = parent->execute(parent, command, value_result);
755 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
759 if (value_result && *value_result)
760 PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
764 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
768 * Obtain the context of a buffer node tree.
770 * The returned pointer equals the context pointer used at creation time of the
773 * \sa btr_new_node(), struct \ref btr_node_description.
775 void *btr_context(struct btr_node *btrn)
777 return btrn->context;
780 static bool need_buffer_pool_merge(struct btr_node *btrn)
782 struct btr_buffer_reference *br = get_first_input_br(btrn);
786 if (br->wrap_count != 0)
793 static void merge_input_pool(struct btr_node *btrn, size_t dest_size)
795 struct btr_buffer_reference *br, *wbr = NULL;
796 int num_refs; /* including wrap buffer */
797 char *buf, *buf1 = NULL, *buf2 = NULL;
798 size_t sz, sz1 = 0, sz2 = 0, wsz;
800 br = get_first_input_br(btrn);
801 if (!br || br_available_bytes(br) >= dest_size)
804 FOR_EACH_BUFFER_REF(br, btrn) {
806 sz = btr_get_buffer_by_reference(br, &buf);
809 if (br->wrap_count != 0) {
811 assert(num_refs == 1);
822 if (buf1 + sz1 == buf) {
831 assert(buf2 + sz2 == buf);
834 if (sz1 + sz2 >= dest_size)
837 if (!buf2) /* nothing to do */
839 assert(buf1 && sz2 > 0);
841 * If the second buffer is large, we only take the first part of it to
842 * avoid having to memcpy() huge buffers.
844 sz2 = PARA_MIN(sz2, (size_t)(64 * 1024));
846 /* Make a new wrap buffer combining buf1 and buf2. */
848 buf = para_malloc(sz);
849 PARA_DEBUG_LOG("merging input buffers: (%p:%zu, %p:%zu) -> %p:%zu\n",
850 buf1, sz1, buf2, sz2, buf, sz);
851 memcpy(buf, buf1, sz1);
852 memcpy(buf + sz1, buf2, sz2);
853 br = para_calloc(sizeof(*br));
854 br->btrb = new_btrb(buf, sz);
855 br->btrb->refcount = 1;
857 /* This is a wrap buffer */
858 br->wrap_count = sz1;
859 para_list_add(&br->node, &btrn->input_queue);
863 * We already have a wrap buffer, but it is too small. It might be
866 wsz = br_available_bytes(wbr);
867 if (wbr->wrap_count == sz1 && wbr->btrb->size >= sz1 + sz2) /* nothing we can do about it */
869 sz = sz1 + sz2 - wbr->btrb->size; /* amount of new data */
870 PARA_DEBUG_LOG("increasing wrap buffer %zu -> %zu\n", wbr->btrb->size,
871 wbr->btrb->size + sz);
872 wbr->btrb->size += sz;
873 wbr->btrb->buf = para_realloc(wbr->btrb->buf, wbr->btrb->size);
874 /* copy the new data to the end of the reallocated buffer */
876 memcpy(wbr->btrb->buf + wbr->btrb->size - sz, buf2 + sz2 - sz, sz);
880 * Merge the first two input buffers into one.
882 * This is a quite expensive operation.
884 * \return The number of buffers that have been available (zero, one or two).
886 static int merge_input(struct btr_node *btrn)
888 struct btr_buffer_reference *brs[2], *br;
893 if (list_empty(&btrn->input_queue))
895 if (list_is_singular(&btrn->input_queue))
898 /* get references to the first two buffers */
899 FOR_EACH_BUFFER_REF(br, btrn) {
901 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
907 /* make a new btrb that combines the two buffers and a br to it. */
908 sz = szs[0] + szs[1];
909 buf = para_malloc(sz);
910 PARA_DEBUG_LOG("%s: memory merging input buffers: (%zu, %zu) -> %zu\n",
911 btrn->name, szs[0], szs[1], sz);
912 memcpy(buf, bufs[0], szs[0]);
913 memcpy(buf + szs[0], bufs[1], szs[1]);
915 br = para_calloc(sizeof(*br));
916 br->btrb = new_btrb(buf, sz);
917 br->btrb->refcount = 1;
919 /* replace the first two refs by the new one */
920 btr_drop_buffer_reference(brs[0]);
921 btr_drop_buffer_reference(brs[1]);
922 para_list_add(&br->node, &btrn->input_queue);
927 * Combine input queue buffers.
929 * \param btrn The buffer tree node whose input should be merged.
930 * \param dest_size Stop merging if a buffer of at least this size exists.
932 * Used to combine as many buffers as needed into a single buffer whose size is
933 * at least \a dest_size. This function is rather cheap in case the parent node
934 * uses buffer pools and rather expensive otherwise.
936 * Note that if less than \a dest_size bytes are available in total, this
937 * function does nothing and subsequent calls to btr_next_buffer() will still
938 * return a buffer size less than \a dest_size.
940 void btr_merge(struct btr_node *btrn, size_t dest_size)
942 if (need_buffer_pool_merge(btrn))
943 return merge_input_pool(btrn, dest_size);
946 size_t len = btr_next_buffer(btrn, &buf);
947 if (len >= dest_size)
949 PARA_DEBUG_LOG("input size = %zu < %zu = dest\n", len, dest_size);
950 if (merge_input(btrn) < 2)
955 bool btr_eof(struct btr_node *btrn)
958 size_t len = btr_next_buffer(btrn, &buf);
960 return (len == 0 && btr_no_parent(btrn));
963 void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
966 const char spaces[] = " ", *space = spaces + 16 - depth;
970 para_log(loglevel, "%s%s\n", space, btrn->name);
971 FOR_EACH_CHILD(ch, btrn)
972 log_tree_recursively(ch, loglevel, depth + 1);
975 void btr_log_tree(struct btr_node *btrn, int loglevel)
977 return log_tree_recursively(btrn, loglevel, 0);
981 * \return \a root if \a name is \p NULL.
983 struct btr_node *btr_search_node(const char *name, struct btr_node *root)
989 if (!strcmp(root->name, name))
991 FOR_EACH_CHILD(ch, root) {
992 struct btr_node *result = btr_search_node(name, ch);
999 /** 640K ought to be enough for everybody ;) */
1000 #define BTRN_MAX_PENDING (640 * 1024)
1002 int btr_node_status(struct btr_node *btrn, size_t min_iqs,
1003 enum btr_node_type type)
1008 if (type != BTR_NT_LEAF) {
1009 if (btr_no_children(btrn))
1010 return -E_BTR_NO_CHILD;
1011 if (btr_bytes_pending(btrn) > BTRN_MAX_PENDING)
1014 if (type != BTR_NT_ROOT) {
1017 iqs = btr_get_input_queue_size(btrn);
1018 if (iqs == 0) /* we have a parent, because not eof */
1020 if (iqs < min_iqs && !btr_no_parent(btrn))
1026 void btr_get_node_start(struct btr_node *btrn, struct timeval *tv)