]>
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"
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
, orig_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
->orig_prebuf
= bq
->prebuf
;
78 bq
->minreq
= (minreq
/base
)*base
;
82 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
);
86 bq
->memblock_stat
= s
;
91 void pa_memblockq_free(struct pa_memblockq
* bq
) {
94 pa_memblockq_flush(bq
);
97 pa_mcalign_free(bq
->mcalign
);
102 void pa_memblockq_push(struct pa_memblockq
* bq
, const struct pa_memchunk
*chunk
, size_t delta
) {
103 struct memblock_list
*q
;
104 assert(bq
&& chunk
&& chunk
->memblock
&& chunk
->length
&& (chunk
->length
% bq
->base
) == 0);
106 pa_memblockq_seek(bq
, delta
);
108 if (bq
->blocks_tail
&& bq
->blocks_tail
->chunk
.memblock
== chunk
->memblock
) {
109 /* Try to merge memory chunks */
111 if (bq
->blocks_tail
->chunk
.index
+bq
->blocks_tail
->chunk
.length
== chunk
->index
) {
112 bq
->blocks_tail
->chunk
.length
+= chunk
->length
;
113 bq
->current_length
+= chunk
->length
;
118 q
= pa_xmalloc(sizeof(struct memblock_list
));
121 pa_memblock_ref(q
->chunk
.memblock
);
122 assert(q
->chunk
.index
+q
->chunk
.length
<= q
->chunk
.memblock
->length
);
124 if ((q
->prev
= bq
->blocks_tail
))
125 bq
->blocks_tail
->next
= q
;
132 bq
->current_length
+= chunk
->length
;
134 pa_memblockq_shorten(bq
, bq
->maxlength
);
137 int pa_memblockq_peek(struct pa_memblockq
* bq
, struct pa_memchunk
*chunk
) {
140 if (!bq
->blocks
|| bq
->current_length
< bq
->prebuf
)
145 *chunk
= bq
->blocks
->chunk
;
146 pa_memblock_ref(chunk
->memblock
);
151 void pa_memblockq_drop(struct pa_memblockq
*bq
, const struct pa_memchunk
*chunk
, size_t length
) {
152 assert(bq
&& chunk
&& length
);
154 if (!bq
->blocks
|| memcmp(&bq
->blocks
->chunk
, chunk
, sizeof(struct pa_memchunk
)))
157 assert(length
<= bq
->blocks
->chunk
.length
);
158 pa_memblockq_skip(bq
, length
);
161 static void remove_block(struct pa_memblockq
*bq
, struct memblock_list
*q
) {
165 q
->prev
->next
= q
->next
;
167 assert(bq
->blocks
== q
);
168 bq
->blocks
= q
->next
;
172 q
->next
->prev
= q
->prev
;
174 assert(bq
->blocks_tail
== q
);
175 bq
->blocks_tail
= q
->prev
;
178 pa_memblock_unref(q
->chunk
.memblock
);
184 void pa_memblockq_skip(struct pa_memblockq
*bq
, size_t length
) {
185 assert(bq
&& length
&& (length
% bq
->base
) == 0);
189 assert(bq
->blocks
&& bq
->current_length
>= length
);
191 if (l
> bq
->blocks
->chunk
.length
)
192 l
= bq
->blocks
->chunk
.length
;
194 bq
->blocks
->chunk
.index
+= l
;
195 bq
->blocks
->chunk
.length
-= l
;
196 bq
->current_length
-= l
;
198 if (!bq
->blocks
->chunk
.length
)
199 remove_block(bq
, bq
->blocks
);
205 void pa_memblockq_shorten(struct pa_memblockq
*bq
, size_t length
) {
209 if (bq
->current_length
<= length
)
212 /*pa_log(__FILE__": Warning! pa_memblockq_shorten()\n");*/
214 l
= bq
->current_length
- length
;
218 pa_memblockq_skip(bq
, l
);
222 void pa_memblockq_empty(struct pa_memblockq
*bq
) {
224 pa_memblockq_shorten(bq
, 0);
227 int pa_memblockq_is_readable(struct pa_memblockq
*bq
) {
230 return bq
->current_length
&& (bq
->current_length
>= bq
->prebuf
);
233 int pa_memblockq_is_writable(struct pa_memblockq
*bq
, size_t length
) {
236 return bq
->current_length
+ length
<= bq
->tlength
;
239 uint32_t pa_memblockq_get_length(struct pa_memblockq
*bq
) {
241 return bq
->current_length
;
244 uint32_t pa_memblockq_missing(struct pa_memblockq
*bq
) {
248 if (bq
->current_length
>= bq
->tlength
)
251 l
= bq
->tlength
- bq
->current_length
;
254 return (l
>= bq
->minreq
) ? l
: 0;
257 void pa_memblockq_push_align(struct pa_memblockq
* bq
, const struct pa_memchunk
*chunk
, size_t delta
) {
258 struct pa_memchunk rchunk
;
259 assert(bq
&& chunk
&& bq
->base
);
262 pa_memblockq_push(bq
, chunk
, delta
);
267 bq
->mcalign
= pa_mcalign_new(bq
->base
, bq
->memblock_stat
);
271 pa_mcalign_push(bq
->mcalign
, chunk
);
273 while (pa_mcalign_pop(bq
->mcalign
, &rchunk
) >= 0) {
274 pa_memblockq_push(bq
, &rchunk
, delta
);
275 pa_memblock_unref(rchunk
.memblock
);
280 uint32_t pa_memblockq_get_minreq(struct pa_memblockq
*bq
) {
285 void pa_memblockq_prebuf_disable(struct pa_memblockq
*bq
) {
290 void pa_memblockq_prebuf_reenable(struct pa_memblockq
*bq
) {
292 bq
->prebuf
= bq
->orig_prebuf
;
295 void pa_memblockq_seek(struct pa_memblockq
*bq
, size_t length
) {
301 while (length
>= bq
->base
) {
303 if (!bq
->current_length
)
306 assert(bq
->blocks_tail
);
308 if (l
> bq
->blocks_tail
->chunk
.length
)
309 l
= bq
->blocks_tail
->chunk
.length
;
311 bq
->blocks_tail
->chunk
.length
-= l
;
312 bq
->current_length
-= l
;
314 if (bq
->blocks_tail
->chunk
.length
== 0)
315 remove_block(bq
, bq
->blocks
);
321 void pa_memblockq_flush(struct pa_memblockq
*bq
) {
322 struct memblock_list
*l
;
325 while ((l
= bq
->blocks
)) {
326 bq
->blocks
= l
->next
;
327 pa_memblock_unref(l
->chunk
.memblock
);
331 bq
->blocks_tail
= NULL
;
333 bq
->current_length
= 0;
336 uint32_t pa_memblockq_get_tlength(struct pa_memblockq
*bq
) {