]>
code.delx.au - pulseaudio/blob - src/pulsecore/memblockq.c
4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/mcalign.h>
38 #include "memblockq.h"
40 struct memblock_list
{
41 struct memblock_list
*next
, *prev
;
47 struct memblock_list
*blocks
, *blocks_tail
;
49 size_t maxlength
, tlength
, base
, prebuf
, minreq
;
50 int64_t read_index
, write_index
;
51 enum { PREBUF
, RUNNING
} state
;
56 pa_memblockq
* pa_memblockq_new(
63 pa_memblock
*silence
) {
68 assert(maxlength
>= base
);
70 bq
= pa_xnew(pa_memblockq
, 1);
71 bq
->blocks
= bq
->blocks_tail
= NULL
;
75 bq
->read_index
= bq
->write_index
= idx
;
77 pa_log_debug(__FILE__
": memblockq requested: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
78 (unsigned long)maxlength
, (unsigned long)tlength
, (unsigned long)base
, (unsigned long)prebuf
, (unsigned long)minreq
);
80 bq
->maxlength
= ((maxlength
+base
-1)/base
)*base
;
81 assert(bq
->maxlength
>= base
);
83 bq
->tlength
= ((tlength
+base
-1)/base
)*base
;
84 if (!bq
->tlength
|| bq
->tlength
>= bq
->maxlength
)
85 bq
->tlength
= bq
->maxlength
;
87 bq
->prebuf
= (prebuf
== (size_t) -1) ? bq
->tlength
/2 : prebuf
;
88 bq
->prebuf
= ((bq
->prebuf
+base
-1)/base
)*base
;
89 if (bq
->prebuf
> bq
->maxlength
)
90 bq
->prebuf
= bq
->maxlength
;
92 bq
->minreq
= (minreq
/base
)*base
;
94 if (bq
->minreq
> bq
->tlength
- bq
->prebuf
)
95 bq
->minreq
= bq
->tlength
- bq
->prebuf
;
100 pa_log_debug(__FILE__
": memblockq sanitized: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu",
101 (unsigned long)bq
->maxlength
, (unsigned long)bq
->tlength
, (unsigned long)bq
->base
, (unsigned long)bq
->prebuf
, (unsigned long)bq
->minreq
);
103 bq
->state
= bq
->prebuf
? PREBUF
: RUNNING
;
104 bq
->silence
= silence
? pa_memblock_ref(silence
) : NULL
;
110 void pa_memblockq_free(pa_memblockq
* bq
) {
113 pa_memblockq_flush(bq
);
116 pa_memblock_unref(bq
->silence
);
119 pa_mcalign_free(bq
->mcalign
);
124 static void drop_block(pa_memblockq
*bq
, struct memblock_list
*q
) {
128 assert(bq
->n_blocks
>= 1);
131 q
->prev
->next
= q
->next
;
133 bq
->blocks
= q
->next
;
136 q
->next
->prev
= q
->prev
;
138 bq
->blocks_tail
= q
->prev
;
140 pa_memblock_unref(q
->chunk
.memblock
);
146 static int can_push(pa_memblockq
*bq
, size_t l
) {
151 if (bq
->read_index
> bq
->write_index
) {
152 size_t d
= bq
->read_index
- bq
->write_index
;
160 end
= bq
->blocks_tail
? bq
->blocks_tail
->index
+ bq
->blocks_tail
->chunk
.length
: 0;
162 /* Make sure that the list doesn't get too long */
163 if (bq
->write_index
+ (int64_t)l
> end
)
164 if (bq
->write_index
+ l
- bq
->read_index
> bq
->maxlength
)
170 int pa_memblockq_push(pa_memblockq
* bq
, const pa_memchunk
*uchunk
) {
172 struct memblock_list
*q
, *n
;
177 assert(uchunk
->memblock
);
178 assert(uchunk
->length
> 0);
179 assert(uchunk
->index
+ uchunk
->length
<= uchunk
->memblock
->length
);
181 if (uchunk
->length
% bq
->base
)
184 if (!can_push(bq
, uchunk
->length
))
189 if (bq
->read_index
> bq
->write_index
) {
191 /* We currently have a buffer underflow, we need to drop some
194 size_t d
= bq
->read_index
- bq
->write_index
;
196 if (chunk
.length
> d
) {
199 bq
->write_index
= bq
->read_index
;
201 /* We drop the incoming data completely */
202 bq
->write_index
+= chunk
.length
;
207 /* We go from back to front to look for the right place to add
208 * this new entry. Drop data we will overwrite on the way */
213 if (bq
->write_index
>= q
->index
+ (int64_t)q
->chunk
.length
)
214 /* We found the entry where we need to place the new entry immediately after */
216 else if (bq
->write_index
+ (int64_t)chunk
.length
<= q
->index
) {
217 /* This entry isn't touched at all, let's skip it */
219 } else if (bq
->write_index
<= q
->index
&&
220 bq
->write_index
+ chunk
.length
>= q
->index
+ q
->chunk
.length
) {
222 /* This entry is fully replaced by the new entry, so let's drop it */
224 struct memblock_list
*p
;
228 } else if (bq
->write_index
>= q
->index
) {
229 /* The write index points into this memblock, so let's
230 * truncate or split it */
232 if (bq
->write_index
+ chunk
.length
< q
->index
+ q
->chunk
.length
) {
234 /* We need to save the end of this memchunk */
235 struct memblock_list
*p
;
238 /* Create a new list entry for the end of thie memchunk */
239 p
= pa_xnew(struct memblock_list
, 1);
241 pa_memblock_ref(p
->chunk
.memblock
);
243 /* Calculate offset */
244 d
= bq
->write_index
+ chunk
.length
- q
->index
;
247 /* Drop it from the new entry */
248 p
->index
= q
->index
+ d
;
249 p
->chunk
.length
-= d
;
251 /* Add it to the list */
253 if ((p
->next
= q
->next
))
262 /* Truncate the chunk */
263 if (!(q
->chunk
.length
= bq
->write_index
- q
->index
)) {
264 struct memblock_list
*p
;
270 /* We had to truncate this block, hence we're now at the right position */
275 assert(bq
->write_index
+ (int64_t)chunk
.length
> q
->index
&&
276 bq
->write_index
+ (int64_t)chunk
.length
< q
->index
+ (int64_t)q
->chunk
.length
&&
277 bq
->write_index
< q
->index
);
279 /* The job overwrites the current entry at the end, so let's drop the beginning of this entry */
281 d
= bq
->write_index
+ chunk
.length
- q
->index
;
284 q
->chunk
.length
-= d
;
292 assert(bq
->write_index
>= q
->index
+ (int64_t)q
->chunk
.length
);
293 assert(!q
->next
|| (bq
->write_index
+ (int64_t)chunk
.length
<= q
->next
->index
));
295 /* Try to merge memory blocks */
297 if (q
->chunk
.memblock
== chunk
.memblock
&&
298 q
->chunk
.index
+ (int64_t)q
->chunk
.length
== chunk
.index
&&
299 bq
->write_index
== q
->index
+ (int64_t)q
->chunk
.length
) {
301 q
->chunk
.length
+= chunk
.length
;
302 bq
->write_index
+= chunk
.length
;
306 assert(!bq
->blocks
|| (bq
->write_index
+ (int64_t)chunk
.length
<= bq
->blocks
->index
));
309 n
= pa_xnew(struct memblock_list
, 1);
311 pa_memblock_ref(n
->chunk
.memblock
);
312 n
->index
= bq
->write_index
;
313 bq
->write_index
+= n
->chunk
.length
;
315 n
->next
= q
? q
->next
: bq
->blocks
;
332 int pa_memblockq_peek(pa_memblockq
* bq
, pa_memchunk
*chunk
) {
336 if (bq
->state
== PREBUF
) {
338 /* We need to pre-buffer */
339 if (pa_memblockq_get_length(bq
) < bq
->prebuf
)
344 } else if (bq
->prebuf
> 0 && bq
->read_index
>= bq
->write_index
) {
346 /* Buffer underflow protection */
351 /* Do we need to spit out silence? */
352 if (!bq
->blocks
|| bq
->blocks
->index
> bq
->read_index
) {
356 /* How much silence shall we return? */
357 length
= bq
->blocks
? bq
->blocks
->index
- bq
->read_index
: 0;
359 /* We need to return silence, since no data is yet available */
361 chunk
->memblock
= pa_memblock_ref(bq
->silence
);
363 if (!length
|| length
> chunk
->memblock
->length
)
364 length
= chunk
->memblock
->length
;
366 chunk
->length
= length
;
369 /* If the memblockq is empty, return -1, otherwise return
370 * the time to sleep */
374 chunk
->memblock
= NULL
;
375 chunk
->length
= length
;
382 /* Ok, let's pass real data to the caller */
383 assert(bq
->blocks
->index
== bq
->read_index
);
385 *chunk
= bq
->blocks
->chunk
;
386 pa_memblock_ref(chunk
->memblock
);
391 void pa_memblockq_drop(pa_memblockq
*bq
, const pa_memchunk
*chunk
, size_t length
) {
393 assert(length
% bq
->base
== 0);
395 assert(!chunk
|| length
<= chunk
->length
);
399 if (bq
->blocks
&& bq
->blocks
->index
== bq
->read_index
) {
400 /* The first item in queue is valid */
402 /* Does the chunk match with what the user supplied us? */
403 if (memcmp(chunk
, &bq
->blocks
->chunk
, sizeof(pa_memchunk
)) != 0)
409 /* The first item in the queue is not yet relevant */
411 assert(!bq
->blocks
|| bq
->blocks
->index
> bq
->read_index
);
412 l
= bq
->blocks
? bq
->blocks
->index
- bq
->read_index
: 0;
416 if (!l
|| l
> bq
->silence
->length
)
417 l
= bq
->silence
->length
;
421 /* Do the entries still match? */
422 if (chunk
->index
!= 0 || chunk
->length
!= l
|| chunk
->memblock
!= bq
->silence
)
432 assert(bq
->blocks
->index
>= bq
->read_index
);
434 d
= (size_t) (bq
->blocks
->index
- bq
->read_index
);
437 /* The first block is too far in the future */
439 bq
->read_index
+= length
;
447 assert(bq
->blocks
->index
== bq
->read_index
);
449 if (bq
->blocks
->chunk
.length
<= length
) {
450 /* We need to drop the full block */
452 length
-= bq
->blocks
->chunk
.length
;
453 bq
->read_index
+= bq
->blocks
->chunk
.length
;
455 drop_block(bq
, bq
->blocks
);
457 /* Only the start of this block needs to be dropped */
459 bq
->blocks
->chunk
.index
+= length
;
460 bq
->blocks
->chunk
.length
-= length
;
461 bq
->blocks
->index
+= length
;
462 bq
->read_index
+= length
;
468 /* The list is empty, there's nothing we could drop */
469 bq
->read_index
+= length
;
475 int pa_memblockq_is_readable(pa_memblockq
*bq
) {
478 if (bq
->prebuf
> 0) {
479 size_t l
= pa_memblockq_get_length(bq
);
481 if (bq
->state
== PREBUF
&& l
< bq
->prebuf
)
491 int pa_memblockq_is_writable(pa_memblockq
*bq
, size_t length
) {
494 if (length
% bq
->base
)
497 return pa_memblockq_get_length(bq
) + length
<= bq
->tlength
;
500 size_t pa_memblockq_get_length(pa_memblockq
*bq
) {
503 if (bq
->write_index
<= bq
->read_index
)
506 return (size_t) (bq
->write_index
- bq
->read_index
);
509 size_t pa_memblockq_missing(pa_memblockq
*bq
) {
513 if ((l
= pa_memblockq_get_length(bq
)) >= bq
->tlength
)
517 return (l
>= bq
->minreq
) ? l
: 0;
520 size_t pa_memblockq_get_minreq(pa_memblockq
*bq
) {
526 void pa_memblockq_seek(pa_memblockq
*bq
, int64_t offset
, pa_seek_mode_t seek
) {
530 case PA_SEEK_RELATIVE
:
531 bq
->write_index
+= offset
;
533 case PA_SEEK_ABSOLUTE
:
534 bq
->write_index
= offset
;
536 case PA_SEEK_RELATIVE_ON_READ
:
537 bq
->write_index
= bq
->read_index
+ offset
;
539 case PA_SEEK_RELATIVE_END
:
540 bq
->write_index
= (bq
->blocks_tail
? bq
->blocks_tail
->index
+ (int64_t)bq
->blocks_tail
->chunk
.length
: bq
->read_index
) + offset
;
547 void pa_memblockq_flush(pa_memblockq
*bq
) {
551 drop_block(bq
, bq
->blocks
);
553 assert(bq
->n_blocks
== 0);
555 bq
->write_index
= bq
->read_index
;
557 pa_memblockq_prebuf_force(bq
);
560 size_t pa_memblockq_get_tlength(pa_memblockq
*bq
) {
566 int64_t pa_memblockq_get_read_index(pa_memblockq
*bq
) {
568 return bq
->read_index
;
571 int64_t pa_memblockq_get_write_index(pa_memblockq
*bq
) {
573 return bq
->write_index
;
576 int pa_memblockq_push_align(pa_memblockq
* bq
, const pa_memchunk
*chunk
) {
580 assert(chunk
&& bq
->base
);
583 return pa_memblockq_push(bq
, chunk
);
586 bq
->mcalign
= pa_mcalign_new(bq
->base
);
588 if (!can_push(bq
, pa_mcalign_csize(bq
->mcalign
, chunk
->length
)))
591 pa_mcalign_push(bq
->mcalign
, chunk
);
593 while (pa_mcalign_pop(bq
->mcalign
, &rchunk
) >= 0) {
595 r
= pa_memblockq_push(bq
, &rchunk
);
596 pa_memblock_unref(rchunk
.memblock
);
605 void pa_memblockq_shorten(pa_memblockq
*bq
, size_t length
) {
609 l
= pa_memblockq_get_length(bq
);
612 pa_memblockq_drop(bq
, NULL
, l
- length
);
615 void pa_memblockq_prebuf_disable(pa_memblockq
*bq
) {
618 if (bq
->state
== PREBUF
)
622 void pa_memblockq_prebuf_force(pa_memblockq
*bq
) {
625 if (bq
->state
== RUNNING
&& bq
->prebuf
> 0)
629 size_t pa_memblockq_get_maxlength(pa_memblockq
*bq
) {
632 return bq
->maxlength
;
635 size_t pa_memblockq_get_prebuf(pa_memblockq
*bq
) {