2 * Copyright (C) 2009-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file buffer_tree.c Buffer tree and buffer pool implementations. */
14 #include "buffer_tree.h"
18 /* whead = NULL means area full */
30 /** The number of references to this buffer. */
32 /* NULL means no buffer pool but a malloced buffer. */
33 struct btr_pool *pool;
36 struct btr_buffer_reference {
37 struct btr_buffer *btrb;
39 /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
40 struct list_head node;
46 struct btr_node *parent;
47 /* The position of this btr node in the buffer tree. */
48 struct list_head node;
49 /* The children nodes of this btr node are linked together in a list. */
50 struct list_head children;
51 /* Time of first data transfer. */
54 * The input queue is a list of references to btr buffers. Each item on
55 * the list represents an input buffer which has not been completely
56 * used by this btr node.
58 struct list_head input_queue;
59 btr_command_handler execute;
64 * Create a new buffer pool.
66 * \param name The name of the new buffer pool.
67 * \param area_size The size in bytes of the pool area.
69 * \return An opaque pointer to the newly created buffer pool. It must be
70 * passed to btr_pool_free() after it is no longer used to deallocate all
73 struct btr_pool *btr_pool_new(const char *name, size_t area_size)
75 struct btr_pool *btrp;
77 PARA_INFO_LOG("%s, %zu bytes\n", name, area_size);
78 btrp = para_malloc(sizeof(*btrp));
79 btrp->area_start = para_malloc(area_size);
80 btrp->area_end = btrp->area_start + area_size;
81 btrp->rhead = btrp->area_start;
82 btrp->whead = btrp->area_start;
83 btrp->name = para_strdup(name);
88 * Deallocate resources used by a buffer pool.
90 * \param btrp A pointer obtained via btr_pool_new().
92 void btr_pool_free(struct btr_pool *btrp)
96 free(btrp->area_start);
102 * Return the size of the buffer pool area.
104 * \param btrp The buffer pool.
106 * \return The same value which was passed during creation time to
109 size_t btr_pool_size(struct btr_pool *btrp)
111 return btrp->area_end - btrp->area_start;
114 static size_t btr_pool_filled(struct btr_pool *btrp)
117 return btr_pool_size(btrp);
118 if (btrp->rhead <= btrp->whead)
119 return btrp->whead - btrp->rhead;
120 return btr_pool_size(btrp) - (btrp->rhead - btrp->whead);
124 * Get the number of unused bytes in the buffer pool.
126 * \param btrp The pool.
128 * \return The number of bytes that can currently be allocated.
130 * Note that in general the returned number of bytes is not available as a
131 * single contiguous buffer. Use btr_pool_available() to obtain the length of
132 * the largest contiguous buffer that can currently be allocated from the
135 size_t btr_pool_unused(struct btr_pool *btrp)
137 return btr_pool_size(btrp) - btr_pool_filled(btrp);
141 * Return maximal size available for one read. This is
142 * smaller than the value returned by btr_pool_unused().
144 static size_t btr_pool_available(struct btr_pool *btrp)
148 if (btrp->rhead <= btrp->whead)
149 return btrp->area_end - btrp->whead;
150 return btrp->rhead - btrp->whead;
154 * Obtain the current write head.
156 * \param btrp The buffer pool.
157 * \param result The write head is returned here.
159 * \return The maximal amount of bytes that may be written to the returned
162 size_t btr_pool_get_buffer(struct btr_pool *btrp, char **result)
165 *result = btrp->whead;
166 return btr_pool_available(btrp);
170 * Get references to buffers pointing to free space of the buffer pool area.
172 * \param btrp The buffer pool.
173 * \param iov The scatter array.
175 * \return Zero if the buffer pool is full, one if the free space of the buffer
176 * pool area is available as a single contiguous buffer, two if the free space
177 * consists of two buffers. If this function returns the value n, then n
178 * elements of \a iov are initialized.
180 int btr_pool_get_buffers(struct btr_pool *btrp, struct iovec iov[2])
185 sz = btr_pool_get_buffer(btrp, &buf);
189 iov[0].iov_base = buf;
190 unused = btr_pool_unused(btrp);
193 iov[1].iov_len = unused - sz;
194 iov[1].iov_base = btrp->area_start;
199 * Mark a part of the buffer pool area as allocated.
201 * \param btrp The buffer pool.
202 * \param size The amount of bytes to be allocated.
204 * This is usually called after the caller wrote to the buffer obtained by
205 * btr_pool_get_buffer().
207 static void btr_pool_allocate(struct btr_pool *btrp, size_t size)
213 assert(size <= btr_pool_available(btrp));
214 end = btrp->whead + size;
215 assert(end <= btrp->area_end);
217 if (end == btrp->area_end) {
218 PARA_DEBUG_LOG("%s: end of pool area reached\n", btrp->name);
219 end = btrp->area_start;
221 if (end == btrp->rhead) {
222 PARA_DEBUG_LOG("%s btrp buffer full\n", btrp->name);
223 end = NULL; /* buffer full */
228 static void btr_pool_deallocate(struct btr_pool *btrp, size_t size)
230 char *end = btrp->rhead + size;
234 assert(end <= btrp->area_end);
235 assert(size <= btr_pool_filled(btrp));
236 if (end == btrp->area_end)
237 end = btrp->area_start;
239 btrp->whead = btrp->rhead;
241 if (btrp->rhead == btrp->whead)
242 btrp->rhead = btrp->whead = btrp->area_start;
245 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
246 &((_btrn)->children), node)
247 #define FOR_EACH_CHILD_SAFE(_tn, _tmp, _btrn) \
248 list_for_each_entry_safe((_tn), (_tmp), &((_btrn)->children), node)
250 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
251 list_for_each_entry((_br), &(_btrn)->input_queue, node)
252 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
253 list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
256 * Create a new buffer tree node.
258 * \param bnd Specifies how to create the new node.
260 * \return A pointer to the newly allocated node.
262 * This function always succeeds (or calls exit()). The returned pointer must
263 * be freed using btr_free_node() after the node has been removed from the
264 * buffer tree via btr_remove_node().
266 struct btr_node *btr_new_node(struct btr_node_description *bnd)
268 struct btr_node *btrn = para_malloc(sizeof(*btrn));
270 btrn->name = para_strdup(bnd->name);
271 btrn->parent = bnd->parent;
272 btrn->execute = bnd->handler;
273 btrn->context = bnd->context;
274 btrn->start.tv_sec = 0;
275 btrn->start.tv_usec = 0;
276 INIT_LIST_HEAD(&btrn->children);
277 INIT_LIST_HEAD(&btrn->input_queue);
280 list_add_tail(&btrn->node, &bnd->parent->children);
281 PARA_INFO_LOG("new leaf node: %s (child of %s)\n",
282 bnd->name, bnd->parent->name);
284 PARA_INFO_LOG("added %s as btr root\n", bnd->name);
288 assert(!bnd->child->parent);
289 PARA_INFO_LOG("new root: %s (was %s)\n",
290 bnd->name, bnd->child->name);
292 list_add_tail(&bnd->child->node, &btrn->children);
294 bnd->child->parent = btrn;
297 PARA_EMERG_LOG("inserting internal nodes not yet supported.\n");
299 assert(bnd->child->parent == bnd->parent);
305 * Allocate a new btr buffer.
307 * The freshly allocated buffer will have a zero refcount and will
308 * not be associated with a btr pool.
310 static struct btr_buffer *new_btrb(char *buf, size_t size)
312 struct btr_buffer *btrb = para_calloc(sizeof(*btrb));
319 static void dealloc_buffer(struct btr_buffer *btrb)
322 btr_pool_deallocate(btrb->pool, btrb->size);
327 static struct btr_buffer_reference *get_first_input_br(struct btr_node *btrn)
329 if (list_empty(&btrn->input_queue))
331 return list_first_entry(&btrn->input_queue,
332 struct btr_buffer_reference, node);
336 * Deallocate the reference, release the resources if refcount drops to zero.
338 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
340 struct btr_buffer *btrb = br->btrb;
345 if (btrb->refcount == 0) {
346 dealloc_buffer(btrb);
351 static void add_btrb_to_children(struct btr_buffer *btrb,
352 struct btr_node *btrn, size_t consumed)
356 if (btrn->start.tv_sec == 0)
358 FOR_EACH_CHILD(ch, btrn) {
359 struct btr_buffer_reference *br = para_calloc(sizeof(*br));
361 br->consumed = consumed;
362 list_add_tail(&br->node, &ch->input_queue);
364 if (ch->start.tv_sec == 0)
370 * Insert a malloced buffer into the buffer tree.
372 * \param buf The buffer to insert.
373 * \param size The size of \a buf in bytes.
374 * \param btrn Position in the buffer tree to create the output.
376 * This creates references to \a buf and adds these references to each child of
377 * \a btrn. The buffer will be freed using standard free() once no buffer tree
378 * node is referencing it any more.
380 * Note that this function must not be used if \a buf was obtained from a
381 * buffer pool. Use btr_add_output_pool() in this case.
383 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
385 struct btr_buffer *btrb;
388 if (list_empty(&btrn->children)) {
392 btrb = new_btrb(buf, size);
393 add_btrb_to_children(btrb, btrn, 0);
397 * Feed data to child nodes of a buffer tree node.
399 * \param btrp The buffer pool.
400 * \param size The number of bytes to be allocated and fed to each child.
401 * \param btrn The node whose children are to be fed.
403 * This function allocates the amount of bytes from the buffer pool area,
404 * starting at the current value of the write head, and creates buffer
405 * references to the resulting part of the buffer pool area, one for each child
406 * of \a btrn. The references are then fed into the input queue of each child.
408 void btr_add_output_pool(struct btr_pool *btrp, size_t size,
409 struct btr_node *btrn)
411 struct btr_buffer *btrb;
416 if (list_empty(&btrn->children))
418 avail = btr_pool_get_buffer(btrp, &buf);
419 assert(avail >= size);
420 btr_pool_allocate(btrp, size);
421 btrb = new_btrb(buf, size);
423 add_btrb_to_children(btrb, btrn, 0);
427 * Copy data to write head of a buffer pool and feed it to all children nodes.
429 * \param src The source buffer.
430 * \param n The size of the source buffer in bytes.
431 * \param btrp The destination buffer pool.
432 * \param btrn Add the data as output of this node.
434 * This is expensive. The caller must make sure the data fits into the buffer
437 void btr_copy(const void *src, size_t n, struct btr_pool *btrp,
438 struct btr_node *btrn)
445 assert(n <= btr_pool_unused(btrp));
446 sz = btr_pool_get_buffer(btrp, &buf);
447 copy = PARA_MIN(sz, n);
448 memcpy(buf, src, copy);
449 btr_add_output_pool(btrp, copy, btrn);
452 sz = btr_pool_get_buffer(btrp, &buf);
453 assert(sz >= n - copy);
454 memcpy(buf, src + copy, n - copy);
455 btr_add_output_pool(btrp, n - copy, btrn);
458 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
460 add_btrb_to_children(br->btrb, btrn, br->consumed);
461 btr_drop_buffer_reference(br);
465 * Feed all buffer references of the input queue through the output channel.
467 * \param btrn The node whose buffer references should be pushed down.
469 * This function is useful for filters that do not change the contents of the
470 * buffers at all, like the wav filter or the amp filter if no amplification
471 * was specified. This function is rather cheap.
473 * \sa \ref btr_pushdown_one().
475 void btr_pushdown(struct btr_node *btrn)
477 struct btr_buffer_reference *br, *tmp;
479 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
480 btr_pushdown_br(br, btrn);
484 * Feed the next buffer of the input queue through the output channel.
486 * \param btrn The node whose first input queue buffer should be pushed down.
488 * This works like \ref btr_pushdown() but pushes down only one buffer
491 void btr_pushdown_one(struct btr_node *btrn)
493 struct btr_buffer_reference *br;
495 if (list_empty(&btrn->input_queue))
497 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
498 btr_pushdown_br(br, btrn);
502 * Find out whether a node is a leaf node.
504 * \param btrn The node to check.
506 * \return True if this node has no children. False otherwise.
508 static bool btr_no_children(struct btr_node *btrn)
510 return list_empty(&btrn->children);
514 * Find out whether a node is an orphan node.
516 * \param btrn The buffer tree node.
518 * \return True if \a btrn has no parent.
520 * This function will always return true for the root node. However in case
521 * nodes have been removed from the tree, other nodes may become orphans too.
523 bool btr_no_parent(struct btr_node *btrn)
525 return !btrn->parent;
529 * Find out whether it is OK to change an input buffer.
531 * \param btrn The buffer tree node to check.
533 * This is used by filters that produce exactly the same amount of output as
534 * there is input. The amp filter which multiplies each sample by some number
535 * is an example of such a filter. If there are no other nodes in the buffer
536 * tree that read the same input stream (i.e. if \a btrn has no siblings), a
537 * node may modify its input buffer directly and push down the modified buffer
538 * to its children, thereby avoiding to allocate a possibly large additional
541 * Since the buffer tree may change at any time, this function should be called
542 * during each post_select call.
544 * \return True if \a btrn has no siblings.
546 bool btr_inplace_ok(struct btr_node *btrn)
550 return list_is_singular(&btrn->parent->children);
553 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
555 return br->btrb->size - br->consumed;
558 static size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
561 *buf = br->btrb->buf + br->consumed;
562 return br_available_bytes(br);
566 * Obtain the next buffer of the input queue, omitting data.
568 * \param btrn The node whose input queue is to be queried.
569 * \param omit Number of bytes to be omitted.
570 * \param bufp Result pointer.
572 * If a buffer tree node needs more input data but can not consume the data it
573 * already has (because it might be needed again later) this function can be
574 * used instead of btr_next_buffer() to get a reference to the buffer obtained
575 * by skipping the given number of bytes. Skipped input bytes are not consumed.
577 * With a zero \a omit argument, this function is equivalent to \ref
580 * \return Number of bytes in \a bufp. If there are less than or equal to \a
581 * omit many bytes available in the input queue of the buffer tree node pointed
582 * to by \a btrn, the function returns zero and the value of \a bufp is
585 size_t btr_next_buffer_omit(struct btr_node *btrn, size_t omit, char **bufp)
587 struct btr_buffer_reference *br;
588 size_t wrap_count, sz, rv = 0;
589 char *buf, *result = NULL;
591 br = get_first_input_br(btrn);
594 wrap_count = br->wrap_count;
595 if (wrap_count > 0) { /* we have a wrap buffer */
596 sz = btr_get_buffer_by_reference(br, &buf);
597 if (sz > omit) { /* and it's big enough */
601 * Wrap buffers are allocated by malloc(), so the next
602 * buffer ref will not align nicely, so we return the
603 * tail of the wrap buffer.
608 * The next wrap_count bytes exist twice, in the wrap buffer
609 * and as a buffer reference in the buffer tree pool.
614 * For buffer tree pools, the buffers in the list align, i.e. the next
615 * buffer in the list starts directly at the end of its predecessor. In
616 * this case we merge adjacent buffers and return one larger buffer
619 FOR_EACH_BUFFER_REF(br, btrn) {
620 sz = btr_get_buffer_by_reference(br, &buf);
622 if (result + rv != buf)
625 } else if (sz > omit) {
640 * Obtain the next buffer of the input queue of a buffer tree node.
642 * \param btrn The node whose input queue is to be queried.
643 * \param bufp Result pointer.
645 * \return The number of bytes that can be read from buf.
647 * The call of this function is is equivalent to calling \ref
648 * btr_next_buffer_omit() with an \a omit value of zero.
650 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
652 return btr_next_buffer_omit(btrn, 0, bufp);
656 * Deallocate the given number of bytes from the input queue.
658 * \param btrn The buffer tree node.
659 * \param numbytes The number of bytes to be deallocated.
661 * This function must be used to get rid of existing buffer references in the
662 * node's input queue. If no references to a buffer remain, the underlying
663 * buffers are either freed (in the non-buffer pool case) or the read head of
664 * the buffer pool is being advanced.
666 * Note that \a numbytes may be smaller than the buffer size. In this case the
667 * buffer is not deallocated and subsequent calls to btr_next_buffer() return
668 * the remaining part of the buffer.
670 void btr_consume(struct btr_node *btrn, size_t numbytes)
672 struct btr_buffer_reference *br, *tmp;
677 br = get_first_input_br(btrn);
680 if (br->wrap_count == 0) {
682 * No wrap buffer. Drop buffer references whose buffer
683 * has been fully used. */
684 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn) {
685 if (br->consumed + numbytes <= br->btrb->size) {
686 br->consumed += numbytes;
687 if (br->consumed == br->btrb->size)
688 btr_drop_buffer_reference(br);
691 numbytes -= br->btrb->size - br->consumed;
692 btr_drop_buffer_reference(br);
697 * We have a wrap buffer, consume from it. If in total, i.e. including
698 * previous calls to brt_consume(), less than wrap_count has been
699 * consumed, there's nothing more we can do.
701 * Otherwise we drop the wrap buffer and consume from subsequent
702 * buffers of the input queue the correct amount of bytes. This is the
703 * total number of bytes that have been consumed from the wrap buffer.
705 PARA_DEBUG_LOG("consuming %zu/%zu bytes from wrap buffer\n", numbytes,
706 br_available_bytes(br));
708 assert(numbytes <= br_available_bytes(br));
709 if (br->consumed + numbytes < br->wrap_count) {
710 br->consumed += numbytes;
713 PARA_DEBUG_LOG("dropping wrap buffer (%zu bytes)\n", br->btrb->size);
714 /* get rid of the wrap buffer */
715 sz = br->consumed + numbytes;
716 btr_drop_buffer_reference(br);
717 return btr_consume(btrn, sz);
721 * Clear the input queue of a buffer tree node.
723 * \param btrn The node whose input queue should be cleared.
725 void btr_drain(struct btr_node *btrn)
727 struct btr_buffer_reference *br, *tmp;
729 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
730 btr_drop_buffer_reference(br);
734 * Free all resources allocated by btr_new_node().
736 * \param btrn Pointer to a btr node obtained by \ref btr_new_node().
738 * Like free(3), it is OK to call this with a \p NULL pointer argument.
740 void btr_free_node(struct btr_node *btrn)
749 * Remove a node from a buffer tree.
751 * \param btrn The node to remove.
753 * This makes all child nodes of \a btrn orphans and removes \a btrn from the
754 * list of children of its parent. Moreover, the input queue of \a btrn is
755 * flushed if it is not empty.
757 * \sa \ref btr_splice_out_node.
759 void btr_remove_node(struct btr_node *btrn)
765 PARA_NOTICE_LOG("removing btr node %s from buffer tree\n", btrn->name);
766 FOR_EACH_CHILD(ch, btrn)
770 list_del(&btrn->node);
774 * Return the amount of available input bytes of a buffer tree node.
776 * \param btrn The node whose input size should be computed.
778 * \return The total number of bytes available in the node's input
781 * This simply iterates over all buffer references in the input queue and
782 * returns the sum of the sizes of all references.
784 size_t btr_get_input_queue_size(struct btr_node *btrn)
786 struct btr_buffer_reference *br;
787 size_t size = 0, wrap_consumed = 0;
789 FOR_EACH_BUFFER_REF(br, btrn) {
790 if (br->wrap_count != 0) {
791 wrap_consumed = br->consumed;
794 size += br_available_bytes(br);
796 assert(wrap_consumed <= size);
797 size -= wrap_consumed;
802 * Remove a node from the buffer tree, reconnecting parent and children.
804 * \param btrn The node to splice out.
806 * This function is used by buffer tree nodes that do not exist during the
807 * whole lifetime of the buffer tree. Unlike btr_remove_node(), calling
808 * btr_splice_out_node() does not split the tree into disconnected components
809 * but reconnects the buffer tree by making all child nodes of \a btrn children
810 * of the parent of \a btrn.
812 void btr_splice_out_node(struct btr_node *btrn)
814 struct btr_node *ch, *tmp;
817 PARA_NOTICE_LOG("splicing out %s\n", btrn->name);
820 list_del(&btrn->node);
821 FOR_EACH_CHILD_SAFE(ch, tmp, btrn) {
822 PARA_INFO_LOG("parent(%s): %s\n", ch->name,
823 btrn->parent? btrn->parent->name : "NULL");
824 ch->parent = btrn->parent;
826 list_move(&ch->node, &btrn->parent->children);
828 assert(list_empty(&btrn->children));
832 * Return number of queued output bytes of a buffer tree node.
834 * \param btrn The node whose output queue size should be computed.
836 * \return This function iterates over all children of the given node and
837 * returns the size of the largest input queue.
839 size_t btr_get_output_queue_size(struct btr_node *btrn)
844 FOR_EACH_CHILD(ch, btrn) {
845 size_t size = btr_get_input_queue_size(ch);
846 max_size = PARA_MAX(max_size, size);
852 * Execute a inter-node command on a parent node.
854 * \param btrn The node to start looking.
855 * \param command The command to execute.
856 * \param value_result Additional arguments and result value.
858 * This function traverses the buffer tree upwards and looks for parent nodes
859 * of \a btrn that understands \a command. On the first such node the command
860 * is executed, and the result is stored in \a value_result.
862 * \return \p -ENOTSUP if no parent node of \a btrn understands \a command.
863 * Otherwise the return value of the command handler is returned.
865 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
869 for (; btrn; btrn = btrn->parent) {
870 struct btr_node *parent = btrn->parent;
872 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
873 if (!parent->execute)
875 PARA_INFO_LOG("parent: %s, cmd: %s\n", parent->name, command);
876 ret = parent->execute(parent, command, value_result);
877 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
881 if (value_result && *value_result)
882 PARA_NOTICE_LOG("%s(%s): %s\n", command, parent->name,
886 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
890 * Obtain the context of a buffer node tree.
892 * \param btrn The node whose output queue size should be computed.
894 * \return A pointer to the \a context address specified at node creation time.
896 * \sa btr_new_node(), struct \ref btr_node_description.
898 void *btr_context(struct btr_node *btrn)
900 return btrn->context;
903 static bool need_buffer_pool_merge(struct btr_node *btrn)
905 struct btr_buffer_reference *br = get_first_input_br(btrn);
909 if (br->wrap_count != 0)
916 static void merge_input_pool(struct btr_node *btrn, size_t dest_size)
918 struct btr_buffer_reference *br, *wbr = NULL;
919 int num_refs; /* including wrap buffer */
920 char *buf, *buf1 = NULL, *buf2 = NULL;
921 size_t sz, sz1 = 0, sz2 = 0, wb_consumed = 0;
923 br = get_first_input_br(btrn);
924 if (!br || br_available_bytes(br) >= dest_size)
927 FOR_EACH_BUFFER_REF(br, btrn) {
929 sz = btr_get_buffer_by_reference(br, &buf);
932 if (br->wrap_count != 0) {
934 assert(num_refs == 1);
938 wb_consumed = br->consumed;
946 if (buf1 + sz1 == buf) {
955 assert(buf2 + sz2 == buf);
958 if (sz1 + sz2 >= dest_size + wb_consumed)
961 if (!buf2) /* nothing to do */
963 assert(buf1 && sz2 > 0);
965 * If the second buffer is large, we only take the first part of it to
966 * avoid having to memcpy() huge buffers.
968 sz2 = PARA_MIN(sz2, (size_t)(64 * 1024));
970 /* Make a new wrap buffer combining buf1 and buf2. */
972 buf = para_malloc(sz);
973 PARA_DEBUG_LOG("merging input buffers: (%p:%zu, %p:%zu) -> %p:%zu\n",
974 buf1, sz1, buf2, sz2, buf, sz);
975 memcpy(buf, buf1, sz1);
976 memcpy(buf + sz1, buf2, sz2);
977 br = para_calloc(sizeof(*br));
978 br->btrb = new_btrb(buf, sz);
979 br->btrb->refcount = 1;
981 /* This is a wrap buffer */
982 br->wrap_count = sz1;
983 para_list_add(&br->node, &btrn->input_queue);
987 * We already have a wrap buffer, but it is too small. It might be
990 if (wbr->wrap_count == sz1 && wbr->btrb->size >= sz1 + sz2) /* nothing we can do about it */
992 sz = sz1 + sz2 - wbr->btrb->size; /* amount of new data */
993 PARA_DEBUG_LOG("increasing wrap buffer %zu -> %zu\n", wbr->btrb->size,
994 wbr->btrb->size + sz);
995 wbr->btrb->size += sz;
996 wbr->btrb->buf = para_realloc(wbr->btrb->buf, wbr->btrb->size);
997 /* copy the new data to the end of the reallocated buffer */
999 memcpy(wbr->btrb->buf + wbr->btrb->size - sz, buf2 + sz2 - sz, sz);
1003 * Merge the first two input buffers into one.
1005 * This is a quite expensive operation.
1007 * \return The number of buffers that have been available (zero, one or two).
1009 static int merge_input(struct btr_node *btrn)
1011 struct btr_buffer_reference *brs[2], *br;
1012 char *bufs[2], *buf;
1016 if (list_empty(&btrn->input_queue))
1018 if (list_is_singular(&btrn->input_queue))
1021 /* get references to the first two buffers */
1022 FOR_EACH_BUFFER_REF(br, btrn) {
1024 szs[i] = btr_get_buffer_by_reference(brs[i], bufs + i);
1030 /* make a new btrb that combines the two buffers and a br to it. */
1031 sz = szs[0] + szs[1];
1032 buf = para_malloc(sz);
1033 PARA_DEBUG_LOG("%s: memory merging input buffers: (%zu, %zu) -> %zu\n",
1034 btrn->name, szs[0], szs[1], sz);
1035 memcpy(buf, bufs[0], szs[0]);
1036 memcpy(buf + szs[0], bufs[1], szs[1]);
1038 br = para_calloc(sizeof(*br));
1039 br->btrb = new_btrb(buf, sz);
1040 br->btrb->refcount = 1;
1042 /* replace the first two refs by the new one */
1043 btr_drop_buffer_reference(brs[0]);
1044 btr_drop_buffer_reference(brs[1]);
1045 para_list_add(&br->node, &btrn->input_queue);
1050 * Combine input queue buffers.
1052 * \param btrn The buffer tree node whose input should be merged.
1053 * \param dest_size Stop merging if a buffer of at least this size exists.
1055 * Used to combine as many buffers as needed into a single buffer whose size is
1056 * at least \a dest_size. This function is rather cheap in case the parent node
1057 * uses buffer pools and rather expensive otherwise.
1059 * Note that if less than \a dest_size bytes are available in total, this
1060 * function does nothing and subsequent calls to btr_next_buffer() will still
1061 * return a buffer size less than \a dest_size.
1063 void btr_merge(struct btr_node *btrn, size_t dest_size)
1065 if (need_buffer_pool_merge(btrn))
1066 return merge_input_pool(btrn, dest_size);
1069 size_t len = btr_next_buffer(btrn, &buf);
1070 if (len >= dest_size)
1072 PARA_DEBUG_LOG("input size = %zu < %zu = dest\n", len, dest_size);
1073 if (merge_input(btrn) < 2)
1078 static bool btr_eof(struct btr_node *btrn)
1081 size_t len = btr_next_buffer(btrn, &buf);
1083 return (len == 0 && btr_no_parent(btrn));
1086 static void log_tree_recursively(struct btr_node *btrn, int loglevel, int depth)
1088 struct btr_node *ch;
1089 const char spaces[] = " ", *space = spaces + 16 - depth;
1093 para_log(loglevel, "%s%s\n", space, btrn->name);
1094 FOR_EACH_CHILD(ch, btrn)
1095 log_tree_recursively(ch, loglevel, depth + 1);
1099 * Write the current buffer (sub-)tree to the log.
1101 * \param btrn Start logging at this node.
1102 * \param loglevel Set severity with which the tree should be logged.
1104 void btr_log_tree(struct btr_node *btrn, int loglevel)
1106 return log_tree_recursively(btrn, loglevel, 0);
1110 * Find the node with the given name in the buffer tree.
1112 * \param name The name of the node to search.
1113 * \param root Where to start the search.
1115 * \return A pointer to the node with the given name on success. If \a name is
1116 * \p NULL, the function returns \a root. If there is no node with the given
1117 * name, \p NULL is returned.
1119 struct btr_node *btr_search_node(const char *name, struct btr_node *root)
1121 struct btr_node *ch;
1125 if (!strcmp(root->name, name))
1127 FOR_EACH_CHILD(ch, root) {
1128 struct btr_node *result = btr_search_node(name, ch);
1135 /** 640K ought to be enough for everybody ;) */
1136 #define BTRN_MAX_PENDING (96 * 1024)
1139 * Return the current state of a buffer tree node.
1141 * \param btrn The node whose state should be queried.
1142 * \param min_iqs The minimal input queue size.
1143 * \param type The supposed type of \a btrn.
1145 * Most users of the buffer tree subsystem call this function from both
1146 * their pre_select and the post_select methods.
1148 * \return Negative if an error condition was detected, zero if there
1149 * is nothing to do and positive otherwise.
1153 * - If a non-root node has no parent and an empty input queue, the function
1154 * returns \p -E_BTR_EOF. Similarly, if a non-leaf node has no children, \p
1155 * -E_BTR_NO_CHILD is returned.
1157 * - If less than \a min_iqs many bytes are available in the input queue and no
1158 * EOF condition was detected, the function returns zero.
1160 * - If there's plenty of data left in the input queue of the children of \a
1161 * btrn, the function also returns zero in order to bound the memory usage of
1164 int btr_node_status(struct btr_node *btrn, size_t min_iqs,
1165 enum btr_node_type type)
1170 if (type != BTR_NT_LEAF) {
1171 if (btr_no_children(btrn))
1172 return -E_BTR_NO_CHILD;
1173 if (btr_get_output_queue_size(btrn) > BTRN_MAX_PENDING)
1176 if (type != BTR_NT_ROOT) {
1179 iqs = btr_get_input_queue_size(btrn);
1180 if (iqs == 0) /* we have a parent, because not eof */
1182 if (iqs < min_iqs && !btr_no_parent(btrn))
1189 * Get the time of the first I/O for a buffer tree node.
1191 * \param btrn The node whose I/O time should be obtained.
1192 * \param tv Result pointer.
1194 * Mainly useful for the time display of para_audiod.
1196 void btr_get_node_start(struct btr_node *btrn, struct timeval *tv)