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