]>
code.delx.au - pulseaudio/blob - polyp/memblockq.c
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include "memblockq.h"
37 struct memblock_list
{
38 struct memblock_list
*next
, *prev
;
39 struct pa_memchunk chunk
;
43 struct memblock_list
*blocks
, *blocks_tail
;
45 size_t current_length
, maxlength
, tlength
, base
, prebuf
, minreq
;
46 struct pa_mcalign
*mcalign
;
47 struct pa_memblock_stat
*memblock_stat
;
50 struct pa_memblockq
* pa_memblockq_new(size_t maxlength
, size_t tlength
, size_t base
, size_t prebuf
, size_t minreq
, struct pa_memblock_stat
*s
) {
51 struct pa_memblockq
* bq
;
52 assert(maxlength
&& base
&& maxlength
);
54 bq
= pa_xmalloc(sizeof(struct pa_memblockq
));
55 bq
->blocks
= bq
->blocks_tail
= 0;
58 bq
->current_length
= 0;
60 /*pa_log(__FILE__": memblockq requested: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", maxlength, tlength, base, prebuf, minreq);*/
64 bq
->maxlength
= ((maxlength
+base
-1)/base
)*base
;
65 assert(bq
->maxlength
>= base
);
67 bq
->tlength
= ((tlength
+base
-1)/base
)*base
;
68 if (!bq
->tlength
|| bq
->tlength
>= bq
->maxlength
)
69 bq
->tlength
= bq
->maxlength
;
71 bq
->prebuf
= (prebuf
== (size_t) -1) ? bq
->maxlength
/2 : prebuf
;
72 bq
->prebuf
= (bq
->prebuf
/base
)*base
;
73 if (bq
->prebuf
> bq
->maxlength
)
74 bq
->prebuf
= bq
->maxlength
;
76 bq
->minreq
= (minreq
/base
)*base
;
80 pa_log(__FILE__
": memblockq sanitized: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", bq
->maxlength
, bq
->tlength
, bq
->base
, bq
->prebuf
, bq
->minreq
);
84 bq
->memblock_stat
= s
;
89 void pa_memblockq_free(struct pa_memblockq
* bq
) {
92 pa_memblockq_flush(bq
);
95 pa_mcalign_free(bq
->mcalign
);
100 void pa_memblockq_push(struct pa_memblockq
* bq
, const struct pa_memchunk
*chunk
, size_t delta
) {
101 struct memblock_list
*q
;
102 assert(bq
&& chunk
&& chunk
->memblock
&& chunk
->length
&& (chunk
->length
% bq
->base
) == 0);
104 pa_memblockq_seek(bq
, delta
);
106 if (bq
->blocks_tail
&& bq
->blocks_tail
->chunk
.memblock
== chunk
->memblock
) {
107 /* Try to merge memory chunks */
109 if (bq
->blocks_tail
->chunk
.index
+bq
->blocks_tail
->chunk
.length
== chunk
->index
) {
110 bq
->blocks_tail
->chunk
.length
+= chunk
->length
;
111 bq
->current_length
+= chunk
->length
;
116 q
= pa_xmalloc(sizeof(struct memblock_list
));
119 pa_memblock_ref(q
->chunk
.memblock
);
120 assert(q
->chunk
.index
+q
->chunk
.length
<= q
->chunk
.memblock
->length
);
122 if ((q
->prev
= bq
->blocks_tail
))
123 bq
->blocks_tail
->next
= q
;
130 bq
->current_length
+= chunk
->length
;
132 pa_memblockq_shorten(bq
, bq
->maxlength
);
135 int pa_memblockq_peek(struct pa_memblockq
* bq
, struct pa_memchunk
*chunk
) {
138 if (!bq
->blocks
|| bq
->current_length
< bq
->prebuf
)
143 *chunk
= bq
->blocks
->chunk
;
144 pa_memblock_ref(chunk
->memblock
);
149 void pa_memblockq_drop(struct pa_memblockq
*bq
, const struct pa_memchunk
*chunk
, size_t length
) {
150 assert(bq
&& chunk
&& length
);
152 if (!bq
->blocks
|| memcmp(&bq
->blocks
->chunk
, chunk
, sizeof(struct pa_memchunk
)))
155 assert(length
<= bq
->blocks
->chunk
.length
);
156 pa_memblockq_skip(bq
, length
);
159 static void remove_block(struct pa_memblockq
*bq
, struct memblock_list
*q
) {
163 q
->prev
->next
= q
->next
;
165 assert(bq
->blocks
== q
);
166 bq
->blocks
= q
->next
;
170 q
->next
->prev
= q
->prev
;
172 assert(bq
->blocks_tail
== q
);
173 bq
->blocks_tail
= q
->prev
;
176 pa_memblock_unref(q
->chunk
.memblock
);
182 void pa_memblockq_skip(struct pa_memblockq
*bq
, size_t length
) {
183 assert(bq
&& length
&& (length
% bq
->base
) == 0);
187 assert(bq
->blocks
&& bq
->current_length
>= length
);
189 if (l
> bq
->blocks
->chunk
.length
)
190 l
= bq
->blocks
->chunk
.length
;
192 bq
->blocks
->chunk
.index
+= l
;
193 bq
->blocks
->chunk
.length
-= l
;
194 bq
->current_length
-= l
;
196 if (!bq
->blocks
->chunk
.length
)
197 remove_block(bq
, bq
->blocks
);
203 void pa_memblockq_shorten(struct pa_memblockq
*bq
, size_t length
) {
207 if (bq
->current_length
<= length
)
210 /*pa_log(__FILE__": Warning! pa_memblockq_shorten()\n");*/
212 l
= bq
->current_length
- length
;
216 pa_memblockq_skip(bq
, l
);
220 void pa_memblockq_empty(struct pa_memblockq
*bq
) {
222 pa_memblockq_shorten(bq
, 0);
225 int pa_memblockq_is_readable(struct pa_memblockq
*bq
) {
228 return bq
->current_length
&& (bq
->current_length
>= bq
->prebuf
);
231 int pa_memblockq_is_writable(struct pa_memblockq
*bq
, size_t length
) {
234 return bq
->current_length
+ length
<= bq
->tlength
;
237 uint32_t pa_memblockq_get_length(struct pa_memblockq
*bq
) {
239 return bq
->current_length
;
242 uint32_t pa_memblockq_missing(struct pa_memblockq
*bq
) {
246 if (bq
->current_length
>= bq
->tlength
)
249 l
= bq
->tlength
- bq
->current_length
;
252 return (l
>= bq
->minreq
) ? l
: 0;
255 void pa_memblockq_push_align(struct pa_memblockq
* bq
, const struct pa_memchunk
*chunk
, size_t delta
) {
256 struct pa_memchunk rchunk
;
257 assert(bq
&& chunk
&& bq
->base
);
260 pa_memblockq_push(bq
, chunk
, delta
);
265 bq
->mcalign
= pa_mcalign_new(bq
->base
, bq
->memblock_stat
);
269 pa_mcalign_push(bq
->mcalign
, chunk
);
271 while (pa_mcalign_pop(bq
->mcalign
, &rchunk
) >= 0) {
272 pa_memblockq_push(bq
, &rchunk
, delta
);
273 pa_memblock_unref(rchunk
.memblock
);
278 uint32_t pa_memblockq_get_minreq(struct pa_memblockq
*bq
) {
283 void pa_memblockq_prebuf_disable(struct pa_memblockq
*bq
) {
288 void pa_memblockq_seek(struct pa_memblockq
*bq
, size_t length
) {
294 while (length
>= bq
->base
) {
296 if (!bq
->current_length
)
299 assert(bq
->blocks_tail
);
301 if (l
> bq
->blocks_tail
->chunk
.length
)
302 l
= bq
->blocks_tail
->chunk
.length
;
304 bq
->blocks_tail
->chunk
.length
-= l
;
305 bq
->current_length
-= l
;
307 if (bq
->blocks_tail
->chunk
.length
== 0)
308 remove_block(bq
, bq
->blocks
);
314 void pa_memblockq_flush(struct pa_memblockq
*bq
) {
315 struct memblock_list
*l
;
318 while ((l
= bq
->blocks
)) {
319 bq
->blocks
= l
->next
;
320 pa_memblock_unref(l
->chunk
.memblock
);
324 bq
->blocks_tail
= NULL
;
326 bq
->current_length
= 0;
329 uint32_t pa_memblockq_get_tlength(struct pa_memblockq
*bq
) {