0bba6581700d7395f075d88d1c37ec74afa687fe
[profile/ivi/pulseaudio-panda.git] / polyp / memblockq.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/time.h>
27 #include <time.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "memblockq.h"
34 #include "xmalloc.h"
35 #include "log.h"
36
37 struct memblock_list {
38     struct memblock_list *next, *prev;
39     struct pa_memchunk chunk;
40 };
41
42 struct pa_memblockq {
43     struct memblock_list *blocks, *blocks_tail;
44     unsigned n_blocks;
45     size_t current_length, maxlength, tlength, base, prebuf, minreq;
46     struct pa_mcalign *mcalign;
47     struct pa_memblock_stat *memblock_stat;
48 };
49
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);
53     
54     bq = pa_xmalloc(sizeof(struct pa_memblockq));
55     bq->blocks = bq->blocks_tail = 0;
56     bq->n_blocks = 0;
57
58     bq->current_length = 0;
59
60     /*pa_log(__FILE__": memblockq requested: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", maxlength, tlength, base, prebuf, minreq);*/
61     
62     bq->base = base;
63
64     bq->maxlength = ((maxlength+base-1)/base)*base;
65     assert(bq->maxlength >= base);
66
67     bq->tlength = ((tlength+base-1)/base)*base;
68     if (!bq->tlength || bq->tlength >= bq->maxlength)
69         bq->tlength = bq->maxlength;
70     
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;
75     
76     bq->minreq = (minreq/base)*base;
77     if (bq->minreq == 0)
78         bq->minreq = 1;
79
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);
81     
82     bq->mcalign = NULL;
83
84     bq->memblock_stat = s;
85
86     return bq;
87 }
88
89 void pa_memblockq_free(struct pa_memblockq* bq) {
90     assert(bq);
91
92     pa_memblockq_flush(bq);
93     
94     if (bq->mcalign)
95         pa_mcalign_free(bq->mcalign);
96
97     pa_xfree(bq);
98 }
99
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);
103
104     pa_memblockq_seek(bq, delta);
105     
106     if (bq->blocks_tail && bq->blocks_tail->chunk.memblock == chunk->memblock) {
107         /* Try to merge memory chunks */
108
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;
112             return;
113         }
114     }
115     
116     q = pa_xmalloc(sizeof(struct memblock_list));
117
118     q->chunk = *chunk;
119     pa_memblock_ref(q->chunk.memblock);
120     assert(q->chunk.index+q->chunk.length <= q->chunk.memblock->length);
121     q->next = NULL;
122     if ((q->prev = bq->blocks_tail))
123         bq->blocks_tail->next = q;
124     else
125         bq->blocks = q;
126     
127     bq->blocks_tail = q;
128
129     bq->n_blocks++;
130     bq->current_length += chunk->length;
131
132     pa_memblockq_shorten(bq, bq->maxlength);
133 }
134
135 int pa_memblockq_peek(struct pa_memblockq* bq, struct pa_memchunk *chunk) {
136     assert(bq && chunk);
137
138     if (!bq->blocks || bq->current_length < bq->prebuf)
139         return -1;
140
141     bq->prebuf = 0;
142
143     *chunk = bq->blocks->chunk;
144     pa_memblock_ref(chunk->memblock);
145
146     return 0;
147 }
148
149 void pa_memblockq_drop(struct pa_memblockq *bq, const struct pa_memchunk *chunk, size_t length) {
150     assert(bq && chunk && length);
151
152     if (!bq->blocks || memcmp(&bq->blocks->chunk, chunk, sizeof(struct pa_memchunk)))
153         return;
154
155     assert(length <= bq->blocks->chunk.length);
156     pa_memblockq_skip(bq, length);
157 }
158
159 static void remove_block(struct pa_memblockq *bq, struct memblock_list *q) {
160     assert(bq && q);
161
162     if (q->prev)
163         q->prev->next = q->next;
164     else {
165         assert(bq->blocks == q);
166         bq->blocks = q->next;
167     }
168     
169     if (q->next)
170         q->next->prev = q->prev;
171     else {
172         assert(bq->blocks_tail == q);
173         bq->blocks_tail = q->prev;
174     }
175     
176     pa_memblock_unref(q->chunk.memblock);
177     pa_xfree(q);
178     
179     bq->n_blocks--;
180 }
181
182 void pa_memblockq_skip(struct pa_memblockq *bq, size_t length) {
183     assert(bq && length && (length % bq->base) == 0);
184
185     while (length > 0) {
186         size_t l = length;
187         assert(bq->blocks && bq->current_length >= length);
188         
189         if (l > bq->blocks->chunk.length)
190             l = bq->blocks->chunk.length;
191
192         bq->blocks->chunk.index += l;
193         bq->blocks->chunk.length -= l;
194         bq->current_length -= l;
195         
196         if (!bq->blocks->chunk.length)
197             remove_block(bq, bq->blocks);
198
199         length -= l;
200     }
201 }
202
203 void pa_memblockq_shorten(struct pa_memblockq *bq, size_t length) {
204     size_t l;
205     assert(bq);
206
207     if (bq->current_length <= length)
208         return;
209
210     pa_log(__FILE__": Warning! pa_memblockq_shorten()\n");
211     
212     l = bq->current_length - length;
213     l /= bq->base;
214     l *= bq->base;
215
216     pa_memblockq_skip(bq, l);
217 }
218
219
220 void pa_memblockq_empty(struct pa_memblockq *bq) {
221     assert(bq);
222     pa_memblockq_shorten(bq, 0);
223 }
224
225 int pa_memblockq_is_readable(struct pa_memblockq *bq) {
226     assert(bq);
227
228     return bq->current_length && (bq->current_length >= bq->prebuf);
229 }
230
231 int pa_memblockq_is_writable(struct pa_memblockq *bq, size_t length) {
232     assert(bq);
233
234     return bq->current_length + length <= bq->tlength;
235 }
236
237 uint32_t pa_memblockq_get_length(struct pa_memblockq *bq) {
238     assert(bq);
239     return bq->current_length;
240 }
241
242 uint32_t pa_memblockq_missing(struct pa_memblockq *bq) {
243     size_t l;
244     assert(bq);
245
246     if (bq->current_length >= bq->tlength)
247         return 0;
248
249     l = bq->tlength - bq->current_length;
250     assert(l);
251
252     return (l >= bq->minreq) ? l : 0;
253 }
254
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);
258
259     if (bq->base == 1) {
260         pa_memblockq_push(bq, chunk, delta);
261         return;
262     }
263
264     if (!bq->mcalign) {
265         bq->mcalign = pa_mcalign_new(bq->base, bq->memblock_stat);
266         assert(bq->mcalign);
267     }
268     
269     pa_mcalign_push(bq->mcalign, chunk);
270
271     while (pa_mcalign_pop(bq->mcalign, &rchunk) >= 0) {
272         pa_memblockq_push(bq, &rchunk, delta);
273         pa_memblock_unref(rchunk.memblock);
274         delta = 0;
275     }
276 }
277
278 uint32_t pa_memblockq_get_minreq(struct pa_memblockq *bq) {
279     assert(bq);
280     return bq->minreq;
281 }
282
283 void pa_memblockq_prebuf_disable(struct pa_memblockq *bq) {
284     assert(bq);
285     bq->prebuf = 0;
286 }
287
288 void pa_memblockq_seek(struct pa_memblockq *bq, size_t length) {
289     assert(bq);
290
291     if (!length)
292         return;
293
294     while (length >= bq->base) {
295         size_t l = length;
296         if (!bq->current_length)
297             return;
298
299         assert(bq->blocks_tail);
300         
301         if (l > bq->blocks_tail->chunk.length)
302             l = bq->blocks_tail->chunk.length;
303
304         bq->blocks_tail->chunk.length -= l;
305         bq->current_length -= l;
306         
307         if (bq->blocks_tail->chunk.length == 0)
308             remove_block(bq, bq->blocks);
309
310         length -= l;
311     }
312 }
313
314 void pa_memblockq_flush(struct pa_memblockq *bq) {
315     struct memblock_list *l;
316     assert(bq);
317     
318     while ((l = bq->blocks)) {
319         bq->blocks = l->next;
320         pa_memblock_unref(l->chunk.memblock);
321         pa_xfree(l);
322     }
323
324     bq->blocks_tail = NULL;
325     bq->n_blocks = 0;
326     bq->current_length = 0;
327 }
328
329 uint32_t pa_memblockq_get_tlength(struct pa_memblockq *bq) {
330     assert(bq);
331     return bq->tlength;
332 }