]>
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 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 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 Lesser 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"
38 struct memblock_list
{
39 struct memblock_list
*next
, *prev
;
40 struct pa_memchunk chunk
;
44 struct memblock_list
*blocks
, *blocks_tail
;
46 size_t current_length
, maxlength
, tlength
, base
, prebuf
, orig_prebuf
, minreq
;
47 struct pa_mcalign
*mcalign
;
48 struct pa_memblock_stat
*memblock_stat
;
51 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
) {
52 struct pa_memblockq
* bq
;
53 assert(maxlength
&& base
&& maxlength
);
55 bq
= pa_xmalloc(sizeof(struct pa_memblockq
));
56 bq
->blocks
= bq
->blocks_tail
= 0;
59 bq
->current_length
= 0;
61 pa_log_debug(__FILE__
": memblockq requested: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", maxlength
, tlength
, base
, prebuf
, minreq
);
65 bq
->maxlength
= ((maxlength
+base
-1)/base
)*base
;
66 assert(bq
->maxlength
>= base
);
68 bq
->tlength
= ((tlength
+base
-1)/base
)*base
;
69 if (!bq
->tlength
|| bq
->tlength
>= bq
->maxlength
)
70 bq
->tlength
= bq
->maxlength
;
72 bq
->minreq
= (minreq
/base
)*base
;
76 bq
->prebuf
= (prebuf
== (size_t) -1) ? bq
->maxlength
/2 : prebuf
;
77 bq
->prebuf
= (bq
->prebuf
/base
)*base
;
78 if (bq
->prebuf
> bq
->maxlength
)
79 bq
->prebuf
= bq
->maxlength
;
81 if (bq
->prebuf
> bq
->tlength
- bq
->minreq
)
82 bq
->prebuf
= bq
->tlength
- bq
->minreq
;
84 bq
->orig_prebuf
= bq
->prebuf
;
86 pa_log_debug(__FILE__
": memblockq sanitized: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", bq
->maxlength
, bq
->tlength
, bq
->base
, bq
->prebuf
, bq
->minreq
);
90 bq
->memblock_stat
= s
;
95 void pa_memblockq_free(struct pa_memblockq
* bq
) {
98 pa_memblockq_flush(bq
);
101 pa_mcalign_free(bq
->mcalign
);
106 void pa_memblockq_push(struct pa_memblockq
* bq
, const struct pa_memchunk
*chunk
, size_t delta
) {
107 struct memblock_list
*q
;
108 assert(bq
&& chunk
&& chunk
->memblock
&& chunk
->length
&& (chunk
->length
% bq
->base
) == 0);
110 pa_memblockq_seek(bq
, delta
);
112 if (bq
->blocks_tail
&& bq
->blocks_tail
->chunk
.memblock
== chunk
->memblock
) {
113 /* Try to merge memory chunks */
115 if (bq
->blocks_tail
->chunk
.index
+bq
->blocks_tail
->chunk
.length
== chunk
->index
) {
116 bq
->blocks_tail
->chunk
.length
+= chunk
->length
;
117 bq
->current_length
+= chunk
->length
;
122 q
= pa_xmalloc(sizeof(struct memblock_list
));
125 pa_memblock_ref(q
->chunk
.memblock
);
126 assert(q
->chunk
.index
+q
->chunk
.length
<= q
->chunk
.memblock
->length
);
128 if ((q
->prev
= bq
->blocks_tail
))
129 bq
->blocks_tail
->next
= q
;
136 bq
->current_length
+= chunk
->length
;
138 pa_memblockq_shorten(bq
, bq
->maxlength
);
141 int pa_memblockq_peek(struct pa_memblockq
* bq
, struct pa_memchunk
*chunk
) {
144 if (!bq
->blocks
|| bq
->current_length
< bq
->prebuf
)
149 *chunk
= bq
->blocks
->chunk
;
150 pa_memblock_ref(chunk
->memblock
);
155 void pa_memblockq_drop(struct pa_memblockq
*bq
, const struct pa_memchunk
*chunk
, size_t length
) {
156 assert(bq
&& chunk
&& length
);
158 if (!bq
->blocks
|| memcmp(&bq
->blocks
->chunk
, chunk
, sizeof(struct pa_memchunk
)))
161 assert(length
<= bq
->blocks
->chunk
.length
);
162 pa_memblockq_skip(bq
, length
);
165 static void remove_block(struct pa_memblockq
*bq
, struct memblock_list
*q
) {
169 q
->prev
->next
= q
->next
;
171 assert(bq
->blocks
== q
);
172 bq
->blocks
= q
->next
;
176 q
->next
->prev
= q
->prev
;
178 assert(bq
->blocks_tail
== q
);
179 bq
->blocks_tail
= q
->prev
;
182 pa_memblock_unref(q
->chunk
.memblock
);
188 void pa_memblockq_skip(struct pa_memblockq
*bq
, size_t length
) {
189 assert(bq
&& length
&& (length
% bq
->base
) == 0);
193 assert(bq
->blocks
&& bq
->current_length
>= length
);
195 if (l
> bq
->blocks
->chunk
.length
)
196 l
= bq
->blocks
->chunk
.length
;
198 bq
->blocks
->chunk
.index
+= l
;
199 bq
->blocks
->chunk
.length
-= l
;
200 bq
->current_length
-= l
;
202 if (!bq
->blocks
->chunk
.length
)
203 remove_block(bq
, bq
->blocks
);
209 void pa_memblockq_shorten(struct pa_memblockq
*bq
, size_t length
) {
213 if (bq
->current_length
<= length
)
216 /*pa_log(__FILE__": Warning! pa_memblockq_shorten()\n");*/
218 l
= bq
->current_length
- length
;
222 pa_memblockq_skip(bq
, l
);
226 void pa_memblockq_empty(struct pa_memblockq
*bq
) {
228 pa_memblockq_shorten(bq
, 0);
231 int pa_memblockq_is_readable(struct pa_memblockq
*bq
) {
234 return bq
->current_length
&& (bq
->current_length
>= bq
->prebuf
);
237 int pa_memblockq_is_writable(struct pa_memblockq
*bq
, size_t length
) {
240 return bq
->current_length
+ length
<= bq
->tlength
;
243 uint32_t pa_memblockq_get_length(struct pa_memblockq
*bq
) {
245 return bq
->current_length
;
248 uint32_t pa_memblockq_missing(struct pa_memblockq
*bq
) {
252 if (bq
->current_length
>= bq
->tlength
)
255 l
= bq
->tlength
- bq
->current_length
;
258 return (l
>= bq
->minreq
) ? l
: 0;
261 void pa_memblockq_push_align(struct pa_memblockq
* bq
, const struct pa_memchunk
*chunk
, size_t delta
) {
262 struct pa_memchunk rchunk
;
263 assert(bq
&& chunk
&& bq
->base
);
266 pa_memblockq_push(bq
, chunk
, delta
);
271 bq
->mcalign
= pa_mcalign_new(bq
->base
, bq
->memblock_stat
);
275 pa_mcalign_push(bq
->mcalign
, chunk
);
277 while (pa_mcalign_pop(bq
->mcalign
, &rchunk
) >= 0) {
278 pa_memblockq_push(bq
, &rchunk
, delta
);
279 pa_memblock_unref(rchunk
.memblock
);
284 uint32_t pa_memblockq_get_minreq(struct pa_memblockq
*bq
) {
289 void pa_memblockq_prebuf_disable(struct pa_memblockq
*bq
) {
294 void pa_memblockq_prebuf_reenable(struct pa_memblockq
*bq
) {
296 bq
->prebuf
= bq
->orig_prebuf
;
299 void pa_memblockq_seek(struct pa_memblockq
*bq
, size_t length
) {
305 while (length
>= bq
->base
) {
307 if (!bq
->current_length
)
310 assert(bq
->blocks_tail
);
312 if (l
> bq
->blocks_tail
->chunk
.length
)
313 l
= bq
->blocks_tail
->chunk
.length
;
315 bq
->blocks_tail
->chunk
.length
-= l
;
316 bq
->current_length
-= l
;
318 if (bq
->blocks_tail
->chunk
.length
== 0)
319 remove_block(bq
, bq
->blocks
);
325 void pa_memblockq_flush(struct pa_memblockq
*bq
) {
326 struct memblock_list
*l
;
329 while ((l
= bq
->blocks
)) {
330 bq
->blocks
= l
->next
;
331 pa_memblock_unref(l
->chunk
.memblock
);
335 bq
->blocks_tail
= NULL
;
337 bq
->current_length
= 0;
340 uint32_t pa_memblockq_get_tlength(struct pa_memblockq
*bq
) {