]>
code.delx.au - pulseaudio/blob - src/pulsecore/memblockq.c
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/mcalign.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/flist.h>
41 #include "memblockq.h"
44 struct list_item
*next
, *prev
;
49 PA_STATIC_FLIST_DECLARE(list_items
, 0, pa_xfree
);
52 struct list_item
*blocks
, *blocks_tail
;
54 size_t maxlength
, tlength
, base
, prebuf
, minreq
;
55 int64_t read_index
, write_index
;
63 pa_memblockq
* pa_memblockq_new(
70 pa_memblock
*silence
) {
76 bq
= pa_xnew(pa_memblockq
, 1);
77 bq
->blocks
= bq
->blocks_tail
= NULL
;
81 bq
->read_index
= bq
->write_index
= idx
;
83 pa_log_debug("memblockq requested: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
84 (unsigned long) maxlength
, (unsigned long) tlength
, (unsigned long) base
, (unsigned long) prebuf
, (unsigned long) minreq
);
86 bq
->missing
= bq
->requested
= bq
->maxlength
= bq
->tlength
= bq
->prebuf
= bq
->minreq
= 0;
89 pa_memblockq_set_maxlength(bq
, maxlength
);
90 pa_memblockq_set_tlength(bq
, tlength
);
91 pa_memblockq_set_prebuf(bq
, prebuf
);
92 pa_memblockq_set_minreq(bq
, minreq
);
94 pa_log_debug("memblockq sanitized: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
95 (unsigned long)bq
->maxlength
, (unsigned long)bq
->tlength
, (unsigned long)bq
->base
, (unsigned long)bq
->prebuf
, (unsigned long)bq
->minreq
);
97 bq
->silence
= silence
? pa_memblock_ref(silence
) : NULL
;
103 void pa_memblockq_free(pa_memblockq
* bq
) {
106 pa_memblockq_flush(bq
);
109 pa_memblock_unref(bq
->silence
);
112 pa_mcalign_free(bq
->mcalign
);
117 static void drop_block(pa_memblockq
*bq
, struct list_item
*q
) {
121 pa_assert(bq
->n_blocks
>= 1);
124 q
->prev
->next
= q
->next
;
126 bq
->blocks
= q
->next
;
129 q
->next
->prev
= q
->prev
;
131 bq
->blocks_tail
= q
->prev
;
133 pa_memblock_unref(q
->chunk
.memblock
);
135 if (pa_flist_push(PA_STATIC_FLIST_GET(list_items
), q
) < 0)
141 static pa_bool_t
can_push(pa_memblockq
*bq
, size_t l
) {
146 if (bq
->read_index
> bq
->write_index
) {
147 size_t d
= bq
->read_index
- bq
->write_index
;
155 end
= bq
->blocks_tail
? bq
->blocks_tail
->index
+ bq
->blocks_tail
->chunk
.length
: 0;
157 /* Make sure that the list doesn't get too long */
158 if (bq
->write_index
+ (int64_t)l
> end
)
159 if (bq
->write_index
+ l
- bq
->read_index
> bq
->maxlength
)
165 int pa_memblockq_push(pa_memblockq
* bq
, const pa_memchunk
*uchunk
) {
166 struct list_item
*q
, *n
;
172 pa_assert(uchunk
->memblock
);
173 pa_assert(uchunk
->length
> 0);
174 pa_assert(uchunk
->index
+ uchunk
->length
<= pa_memblock_get_length(uchunk
->memblock
));
176 if (uchunk
->length
% bq
->base
)
179 if (!can_push(bq
, uchunk
->length
))
182 old
= bq
->write_index
;
185 if (bq
->read_index
> bq
->write_index
) {
187 /* We currently have a buffer underflow, we need to drop some
190 size_t d
= bq
->read_index
- bq
->write_index
;
192 if (chunk
.length
> d
) {
195 bq
->write_index
+= d
;
197 /* We drop the incoming data completely */
198 bq
->write_index
+= chunk
.length
;
203 /* We go from back to front to look for the right place to add
204 * this new entry. Drop data we will overwrite on the way */
209 if (bq
->write_index
>= q
->index
+ (int64_t) q
->chunk
.length
)
210 /* We found the entry where we need to place the new entry immediately after */
212 else if (bq
->write_index
+ (int64_t) chunk
.length
<= q
->index
) {
213 /* This entry isn't touched at all, let's skip it */
215 } else if (bq
->write_index
<= q
->index
&&
216 bq
->write_index
+ chunk
.length
>= q
->index
+ q
->chunk
.length
) {
218 /* This entry is fully replaced by the new entry, so let's drop it */
224 } else if (bq
->write_index
>= q
->index
) {
225 /* The write index points into this memblock, so let's
226 * truncate or split it */
228 if (bq
->write_index
+ chunk
.length
< q
->index
+ q
->chunk
.length
) {
230 /* We need to save the end of this memchunk */
234 /* Create a new list entry for the end of thie memchunk */
235 if (!(p
= pa_flist_pop(PA_STATIC_FLIST_GET(list_items
))))
236 p
= pa_xnew(struct list_item
, 1);
239 pa_memblock_ref(p
->chunk
.memblock
);
241 /* Calculate offset */
242 d
= bq
->write_index
+ chunk
.length
- q
->index
;
245 /* Drop it from the new entry */
246 p
->index
= q
->index
+ d
;
247 p
->chunk
.length
-= d
;
249 /* Add it to the list */
251 if ((p
->next
= q
->next
))
260 /* Truncate the chunk */
261 if (!(q
->chunk
.length
= bq
->write_index
- q
->index
)) {
268 /* We had to truncate this block, hence we're now at the right position */
273 pa_assert(bq
->write_index
+ (int64_t)chunk
.length
> q
->index
&&
274 bq
->write_index
+ (int64_t)chunk
.length
< q
->index
+ (int64_t)q
->chunk
.length
&&
275 bq
->write_index
< q
->index
);
277 /* The job overwrites the current entry at the end, so let's drop the beginning of this entry */
279 d
= bq
->write_index
+ chunk
.length
- q
->index
;
282 q
->chunk
.length
-= d
;
289 pa_assert(bq
->write_index
>= q
->index
+ (int64_t)q
->chunk
.length
);
290 pa_assert(!q
->next
|| (bq
->write_index
+ (int64_t)chunk
.length
<= q
->next
->index
));
292 /* Try to merge memory blocks */
294 if (q
->chunk
.memblock
== chunk
.memblock
&&
295 q
->chunk
.index
+ (int64_t)q
->chunk
.length
== chunk
.index
&&
296 bq
->write_index
== q
->index
+ (int64_t)q
->chunk
.length
) {
298 q
->chunk
.length
+= chunk
.length
;
299 bq
->write_index
+= chunk
.length
;
303 pa_assert(!bq
->blocks
|| (bq
->write_index
+ (int64_t)chunk
.length
<= bq
->blocks
->index
));
305 if (!(n
= pa_flist_pop(PA_STATIC_FLIST_GET(list_items
))))
306 n
= pa_xnew(struct list_item
, 1);
309 pa_memblock_ref(n
->chunk
.memblock
);
310 n
->index
= bq
->write_index
;
311 bq
->write_index
+= n
->chunk
.length
;
313 n
->next
= q
? q
->next
: bq
->blocks
;
330 delta
= bq
->write_index
- old
;
332 if (delta
>= bq
->requested
) {
333 delta
-= bq
->requested
;
336 bq
->requested
-= delta
;
340 bq
->missing
-= delta
;
345 static pa_bool_t
memblockq_check_prebuf(pa_memblockq
*bq
) {
350 if (pa_memblockq_get_length(bq
) < bq
->prebuf
)
353 bq
->in_prebuf
= FALSE
;
357 if (bq
->prebuf
> 0 && bq
->read_index
>= bq
->write_index
) {
358 bq
->in_prebuf
= TRUE
;
366 int pa_memblockq_peek(pa_memblockq
* bq
, pa_memchunk
*chunk
) {
370 /* We need to pre-buffer */
371 if (memblockq_check_prebuf(bq
))
374 /* Do we need to spit out silence? */
375 if (!bq
->blocks
|| bq
->blocks
->index
> bq
->read_index
) {
379 /* How much silence shall we return? */
380 length
= bq
->blocks
? bq
->blocks
->index
- bq
->read_index
: 0;
382 /* We need to return silence, since no data is yet available */
384 chunk
->memblock
= pa_memblock_ref(bq
->silence
);
386 if (!length
|| length
> pa_memblock_get_length(chunk
->memblock
))
387 length
= pa_memblock_get_length(chunk
->memblock
);
389 chunk
->length
= length
;
392 /* If the memblockq is empty, return -1, otherwise return
393 * the time to sleep */
397 chunk
->memblock
= NULL
;
398 chunk
->length
= length
;
405 /* Ok, let's pass real data to the caller */
406 pa_assert(bq
->blocks
->index
== bq
->read_index
);
408 *chunk
= bq
->blocks
->chunk
;
409 pa_memblock_ref(chunk
->memblock
);
414 void pa_memblockq_drop(pa_memblockq
*bq
, size_t length
) {
417 pa_assert(length
% bq
->base
== 0);
419 old
= bq
->read_index
;
423 /* Do not drop any data when we are in prebuffering mode */
424 if (memblockq_check_prebuf(bq
))
430 pa_assert(bq
->blocks
->index
>= bq
->read_index
);
432 d
= (size_t) (bq
->blocks
->index
- bq
->read_index
);
435 /* The first block is too far in the future */
437 bq
->read_index
+= length
;
445 pa_assert(bq
->blocks
->index
== bq
->read_index
);
447 if (bq
->blocks
->chunk
.length
<= length
) {
448 /* We need to drop the full block */
450 length
-= bq
->blocks
->chunk
.length
;
451 bq
->read_index
+= bq
->blocks
->chunk
.length
;
453 drop_block(bq
, bq
->blocks
);
455 /* Only the start of this block needs to be dropped */
457 bq
->blocks
->chunk
.index
+= length
;
458 bq
->blocks
->chunk
.length
-= length
;
459 bq
->blocks
->index
+= length
;
460 bq
->read_index
+= length
;
466 /* The list is empty, there's nothing we could drop */
467 bq
->read_index
+= length
;
472 delta
= bq
->read_index
- old
;
473 bq
->missing
+= delta
;
476 int pa_memblockq_is_readable(pa_memblockq
*bq
) {
479 if (memblockq_check_prebuf(bq
))
482 if (pa_memblockq_get_length(bq
) <= 0)
488 size_t pa_memblockq_get_length(pa_memblockq
*bq
) {
491 if (bq
->write_index
<= bq
->read_index
)
494 return (size_t) (bq
->write_index
- bq
->read_index
);
497 size_t pa_memblockq_missing(pa_memblockq
*bq
) {
501 if ((l
= pa_memblockq_get_length(bq
)) >= bq
->tlength
)
506 return l
>= bq
->minreq
? l
: 0;
509 size_t pa_memblockq_get_minreq(pa_memblockq
*bq
) {
515 void pa_memblockq_seek(pa_memblockq
*bq
, int64_t offset
, pa_seek_mode_t seek
) {
519 old
= bq
->write_index
;
522 case PA_SEEK_RELATIVE
:
523 bq
->write_index
+= offset
;
525 case PA_SEEK_ABSOLUTE
:
526 bq
->write_index
= offset
;
528 case PA_SEEK_RELATIVE_ON_READ
:
529 bq
->write_index
= bq
->read_index
+ offset
;
531 case PA_SEEK_RELATIVE_END
:
532 bq
->write_index
= (bq
->blocks_tail
? bq
->blocks_tail
->index
+ (int64_t) bq
->blocks_tail
->chunk
.length
: bq
->read_index
) + offset
;
535 pa_assert_not_reached();
538 delta
= bq
->write_index
- old
;
540 if (delta
>= bq
->requested
) {
541 delta
-= bq
->requested
;
543 } else if (delta
>= 0) {
544 bq
->requested
-= delta
;
548 bq
->missing
-= delta
;
551 void pa_memblockq_flush(pa_memblockq
*bq
) {
556 drop_block(bq
, bq
->blocks
);
558 pa_assert(bq
->n_blocks
== 0);
560 old
= bq
->write_index
;
561 bq
->write_index
= bq
->read_index
;
563 pa_memblockq_prebuf_force(bq
);
565 delta
= bq
->write_index
- old
;
567 if (delta
> bq
->requested
) {
568 delta
-= bq
->requested
;
570 } else if (delta
>= 0) {
571 bq
->requested
-= delta
;
575 bq
->missing
-= delta
;
578 size_t pa_memblockq_get_tlength(pa_memblockq
*bq
) {
584 int64_t pa_memblockq_get_read_index(pa_memblockq
*bq
) {
586 return bq
->read_index
;
589 int64_t pa_memblockq_get_write_index(pa_memblockq
*bq
) {
591 return bq
->write_index
;
594 int pa_memblockq_push_align(pa_memblockq
* bq
, const pa_memchunk
*chunk
) {
601 return pa_memblockq_push(bq
, chunk
);
604 bq
->mcalign
= pa_mcalign_new(bq
->base
);
606 if (!can_push(bq
, pa_mcalign_csize(bq
->mcalign
, chunk
->length
)))
609 pa_mcalign_push(bq
->mcalign
, chunk
);
611 while (pa_mcalign_pop(bq
->mcalign
, &rchunk
) >= 0) {
613 r
= pa_memblockq_push(bq
, &rchunk
);
614 pa_memblock_unref(rchunk
.memblock
);
623 void pa_memblockq_shorten(pa_memblockq
*bq
, size_t length
) {
627 l
= pa_memblockq_get_length(bq
);
630 pa_memblockq_drop(bq
, l
- length
);
633 void pa_memblockq_prebuf_disable(pa_memblockq
*bq
) {
636 bq
->in_prebuf
= FALSE
;
639 void pa_memblockq_prebuf_force(pa_memblockq
*bq
) {
642 if (!bq
->in_prebuf
&& bq
->prebuf
> 0)
643 bq
->in_prebuf
= TRUE
;
646 size_t pa_memblockq_get_maxlength(pa_memblockq
*bq
) {
649 return bq
->maxlength
;
652 size_t pa_memblockq_get_prebuf(pa_memblockq
*bq
) {
658 size_t pa_memblockq_pop_missing(pa_memblockq
*bq
) {
663 /* pa_log("pop: %lli", bq->missing); */
665 if (bq
->missing
<= 0)
668 l
= (size_t) bq
->missing
;
675 void pa_memblockq_set_maxlength(pa_memblockq
*bq
, size_t maxlength
) {
678 bq
->maxlength
= ((maxlength
+bq
->base
-1)/bq
->base
)*bq
->base
;
680 if (bq
->maxlength
< bq
->base
)
681 bq
->maxlength
= bq
->base
;
683 if (bq
->tlength
> bq
->maxlength
)
684 pa_memblockq_set_tlength(bq
, bq
->maxlength
);
686 if (bq
->prebuf
> bq
->maxlength
)
687 pa_memblockq_set_prebuf(bq
, bq
->maxlength
);
690 void pa_memblockq_set_tlength(pa_memblockq
*bq
, size_t tlength
) {
694 old_tlength
= bq
->tlength
;
697 tlength
= bq
->maxlength
;
699 bq
->tlength
= ((tlength
+bq
->base
-1)/bq
->base
)*bq
->base
;
701 if (bq
->tlength
> bq
->maxlength
)
702 bq
->tlength
= bq
->maxlength
;
704 if (bq
->minreq
> bq
->tlength
- bq
->prebuf
)
705 pa_memblockq_set_minreq(bq
, bq
->tlength
- bq
->prebuf
);
707 bq
->missing
+= (int64_t) bq
->tlength
- (int64_t) old_tlength
;
710 void pa_memblockq_set_prebuf(pa_memblockq
*bq
, size_t prebuf
) {
713 bq
->prebuf
= (prebuf
== (size_t) -1) ? bq
->tlength
/2 : prebuf
;
714 bq
->prebuf
= ((bq
->prebuf
+bq
->base
-1)/bq
->base
)*bq
->base
;
716 if (prebuf
> 0 && bq
->prebuf
< bq
->base
)
717 bq
->prebuf
= bq
->base
;
719 if (bq
->prebuf
> bq
->maxlength
)
720 bq
->prebuf
= bq
->maxlength
;
722 if (bq
->prebuf
<= 0 || pa_memblockq_get_length(bq
) >= bq
->prebuf
)
723 bq
->in_prebuf
= FALSE
;
725 if (bq
->minreq
> bq
->tlength
- bq
->prebuf
)
726 pa_memblockq_set_minreq(bq
, bq
->tlength
- bq
->prebuf
);
729 void pa_memblockq_set_minreq(pa_memblockq
*bq
, size_t minreq
) {
732 bq
->minreq
= (minreq
/bq
->base
)*bq
->base
;
734 if (bq
->minreq
> bq
->tlength
- bq
->prebuf
)
735 bq
->minreq
= bq
->tlength
- bq
->prebuf
;
737 if (bq
->minreq
< bq
->base
)
738 bq
->minreq
= bq
->base
;