4 This file is part of polypaudio.
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.
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.
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
32 #include "memblockq.h"
34 struct memblock_list {
35 struct memblock_list *next;
36 struct pa_memchunk chunk;
41 struct memblock_list *blocks, *blocks_tail;
43 size_t current_length, maxlength, tlength, base, prebuf, minreq;
46 struct pa_mcalign *mcalign;
49 struct pa_memblockq* pa_memblockq_new(size_t maxlength, size_t tlength, size_t base, size_t prebuf, size_t minreq) {
50 struct pa_memblockq* bq;
51 assert(maxlength && base && maxlength);
53 bq = malloc(sizeof(struct pa_memblockq));
55 bq->blocks = bq->blocks_tail = 0;
58 bq->current_length = 0;
60 fprintf(stderr, "memblockq requested: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", maxlength, tlength, base, prebuf, minreq);
64 bq->maxlength = ((maxlength+base-1)/base)*base;
65 assert(bq->maxlength >= base);
67 bq->tlength = ((tlength+base-1)/base)*base;
68 if (bq->tlength == 0 || bq->tlength >= bq->maxlength)
69 bq->tlength = bq->maxlength;
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;
76 bq->minreq = (minreq/base)*base;
80 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 bq->measure_delay = 0;
90 void pa_memblockq_free(struct pa_memblockq* bq) {
91 struct memblock_list *l;
95 pa_mcalign_free(bq->mcalign);
97 while ((l = bq->blocks)) {
99 pa_memblock_unref(l->chunk.memblock);
106 void pa_memblockq_push(struct pa_memblockq* bq, const struct pa_memchunk *chunk, size_t delta) {
107 struct memblock_list *q;
108 assert(bq && chunk && chunk->memblock && chunk->length && (chunk->length % bq->base) == 0);
110 if (bq->blocks_tail && bq->blocks_tail->chunk.memblock == chunk->memblock) {
111 /* Try to merge memory chunks */
113 if (bq->blocks_tail->chunk.index+bq->blocks_tail->chunk.length == chunk->index) {
114 bq->blocks_tail->chunk.length += chunk->length;
115 bq->current_length += chunk->length;
117 /* fprintf(stderr, __FILE__": merge succeeded: %u\n", chunk->length);*/
122 q = malloc(sizeof(struct memblock_list));
125 if (bq->measure_delay)
126 gettimeofday(&q->stamp, NULL);
128 timerclear(&q->stamp);
131 pa_memblock_ref(q->chunk.memblock);
132 assert(q->chunk.index+q->chunk.length <= q->chunk.memblock->length);
136 bq->blocks_tail->next = q;
143 bq->current_length += chunk->length;
145 pa_memblockq_shorten(bq, bq->maxlength);
148 int pa_memblockq_peek(struct pa_memblockq* bq, struct pa_memchunk *chunk) {
151 if (!bq->blocks || bq->current_length < bq->prebuf)
156 *chunk = bq->blocks->chunk;
157 pa_memblock_ref(chunk->memblock);
159 /* if (chunk->memblock->ref != 2) */
160 /* fprintf(stderr, "block %p with ref %u peeked.\n", chunk->memblock, chunk->memblock->ref); */
166 int memblockq_pop(struct memblockq* bq, struct pa_memchunk *chunk) {
167 struct memblock_list *q;
171 if (!bq->blocks || bq->current_length < bq->prebuf)
177 bq->blocks = bq->blocks->next;
182 bq->current_length -= chunk->length;
189 static uint32_t age(struct timeval *tv) {
197 gettimeofday(&now, NULL);
199 r = (now.tv_sec-tv->tv_sec) * 1000000;
201 if (now.tv_usec >= tv->tv_usec)
202 r += now.tv_usec - tv->tv_usec;
204 r -= tv->tv_usec - now.tv_usec;
209 void pa_memblockq_drop(struct pa_memblockq *bq, size_t length) {
210 assert(bq && length && (length % bq->base) == 0);
214 assert(bq->blocks && bq->current_length >= length);
216 if (l > bq->blocks->chunk.length)
217 l = bq->blocks->chunk.length;
219 if (bq->measure_delay)
220 bq->delay = age(&bq->blocks->stamp);
222 bq->blocks->chunk.index += l;
223 bq->blocks->chunk.length -= l;
224 bq->current_length -= l;
226 if (bq->blocks->chunk.length == 0) {
227 struct memblock_list *q;
230 bq->blocks = bq->blocks->next;
231 if (bq->blocks == NULL)
232 bq->blocks_tail = NULL;
233 pa_memblock_unref(q->chunk.memblock);
243 void pa_memblockq_shorten(struct pa_memblockq *bq, size_t length) {
247 if (bq->current_length <= length)
250 fprintf(stderr, "Warning! pa_memblockq_shorten()\n");
252 l = bq->current_length - length;
256 pa_memblockq_drop(bq, l);
260 void pa_memblockq_empty(struct pa_memblockq *bq) {
262 pa_memblockq_shorten(bq, 0);
265 int pa_memblockq_is_readable(struct pa_memblockq *bq) {
268 return bq->current_length && (bq->current_length >= bq->prebuf);
271 int pa_memblockq_is_writable(struct pa_memblockq *bq, size_t length) {
274 return bq->current_length + length <= bq->tlength;
277 uint32_t pa_memblockq_get_delay(struct pa_memblockq *bq) {
282 uint32_t pa_memblockq_get_length(struct pa_memblockq *bq) {
284 return bq->current_length;
287 uint32_t pa_memblockq_missing(struct pa_memblockq *bq) {
291 if (bq->current_length >= bq->tlength)
294 l = bq->tlength - bq->current_length;
297 return (l >= bq->minreq) ? l : 0;
300 void pa_memblockq_push_align(struct pa_memblockq* bq, const struct pa_memchunk *chunk, size_t delta) {
301 struct pa_memchunk rchunk;
302 assert(bq && chunk && bq->base);
305 pa_memblockq_push(bq, chunk, delta);
310 bq->mcalign = pa_mcalign_new(bq->base);
314 pa_mcalign_push(bq->mcalign, chunk);
316 while (pa_mcalign_pop(bq->mcalign, &rchunk) >= 0) {
317 pa_memblockq_push(bq, &rchunk, delta);
318 pa_memblock_unref(rchunk.memblock);
323 uint32_t pa_memblockq_get_minreq(struct pa_memblockq *bq) {