]> code.delx.au - pulseaudio/blob - polyp/memblockq.c
move sample cache to namereg
[pulseaudio] / 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
32 #include "memblockq.h"
33 #include "xmalloc.h"
34
35 struct memblock_list {
36 struct memblock_list *next;
37 struct pa_memchunk chunk;
38 struct timeval stamp;
39 };
40
41 struct pa_memblockq {
42 struct memblock_list *blocks, *blocks_tail;
43 unsigned n_blocks;
44 size_t current_length, maxlength, tlength, base, prebuf, minreq;
45 int measure_delay;
46 uint32_t delay;
47 struct pa_mcalign *mcalign;
48 struct pa_memblock_stat *memblock_stat;
49 };
50
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);
54
55 bq = pa_xmalloc(sizeof(struct pa_memblockq));
56 bq->blocks = bq->blocks_tail = 0;
57 bq->n_blocks = 0;
58
59 bq->current_length = 0;
60
61 fprintf(stderr, "memblockq requested: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", maxlength, tlength, base, prebuf, minreq);
62
63 bq->base = base;
64
65 bq->maxlength = ((maxlength+base-1)/base)*base;
66 assert(bq->maxlength >= base);
67
68 bq->tlength = ((tlength+base-1)/base)*base;
69 if (bq->tlength == 0 || bq->tlength >= bq->maxlength)
70 bq->tlength = bq->maxlength;
71
72 bq->prebuf = (prebuf == (size_t) -1) ? bq->maxlength/2 : prebuf;
73 bq->prebuf = (bq->prebuf/base)*base;
74 if (bq->prebuf > bq->maxlength)
75 bq->prebuf = bq->maxlength;
76
77 bq->minreq = (minreq/base)*base;
78 if (bq->minreq == 0)
79 bq->minreq = 1;
80
81 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);
82
83 bq->measure_delay = 0;
84 bq->delay = 0;
85
86 bq->mcalign = NULL;
87
88 bq->memblock_stat = s;
89
90 return bq;
91 }
92
93 void pa_memblockq_free(struct pa_memblockq* bq) {
94 struct memblock_list *l;
95 assert(bq);
96
97 if (bq->mcalign)
98 pa_mcalign_free(bq->mcalign);
99
100 while ((l = bq->blocks)) {
101 bq->blocks = l->next;
102 pa_memblock_unref(l->chunk.memblock);
103 pa_xfree(l);
104 }
105
106 pa_xfree(bq);
107 }
108
109 void pa_memblockq_push(struct pa_memblockq* bq, const struct pa_memchunk *chunk, size_t delta) {
110 struct memblock_list *q;
111 assert(bq && chunk && chunk->memblock && chunk->length && (chunk->length % bq->base) == 0);
112
113 if (bq->blocks_tail && bq->blocks_tail->chunk.memblock == chunk->memblock) {
114 /* Try to merge memory chunks */
115
116 if (bq->blocks_tail->chunk.index+bq->blocks_tail->chunk.length == chunk->index) {
117 bq->blocks_tail->chunk.length += chunk->length;
118 bq->current_length += chunk->length;
119
120 /* fprintf(stderr, __FILE__": merge succeeded: %u\n", chunk->length);*/
121 return;
122 }
123 }
124
125 q = pa_xmalloc(sizeof(struct memblock_list));
126
127 if (bq->measure_delay)
128 gettimeofday(&q->stamp, NULL);
129 else
130 timerclear(&q->stamp);
131
132 q->chunk = *chunk;
133 pa_memblock_ref(q->chunk.memblock);
134 assert(q->chunk.index+q->chunk.length <= q->chunk.memblock->length);
135 q->next = NULL;
136
137 if (bq->blocks_tail)
138 bq->blocks_tail->next = q;
139 else
140 bq->blocks = q;
141
142 bq->blocks_tail = q;
143
144 bq->n_blocks++;
145 bq->current_length += chunk->length;
146
147 pa_memblockq_shorten(bq, bq->maxlength);
148 }
149
150 int pa_memblockq_peek(struct pa_memblockq* bq, struct pa_memchunk *chunk) {
151 assert(bq && chunk);
152
153 if (!bq->blocks || bq->current_length < bq->prebuf)
154 return -1;
155
156 bq->prebuf = 0;
157
158 *chunk = bq->blocks->chunk;
159 pa_memblock_ref(chunk->memblock);
160
161 /* if (chunk->memblock->ref != 2) */
162 /* fprintf(stderr, "block %p with ref %u peeked.\n", chunk->memblock, chunk->memblock->ref); */
163
164 return 0;
165 }
166
167 /*
168 int memblockq_pop(struct memblockq* bq, struct pa_memchunk *chunk) {
169 struct memblock_list *q;
170
171 assert(bq && chunk);
172
173 if (!bq->blocks || bq->current_length < bq->prebuf)
174 return -1;
175
176 bq->prebuf = 0;
177
178 q = bq->blocks;
179 bq->blocks = bq->blocks->next;
180
181 *chunk = q->chunk;
182
183 bq->n_blocks--;
184 bq->current_length -= chunk->length;
185
186 pa_xfree(q);
187 return 0;
188 }
189 */
190
191 static uint32_t age(struct timeval *tv) {
192 struct timeval now;
193 uint32_t r;
194 assert(tv);
195
196 if (tv->tv_sec == 0)
197 return 0;
198
199 gettimeofday(&now, NULL);
200
201 r = (now.tv_sec-tv->tv_sec) * 1000000;
202
203 if (now.tv_usec >= tv->tv_usec)
204 r += now.tv_usec - tv->tv_usec;
205 else
206 r -= tv->tv_usec - now.tv_usec;
207
208 return r;
209 }
210
211 void pa_memblockq_drop(struct pa_memblockq *bq, size_t length) {
212 assert(bq && length && (length % bq->base) == 0);
213
214 while (length > 0) {
215 size_t l = length;
216 assert(bq->blocks && bq->current_length >= length);
217
218 if (l > bq->blocks->chunk.length)
219 l = bq->blocks->chunk.length;
220
221 if (bq->measure_delay)
222 bq->delay = age(&bq->blocks->stamp);
223
224 bq->blocks->chunk.index += l;
225 bq->blocks->chunk.length -= l;
226 bq->current_length -= l;
227
228 if (bq->blocks->chunk.length == 0) {
229 struct memblock_list *q;
230
231 q = bq->blocks;
232 bq->blocks = bq->blocks->next;
233 if (bq->blocks == NULL)
234 bq->blocks_tail = NULL;
235 pa_memblock_unref(q->chunk.memblock);
236 pa_xfree(q);
237
238 bq->n_blocks--;
239 }
240
241 length -= l;
242 }
243 }
244
245 void pa_memblockq_shorten(struct pa_memblockq *bq, size_t length) {
246 size_t l;
247 assert(bq);
248
249 if (bq->current_length <= length)
250 return;
251
252 fprintf(stderr, "Warning! pa_memblockq_shorten()\n");
253
254 l = bq->current_length - length;
255 l /= bq->base;
256 l *= bq->base;
257
258 pa_memblockq_drop(bq, l);
259 }
260
261
262 void pa_memblockq_empty(struct pa_memblockq *bq) {
263 assert(bq);
264 pa_memblockq_shorten(bq, 0);
265 }
266
267 int pa_memblockq_is_readable(struct pa_memblockq *bq) {
268 assert(bq);
269
270 return bq->current_length && (bq->current_length >= bq->prebuf);
271 }
272
273 int pa_memblockq_is_writable(struct pa_memblockq *bq, size_t length) {
274 assert(bq);
275
276 return bq->current_length + length <= bq->tlength;
277 }
278
279 uint32_t pa_memblockq_get_delay(struct pa_memblockq *bq) {
280 assert(bq);
281 return bq->delay;
282 }
283
284 uint32_t pa_memblockq_get_length(struct pa_memblockq *bq) {
285 assert(bq);
286 return bq->current_length;
287 }
288
289 uint32_t pa_memblockq_missing(struct pa_memblockq *bq) {
290 size_t l;
291 assert(bq);
292
293 if (bq->current_length >= bq->tlength)
294 return 0;
295
296 l = bq->tlength - bq->current_length;
297 assert(l);
298
299 return (l >= bq->minreq) ? l : 0;
300 }
301
302 void pa_memblockq_push_align(struct pa_memblockq* bq, const struct pa_memchunk *chunk, size_t delta) {
303 struct pa_memchunk rchunk;
304 assert(bq && chunk && bq->base);
305
306 if (bq->base == 1) {
307 pa_memblockq_push(bq, chunk, delta);
308 return;
309 }
310
311 if (!bq->mcalign) {
312 bq->mcalign = pa_mcalign_new(bq->base, bq->memblock_stat);
313 assert(bq->mcalign);
314 }
315
316 pa_mcalign_push(bq->mcalign, chunk);
317
318 while (pa_mcalign_pop(bq->mcalign, &rchunk) >= 0) {
319 pa_memblockq_push(bq, &rchunk, delta);
320 pa_memblock_unref(rchunk.memblock);
321 delta = 0;
322 }
323 }
324
325 uint32_t pa_memblockq_get_minreq(struct pa_memblockq *bq) {
326 assert(bq);
327 return bq->minreq;
328 }
329
330 void pa_memblockq_prebuf_disable(struct pa_memblockq *bq) {
331 assert(bq);
332 bq->prebuf = 0;
333 }