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