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