]>
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
;
61 pa_memblockq
* pa_memblockq_new(
68 pa_memblock
*silence
) {
73 pa_assert(maxlength
>= base
);
75 bq
= pa_xnew(pa_memblockq
, 1);
76 bq
->blocks
= bq
->blocks_tail
= NULL
;
80 bq
->read_index
= bq
->write_index
= idx
;
82 pa_log_debug("memblockq requested: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
83 (unsigned long) maxlength
, (unsigned long) tlength
, (unsigned long) base
, (unsigned long) prebuf
, (unsigned long) minreq
);
85 bq
->maxlength
= ((maxlength
+base
-1)/base
)*base
;
86 pa_assert(bq
->maxlength
>= base
);
88 bq
->tlength
= ((tlength
+base
-1)/base
)*base
;
89 if (bq
->tlength
<= 0 || bq
->tlength
> bq
->maxlength
)
90 bq
->tlength
= bq
->maxlength
;
92 bq
->prebuf
= (prebuf
== (size_t) -1) ? bq
->tlength
/2 : prebuf
;
93 bq
->prebuf
= ((bq
->prebuf
+base
-1)/base
)*base
;
94 if (bq
->prebuf
> bq
->maxlength
)
95 bq
->prebuf
= bq
->maxlength
;
97 bq
->minreq
= (minreq
/base
)*base
;
99 if (bq
->minreq
> bq
->tlength
- bq
->prebuf
)
100 bq
->minreq
= bq
->tlength
- bq
->prebuf
;
105 pa_log_debug("memblockq sanitized: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
106 (unsigned long)bq
->maxlength
, (unsigned long)bq
->tlength
, (unsigned long)bq
->base
, (unsigned long)bq
->prebuf
, (unsigned long)bq
->minreq
);
108 bq
->in_prebuf
= bq
->prebuf
> 0;
109 bq
->silence
= silence
? pa_memblock_ref(silence
) : NULL
;
115 void pa_memblockq_free(pa_memblockq
* bq
) {
118 pa_memblockq_flush(bq
);
121 pa_memblock_unref(bq
->silence
);
124 pa_mcalign_free(bq
->mcalign
);
129 static void drop_block(pa_memblockq
*bq
, struct list_item
*q
) {
133 pa_assert(bq
->n_blocks
>= 1);
136 q
->prev
->next
= q
->next
;
138 bq
->blocks
= q
->next
;
141 q
->next
->prev
= q
->prev
;
143 bq
->blocks_tail
= q
->prev
;
145 pa_memblock_unref(q
->chunk
.memblock
);
147 if (pa_flist_push(PA_STATIC_FLIST_GET(list_items
), q
) < 0)
153 static int can_push(pa_memblockq
*bq
, size_t l
) {
158 if (bq
->read_index
> bq
->write_index
) {
159 size_t d
= bq
->read_index
- bq
->write_index
;
167 end
= bq
->blocks_tail
? bq
->blocks_tail
->index
+ bq
->blocks_tail
->chunk
.length
: 0;
169 /* Make sure that the list doesn't get too long */
170 if (bq
->write_index
+ (int64_t)l
> end
)
171 if (bq
->write_index
+ l
- bq
->read_index
> bq
->maxlength
)
177 int pa_memblockq_push(pa_memblockq
* bq
, const pa_memchunk
*uchunk
) {
179 struct list_item
*q
, *n
;
184 pa_assert(uchunk
->memblock
);
185 pa_assert(uchunk
->length
> 0);
186 pa_assert(uchunk
->index
+ uchunk
->length
<= pa_memblock_get_length(uchunk
->memblock
));
188 if (uchunk
->length
% bq
->base
)
191 if (!can_push(bq
, uchunk
->length
))
196 if (bq
->read_index
> bq
->write_index
) {
198 /* We currently have a buffer underflow, we need to drop some
201 size_t d
= bq
->read_index
- bq
->write_index
;
203 if (chunk
.length
> d
) {
206 bq
->write_index
+= d
;
208 /* We drop the incoming data completely */
209 bq
->write_index
+= chunk
.length
;
214 /* We go from back to front to look for the right place to add
215 * this new entry. Drop data we will overwrite on the way */
220 if (bq
->write_index
>= q
->index
+ (int64_t) q
->chunk
.length
)
221 /* We found the entry where we need to place the new entry immediately after */
223 else if (bq
->write_index
+ (int64_t) chunk
.length
<= q
->index
) {
224 /* This entry isn't touched at all, let's skip it */
226 } else if (bq
->write_index
<= q
->index
&&
227 bq
->write_index
+ chunk
.length
>= q
->index
+ q
->chunk
.length
) {
229 /* This entry is fully replaced by the new entry, so let's drop it */
235 } else if (bq
->write_index
>= q
->index
) {
236 /* The write index points into this memblock, so let's
237 * truncate or split it */
239 if (bq
->write_index
+ chunk
.length
< q
->index
+ q
->chunk
.length
) {
241 /* We need to save the end of this memchunk */
245 /* Create a new list entry for the end of thie memchunk */
246 if (!(p
= pa_flist_pop(PA_STATIC_FLIST_GET(list_items
))))
247 p
= pa_xnew(struct list_item
, 1);
250 pa_memblock_ref(p
->chunk
.memblock
);
252 /* Calculate offset */
253 d
= bq
->write_index
+ chunk
.length
- q
->index
;
256 /* Drop it from the new entry */
257 p
->index
= q
->index
+ d
;
258 p
->chunk
.length
-= d
;
260 /* Add it to the list */
262 if ((p
->next
= q
->next
))
271 /* Truncate the chunk */
272 if (!(q
->chunk
.length
= bq
->write_index
- q
->index
)) {
279 /* We had to truncate this block, hence we're now at the right position */
284 pa_assert(bq
->write_index
+ (int64_t)chunk
.length
> q
->index
&&
285 bq
->write_index
+ (int64_t)chunk
.length
< q
->index
+ (int64_t)q
->chunk
.length
&&
286 bq
->write_index
< q
->index
);
288 /* The job overwrites the current entry at the end, so let's drop the beginning of this entry */
290 d
= bq
->write_index
+ chunk
.length
- q
->index
;
293 q
->chunk
.length
-= d
;
300 pa_assert(bq
->write_index
>= q
->index
+ (int64_t)q
->chunk
.length
);
301 pa_assert(!q
->next
|| (bq
->write_index
+ (int64_t)chunk
.length
<= q
->next
->index
));
303 /* Try to merge memory blocks */
305 if (q
->chunk
.memblock
== chunk
.memblock
&&
306 q
->chunk
.index
+ (int64_t)q
->chunk
.length
== chunk
.index
&&
307 bq
->write_index
== q
->index
+ (int64_t)q
->chunk
.length
) {
309 q
->chunk
.length
+= chunk
.length
;
310 bq
->write_index
+= chunk
.length
;
314 pa_assert(!bq
->blocks
|| (bq
->write_index
+ (int64_t)chunk
.length
<= bq
->blocks
->index
));
317 if (!(n
= pa_flist_pop(PA_STATIC_FLIST_GET(list_items
))))
318 n
= pa_xnew(struct list_item
, 1);
321 pa_memblock_ref(n
->chunk
.memblock
);
322 n
->index
= bq
->write_index
;
323 bq
->write_index
+= n
->chunk
.length
;
325 n
->next
= q
? q
->next
: bq
->blocks
;
342 static int memblockq_check_prebuf(pa_memblockq
*bq
) {
347 if (pa_memblockq_get_length(bq
) < bq
->prebuf
)
354 if (bq
->prebuf
> 0 && bq
->read_index
>= bq
->write_index
) {
363 int pa_memblockq_peek(pa_memblockq
* bq
, pa_memchunk
*chunk
) {
367 /* We need to pre-buffer */
368 if (memblockq_check_prebuf(bq
))
371 /* Do we need to spit out silence? */
372 if (!bq
->blocks
|| bq
->blocks
->index
> bq
->read_index
) {
376 /* How much silence shall we return? */
377 length
= bq
->blocks
? bq
->blocks
->index
- bq
->read_index
: 0;
379 /* We need to return silence, since no data is yet available */
381 chunk
->memblock
= pa_memblock_ref(bq
->silence
);
383 if (!length
|| length
> pa_memblock_get_length(chunk
->memblock
))
384 length
= pa_memblock_get_length(chunk
->memblock
);
386 chunk
->length
= length
;
389 /* If the memblockq is empty, return -1, otherwise return
390 * the time to sleep */
394 chunk
->memblock
= NULL
;
395 chunk
->length
= length
;
402 /* Ok, let's pass real data to the caller */
403 pa_assert(bq
->blocks
->index
== bq
->read_index
);
405 *chunk
= bq
->blocks
->chunk
;
406 pa_memblock_ref(chunk
->memblock
);
411 void pa_memblockq_drop(pa_memblockq
*bq
, size_t length
) {
413 pa_assert(length
% bq
->base
== 0);
417 /* Do not drop any data when we are in prebuffering mode */
418 if (memblockq_check_prebuf(bq
))
424 pa_assert(bq
->blocks
->index
>= bq
->read_index
);
426 d
= (size_t) (bq
->blocks
->index
- bq
->read_index
);
429 /* The first block is too far in the future */
431 bq
->read_index
+= length
;
439 pa_assert(bq
->blocks
->index
== bq
->read_index
);
441 if (bq
->blocks
->chunk
.length
<= length
) {
442 /* We need to drop the full block */
444 length
-= bq
->blocks
->chunk
.length
;
445 bq
->read_index
+= bq
->blocks
->chunk
.length
;
447 drop_block(bq
, bq
->blocks
);
449 /* Only the start of this block needs to be dropped */
451 bq
->blocks
->chunk
.index
+= length
;
452 bq
->blocks
->chunk
.length
-= length
;
453 bq
->blocks
->index
+= length
;
454 bq
->read_index
+= length
;
460 /* The list is empty, there's nothing we could drop */
461 bq
->read_index
+= length
;
467 int pa_memblockq_is_readable(pa_memblockq
*bq
) {
470 if (memblockq_check_prebuf(bq
))
473 if (pa_memblockq_get_length(bq
) <= 0)
479 size_t pa_memblockq_get_length(pa_memblockq
*bq
) {
482 if (bq
->write_index
<= bq
->read_index
)
485 return (size_t) (bq
->write_index
- bq
->read_index
);
488 size_t pa_memblockq_missing(pa_memblockq
*bq
) {
492 if ((l
= pa_memblockq_get_length(bq
)) >= bq
->tlength
)
496 return l
>= bq
->minreq
? l
: 0;
499 size_t pa_memblockq_get_minreq(pa_memblockq
*bq
) {
505 void pa_memblockq_seek(pa_memblockq
*bq
, int64_t offset
, pa_seek_mode_t seek
) {
509 case PA_SEEK_RELATIVE
:
510 bq
->write_index
+= offset
;
512 case PA_SEEK_ABSOLUTE
:
513 bq
->write_index
= offset
;
515 case PA_SEEK_RELATIVE_ON_READ
:
516 bq
->write_index
= bq
->read_index
+ offset
;
518 case PA_SEEK_RELATIVE_END
:
519 bq
->write_index
= (bq
->blocks_tail
? bq
->blocks_tail
->index
+ (int64_t) bq
->blocks_tail
->chunk
.length
: bq
->read_index
) + offset
;
523 pa_assert_not_reached();
526 void pa_memblockq_flush(pa_memblockq
*bq
) {
530 drop_block(bq
, bq
->blocks
);
532 pa_assert(bq
->n_blocks
== 0);
534 bq
->write_index
= bq
->read_index
;
536 pa_memblockq_prebuf_force(bq
);
539 size_t pa_memblockq_get_tlength(pa_memblockq
*bq
) {
545 int64_t pa_memblockq_get_read_index(pa_memblockq
*bq
) {
547 return bq
->read_index
;
550 int64_t pa_memblockq_get_write_index(pa_memblockq
*bq
) {
552 return bq
->write_index
;
555 int pa_memblockq_push_align(pa_memblockq
* bq
, const pa_memchunk
*chunk
) {
562 return pa_memblockq_push(bq
, chunk
);
565 bq
->mcalign
= pa_mcalign_new(bq
->base
);
567 if (!can_push(bq
, pa_mcalign_csize(bq
->mcalign
, chunk
->length
)))
570 pa_mcalign_push(bq
->mcalign
, chunk
);
572 while (pa_mcalign_pop(bq
->mcalign
, &rchunk
) >= 0) {
574 r
= pa_memblockq_push(bq
, &rchunk
);
575 pa_memblock_unref(rchunk
.memblock
);
584 void pa_memblockq_shorten(pa_memblockq
*bq
, size_t length
) {
588 l
= pa_memblockq_get_length(bq
);
591 pa_memblockq_drop(bq
, l
- length
);
594 void pa_memblockq_prebuf_disable(pa_memblockq
*bq
) {
600 void pa_memblockq_prebuf_force(pa_memblockq
*bq
) {
603 if (!bq
->in_prebuf
&& bq
->prebuf
> 0)
607 size_t pa_memblockq_get_maxlength(pa_memblockq
*bq
) {
610 return bq
->maxlength
;
613 size_t pa_memblockq_get_prebuf(pa_memblockq
*bq
) {