+
+/**
+ * Combine input queue buffers.
+ *
+ * \param btrn The buffer tree node whose input should be merged.
+ * \param dest_size Stop merging if a buffer of at least this size exists.
+ *
+ * Used to combine as many buffers as needed into a single buffer whose size is
+ * at least \a dest_size. This function is rather cheap in case the parent node
+ * uses buffer pools and rather expensive otherwise.
+ *
+ * Note that if less than \a dest_size bytes are available in total, this
+ * function does nothing and subsequent calls to btr_next_buffer() will still
+ * return a buffer size less than \a dest_size.
+ */
+void btr_merge(struct btr_node *btrn, size_t dest_size)
+{
+ if (need_buffer_pool_merge(btrn))
+ return merge_input_pool(btrn, dest_size);
+ for (;;) {
+ char *buf;
+ size_t len = btr_next_buffer(btrn, &buf);
+ if (len >= dest_size)
+ return;
+ PARA_DEBUG_LOG("input size = %zu < %zu = dest\n", len, dest_size);
+ if (merge_input(btrn) < 2)
+ return;
+ }
+}
+
+static bool btr_eof(struct btr_node *btrn)
+{
+ char *buf;
+ size_t len = btr_next_buffer(btrn, &buf);
+
+ return (len == 0 && btr_no_parent(btrn));
+}
+
+static void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
+{
+ struct btr_node *ch;
+ const char spaces[] = " ", *space = spaces + 16 - depth;
+
+ if (depth > 16)
+ return;
+ para_log(loglevel, "%s%s\n", space, btrn->name);
+ FOR_EACH_CHILD(ch, btrn)
+ log_tree_recursively(ch, loglevel, depth + 1);
+}
+
+/**
+ * Write the current buffer (sub-)tree to the log.
+ *
+ * \param btrn Start logging at this node.
+ * \param loglevel Set severity with which the tree should be logged.
+ */
+void btr_log_tree(struct btr_node *btrn, int loglevel)
+{
+ return log_tree_recursively(btrn, loglevel, 0);
+}
+
+/**
+ * Find the node with the given name in the buffer tree.
+ *
+ * \param name The name of the node to search.
+ * \param root Where to start the search.
+ *
+ * \return A pointer to the node with the given name on success. If \a name is
+ * \p NULL, the function returns \a root. If there is no node with the given
+ * name, \p NULL is returned.
+ */
+struct btr_node *btr_search_node(const char *name, struct btr_node *root)
+{
+ struct btr_node *ch;
+
+ if (!name)
+ return root;
+ if (!strcmp(root->name, name))
+ return root;
+ FOR_EACH_CHILD(ch, root) {
+ struct btr_node *result = btr_search_node(name, ch);
+ if (result)
+ return result;
+ }
+ return NULL;
+}
+
+/** 640K ought to be enough for everybody ;) */
+#define BTRN_MAX_PENDING (96 * 1024)
+
+/**
+ * Return the current state of a buffer tree node.
+ *
+ * \param btrn The node whose state should be queried.
+ * \param min_iqs The minimal input queue size.
+ * \param type The supposed type of \a btrn.
+ *
+ * Most users of the buffer tree subsystem call this function from both
+ * their pre_select and the post_select methods.
+ *
+ * \return Negative if an error condition was detected, zero if there
+ * is nothing to do and positive otherwise.
+ *
+ * Examples:
+ *
+ * - If a non-root node has no parent and an empty input queue, the function
+ * returns \p -E_BTR_EOF. Similarly, if a non-leaf node has no children, \p
+ * -E_BTR_NO_CHILD is returned.
+ *
+ * - If less than \a min_iqs many bytes are available in the input queue and no
+ * EOF condition was detected, the function returns zero.
+ *
+ * - If there's plenty of data left in the input queue of the children of \a
+ * btrn, the function also returns zero in order to bound the memory usage of
+ * the buffer tree.
+ */
+int btr_node_status(struct btr_node *btrn, size_t min_iqs,
+ enum btr_node_type type)
+{
+ size_t iqs;
+
+ assert(btrn);
+ if (type != BTR_NT_LEAF && btr_no_children(btrn))
+ return -E_BTR_NO_CHILD;
+ if (type != BTR_NT_ROOT && btr_eof(btrn))
+ return -E_BTR_EOF;
+
+ if (btr_get_output_queue_size(btrn) > BTRN_MAX_PENDING)
+ return 0;
+ if (type == BTR_NT_ROOT)
+ return 1;
+ iqs = btr_get_input_queue_size(btrn);
+ if (iqs == 0) /* we have a parent, because not eof */
+ return 0;
+ if (iqs < min_iqs && !btr_no_parent(btrn))
+ return 0;
+ return 1;
+}
+
+/**
+ * Get the time of the first I/O for a buffer tree node.
+ *
+ * \param btrn The node whose I/O time should be obtained.
+ * \param tv Result pointer.
+ *
+ * Mainly useful for the time display of para_audiod.
+ */
+void btr_get_node_start(struct btr_node *btrn, struct timeval *tv)
+{
+ *tv = btrn->start;
+}
+
+/**
+ * Get the parent node of a buffer tree node.
+ *
+ * \param btrn The node whose parent should be returned.
+ *
+ * \a btrn must not be \p NULL.
+ *
+ * \return The parent of \a btrn, or \p NULL if \a btrn is the
+ * root node of the buffer tree.
+ */
+struct btr_node *btr_parent(struct btr_node *btrn)
+{
+ return btrn->parent;
+}