summaryrefslogtreecommitdiff
path: root/polyp/memblockq.c
blob: bc5d743715515f31d34a860ebf9f30210fde2824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  polypaudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#include "memblockq.h"
#include "xmalloc.h"

struct memblock_list {
    struct memblock_list *next;
    struct pa_memchunk chunk;
    struct timeval stamp;
};

struct pa_memblockq {
    struct memblock_list *blocks, *blocks_tail;
    unsigned n_blocks;
    size_t current_length, maxlength, tlength, base, prebuf, minreq;
    int measure_delay;
    uint32_t delay;
    struct pa_mcalign *mcalign;
    struct pa_memblock_stat *memblock_stat;
};

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) {
    struct pa_memblockq* bq;
    assert(maxlength && base && maxlength);
    
    bq = pa_xmalloc(sizeof(struct pa_memblockq));
    bq->blocks = bq->blocks_tail = 0;
    bq->n_blocks = 0;

    bq->current_length = 0;

    fprintf(stderr, "memblockq requested: maxlength=%u, tlength=%u, base=%u, prebuf=%u, minreq=%u\n", maxlength, tlength, base, prebuf, minreq);
    
    bq->base = base;

    bq->maxlength = ((maxlength+base-1)/base)*base;
    assert(bq->maxlength >= base);

    bq->tlength = ((tlength+base-1)/base)*base;
    if (bq->tlength == 0 || bq->tlength >= bq->maxlength)
        bq->tlength = bq->maxlength;
    
    bq->prebuf = (prebuf == (size_t) -1) ? bq->maxlength/2 : prebuf;
    bq->prebuf = (bq->prebuf/base)*base;
    if (bq->prebuf > bq->maxlength)
        bq->prebuf = bq->maxlength;
    
    bq->minreq = (minreq/base)*base;
    if (bq->minreq == 0)
        bq->minreq = 1;

    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);
    
    bq->measure_delay = 0;
    bq->delay = 0;

    bq->mcalign = NULL;

    bq->memblock_stat = s;
    
    return bq;
}

void pa_memblockq_free(struct pa_memblockq* bq) {
    struct memblock_list *l;
    assert(bq);

    if (bq->mcalign)
        pa_mcalign_free(bq->mcalign);

    while ((l = bq->blocks)) {
        bq->blocks = l->next;
        pa_memblock_unref(l->chunk.memblock);
        pa_xfree(l);
    }
    
    pa_xfree(bq);
}

void pa_memblockq_push(struct pa_memblockq* bq, const struct pa_memchunk *chunk, size_t delta) {
    struct memblock_list *q;
    assert(bq && chunk && chunk->memblock && chunk->length && (chunk->length % bq->base) == 0);

    if (bq->blocks_tail && bq->blocks_tail->chunk.memblock == chunk->memblock) {
        /* Try to merge memory chunks */

        if (bq->blocks_tail->chunk.index+bq->blocks_tail->chunk.length == chunk->index) {
            bq->blocks_tail->chunk.length += chunk->length;
            bq->current_length += chunk->length;

    /*        fprintf(stderr, __FILE__": merge succeeded: %u\n", chunk->length);*/
            return;
        }
    }
    
    q = pa_xmalloc(sizeof(struct memblock_list));

    if (bq->measure_delay)
        gettimeofday(&q->stamp, NULL);
    else
        timerclear(&q->stamp);

    q->chunk = *chunk;
    pa_memblock_ref(q->chunk.memblock);
    assert(q->chunk.index+q->chunk.length <= q->chunk.memblock->length);
    q->next = NULL;
    
    if (bq->blocks_tail)
        bq->blocks_tail->next = q;
    else
        bq->blocks = q;
    
    bq->blocks_tail = q;

    bq->n_blocks++;
    bq->current_length += chunk->length;

    pa_memblockq_shorten(bq, bq->maxlength);
}

int pa_memblockq_peek(struct pa_memblockq* bq, struct pa_memchunk *chunk) {
    assert(bq && chunk);

    if (!bq->blocks || bq->current_length < bq->prebuf)
        return -1;

    bq->prebuf = 0;

    *chunk = bq->blocks->chunk;
    pa_memblock_ref(chunk->memblock);

/*     if (chunk->memblock->ref != 2) */
/*         fprintf(stderr, "block %p with ref %u peeked.\n", chunk->memblock, chunk->memblock->ref); */
    
    return 0;
}

/*
int memblockq_pop(struct memblockq* bq, struct pa_memchunk *chunk) {
    struct memblock_list *q;
    
    assert(bq && chunk);

    if (!bq->blocks || bq->current_length < bq->prebuf)
        return -1;

    bq->prebuf = 0;

    q = bq->blocks;
    bq->blocks = bq->blocks->next;

    *chunk = q->chunk;

    bq->n_blocks--;
    bq->current_length -= chunk->length;

    pa_xfree(q);
    return 0;
}
*/

static uint32_t age(struct timeval *tv) {
    assert(tv);
    struct timeval now;
    uint32_t r;

    if (tv->tv_sec == 0)
        return 0;

    gettimeofday(&now, NULL);
    
    r = (now.tv_sec-tv->tv_sec) * 1000000;

    if (now.tv_usec >= tv->tv_usec)
        r += now.tv_usec - tv->tv_usec;
    else
        r -= tv->tv_usec - now.tv_usec;

    return r;
}

void pa_memblockq_drop(struct pa_memblockq *bq, size_t length) {
    assert(bq && length && (length % bq->base) == 0);

    while (length > 0) {
        size_t l = length;
        assert(bq->blocks && bq->current_length >= length);
        
        if (l > bq->blocks->chunk.length)
            l = bq->blocks->chunk.length;

        if (bq->measure_delay)
            bq->delay = age(&bq->blocks->stamp);
        
        bq->blocks->chunk.index += l;
        bq->blocks->chunk.length -= l;
        bq->current_length -= l;
        
        if (bq->blocks->chunk.length == 0) {
            struct memblock_list *q;
            
            q = bq->blocks;
            bq->blocks = bq->blocks->next;
            if (bq->blocks == NULL)
                bq->blocks_tail = NULL;
            pa_memblock_unref(q->chunk.memblock);
            pa_xfree(q);
            
            bq->n_blocks--;
        }

        length -= l;
    }
}

void pa_memblockq_shorten(struct pa_memblockq *bq, size_t length) {
    size_t l;
    assert(bq);

    if (bq->current_length <= length)
        return;

    fprintf(stderr, "Warning! pa_memblockq_shorten()\n");
    
    l = bq->current_length - length;
    l /= bq->base;
    l *= bq->base;

    pa_memblockq_drop(bq, l);
}


void pa_memblockq_empty(struct pa_memblockq *bq) {
    assert(bq);
    pa_memblockq_shorten(bq, 0);
}

int pa_memblockq_is_readable(struct pa_memblockq *bq) {
    assert(bq);

    return bq->current_length && (bq->current_length >= bq->prebuf);
}

int pa_memblockq_is_writable(struct pa_memblockq *bq, size_t length) {
    assert(bq);

    return bq->current_length + length <= bq->tlength;
}

uint32_t pa_memblockq_get_delay(struct pa_memblockq *bq) {
    assert(bq);
    return bq->delay;
}

uint32_t pa_memblockq_get_length(struct pa_memblockq *bq) {
    assert(bq);
    return bq->current_length;
}

uint32_t pa_memblockq_missing(struct pa_memblockq *bq) {
    size_t l;
    assert(bq);

    if (bq->current_length >= bq->tlength)
        return 0;

    l = bq->tlength - bq->current_length;
    assert(l);

    return (l >= bq->minreq) ? l : 0;
}

void pa_memblockq_push_align(struct pa_memblockq* bq, const struct pa_memchunk *chunk, size_t delta) {
    struct pa_memchunk rchunk;
    assert(bq && chunk && bq->base);

    if (bq->base == 1) {
        pa_memblockq_push(bq, chunk, delta);
        return;
    }

    if (!bq->mcalign) {
        bq->mcalign = pa_mcalign_new(bq->base, bq->memblock_stat);
        assert(bq->mcalign);
    }
    
    pa_mcalign_push(bq->mcalign, chunk);

    while (pa_mcalign_pop(bq->mcalign, &rchunk) >= 0) {
        pa_memblockq_push(bq, &rchunk, delta);
        pa_memblock_unref(rchunk.memblock);
        delta = 0;
    }
}

uint32_t pa_memblockq_get_minreq(struct pa_memblockq *bq) {
    assert(bq);
    return bq->minreq;
}

void pa_memblockq_prebuf_disable(struct pa_memblockq *bq) {
    assert(bq);
    bq->prebuf = 0;
}