]>
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"
36 struct memblock_list
{
37 struct memblock_list
*next
, *prev
;
38 struct pa_memchunk chunk
;
42 struct memblock_list
*blocks
, *blocks_tail
;
44 size_t current_length
, maxlength
, tlength
, base
, prebuf
, minreq
;
45 struct pa_mcalign
*mcalign
;
46 struct pa_memblock_stat
*memblock_stat
;
49 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
) {
50 struct pa_memblockq
* bq
;
51 assert(maxlength
&& base
&& maxlength
);
53 bq
= pa_xmalloc(sizeof(struct pa_memblockq
));
54 bq
->blocks
= bq
->blocks_tail
= 0;
57 bq
->current_length
= 0;
59 /*fprintf(stderr, "memblockq requested: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", maxlength, tlength, base, prebuf, minreq);*/
63 bq
->maxlength
= ((maxlength
+base
-1)/base
)*base
;
64 assert(bq
->maxlength
>= base
);
66 bq
->tlength
= ((tlength
+base
-1)/base
)*base
;
67 if (!bq
->tlength
|| bq
->tlength
>= bq
->maxlength
)
68 bq
->tlength
= bq
->maxlength
;
70 bq
->prebuf
= (prebuf
== (size_t) -1) ? bq
->maxlength
/2 : prebuf
;
71 bq
->prebuf
= (bq
->prebuf
/base
)*base
;
72 if (bq
->prebuf
> bq
->maxlength
)
73 bq
->prebuf
= bq
->maxlength
;
75 bq
->minreq
= (minreq
/base
)*base
;
79 fprintf(stderr
, "memblockq sanitized: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", bq
->maxlength
, bq
->tlength
, bq
->base
, bq
->prebuf
, bq
->minreq
);
83 bq
->memblock_stat
= s
;
88 void pa_memblockq_free(struct pa_memblockq
* bq
) {
91 pa_memblockq_flush(bq
);
94 pa_mcalign_free(bq
->mcalign
);
99 void pa_memblockq_push(struct pa_memblockq
* bq
, const struct pa_memchunk
*chunk
, size_t delta
) {
100 struct memblock_list
*q
;
101 assert(bq
&& chunk
&& chunk
->memblock
&& chunk
->length
&& (chunk
->length
% bq
->base
) == 0);
103 pa_memblockq_seek(bq
, delta
);
105 if (bq
->blocks_tail
&& bq
->blocks_tail
->chunk
.memblock
== chunk
->memblock
) {
106 /* Try to merge memory chunks */
108 if (bq
->blocks_tail
->chunk
.index
+bq
->blocks_tail
->chunk
.length
== chunk
->index
) {
109 bq
->blocks_tail
->chunk
.length
+= chunk
->length
;
110 bq
->current_length
+= chunk
->length
;
115 q
= pa_xmalloc(sizeof(struct memblock_list
));
118 pa_memblock_ref(q
->chunk
.memblock
);
119 assert(q
->chunk
.index
+q
->chunk
.length
<= q
->chunk
.memblock
->length
);
121 if ((q
->prev
= bq
->blocks_tail
))
122 bq
->blocks_tail
->next
= q
;
129 bq
->current_length
+= chunk
->length
;
131 pa_memblockq_shorten(bq
, bq
->maxlength
);
134 int pa_memblockq_peek(struct pa_memblockq
* bq
, struct pa_memchunk
*chunk
) {
137 if (!bq
->blocks
|| bq
->current_length
< bq
->prebuf
)
142 *chunk
= bq
->blocks
->chunk
;
143 pa_memblock_ref(chunk
->memblock
);
148 void pa_memblockq_drop(struct pa_memblockq
*bq
, const struct pa_memchunk
*chunk
, size_t length
) {
149 assert(bq
&& chunk
&& length
);
151 if (!bq
->blocks
|| memcmp(&bq
->blocks
->chunk
, chunk
, sizeof(struct pa_memchunk
)))
154 assert(length
<= bq
->blocks
->chunk
.length
);
155 pa_memblockq_skip(bq
, length
);
158 static void remove_block(struct pa_memblockq
*bq
, struct memblock_list
*q
) {
162 q
->prev
->next
= q
->next
;
164 assert(bq
->blocks
== q
);
165 bq
->blocks
= q
->next
;
169 q
->next
->prev
= q
->prev
;
171 assert(bq
->blocks_tail
== q
);
172 bq
->blocks_tail
= q
->prev
;
175 pa_memblock_unref(q
->chunk
.memblock
);
181 void pa_memblockq_skip(struct pa_memblockq
*bq
, size_t length
) {
182 assert(bq
&& length
&& (length
% bq
->base
) == 0);
186 assert(bq
->blocks
&& bq
->current_length
>= length
);
188 if (l
> bq
->blocks
->chunk
.length
)
189 l
= bq
->blocks
->chunk
.length
;
191 bq
->blocks
->chunk
.index
+= l
;
192 bq
->blocks
->chunk
.length
-= l
;
193 bq
->current_length
-= l
;
195 if (!bq
->blocks
->chunk
.length
)
196 remove_block(bq
, bq
->blocks
);
202 void pa_memblockq_shorten(struct pa_memblockq
*bq
, size_t length
) {
206 if (bq
->current_length
<= length
)
209 fprintf(stderr
, "Warning! pa_memblockq_shorten()\n");
211 l
= bq
->current_length
- length
;
215 pa_memblockq_skip(bq
, l
);
219 void pa_memblockq_empty(struct pa_memblockq
*bq
) {
221 pa_memblockq_shorten(bq
, 0);
224 int pa_memblockq_is_readable(struct pa_memblockq
*bq
) {
227 return bq
->current_length
&& (bq
->current_length
>= bq
->prebuf
);
230 int pa_memblockq_is_writable(struct pa_memblockq
*bq
, size_t length
) {
233 return bq
->current_length
+ length
<= bq
->tlength
;
236 uint32_t pa_memblockq_get_length(struct pa_memblockq
*bq
) {
238 return bq
->current_length
;
241 uint32_t pa_memblockq_missing(struct pa_memblockq
*bq
) {
245 if (bq
->current_length
>= bq
->tlength
)
248 l
= bq
->tlength
- bq
->current_length
;
251 return (l
>= bq
->minreq
) ? l
: 0;
254 void pa_memblockq_push_align(struct pa_memblockq
* bq
, const struct pa_memchunk
*chunk
, size_t delta
) {
255 struct pa_memchunk rchunk
;
256 assert(bq
&& chunk
&& bq
->base
);
259 pa_memblockq_push(bq
, chunk
, delta
);
264 bq
->mcalign
= pa_mcalign_new(bq
->base
, bq
->memblock_stat
);
268 pa_mcalign_push(bq
->mcalign
, chunk
);
270 while (pa_mcalign_pop(bq
->mcalign
, &rchunk
) >= 0) {
271 pa_memblockq_push(bq
, &rchunk
, delta
);
272 pa_memblock_unref(rchunk
.memblock
);
277 uint32_t pa_memblockq_get_minreq(struct pa_memblockq
*bq
) {
282 void pa_memblockq_prebuf_disable(struct pa_memblockq
*bq
) {
287 void pa_memblockq_seek(struct pa_memblockq
*bq
, size_t length
) {
293 while (length
>= bq
->base
) {
295 if (!bq
->current_length
)
298 assert(bq
->blocks_tail
);
300 if (l
> bq
->blocks_tail
->chunk
.length
)
301 l
= bq
->blocks_tail
->chunk
.length
;
303 bq
->blocks_tail
->chunk
.length
-= l
;
304 bq
->current_length
-= l
;
306 if (bq
->blocks_tail
->chunk
.length
== 0)
307 remove_block(bq
, bq
->blocks
);
313 void pa_memblockq_flush(struct pa_memblockq
*bq
) {
314 struct memblock_list
*l
;
317 while ((l
= bq
->blocks
)) {
318 bq
->blocks
= l
->next
;
319 pa_memblock_unref(l
->chunk
.memblock
);
323 bq
->blocks_tail
= NULL
;
325 bq
->current_length
= 0;
328 uint32_t pa_memblockq_get_tlength(struct pa_memblockq
*bq
) {