summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/memblockq.c
blob: 77f9efc9eb41f21b9da8ac4eb933776646822b34 (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio 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 Lesser General Public License
  along with PulseAudio; 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 <stdlib.h>
#include <string.h>

#include <pulse/xmalloc.h>

#include <pulsecore/log.h>
#include <pulsecore/mcalign.h>
#include <pulsecore/macro.h>
#include <pulsecore/flist.h>

#include "memblockq.h"

struct list_item {
    struct list_item *next, *prev;
    int64_t index;
    pa_memchunk chunk;
};

PA_STATIC_FLIST_DECLARE(list_items, 0, pa_xfree);

struct pa_memblockq {
    struct list_item *blocks, *blocks_tail;
    struct list_item *current_read, *current_write;
    unsigned n_blocks;
    size_t maxlength, tlength, base, prebuf, minreq, maxrewind;
    int64_t read_index, write_index;
    pa_bool_t in_prebuf;
    pa_memchunk silence;
    pa_mcalign *mcalign;
    int64_t missing;
    size_t requested;
};

pa_memblockq* pa_memblockq_new(
        int64_t idx,
        size_t maxlength,
        size_t tlength,
        size_t base,
        size_t prebuf,
        size_t minreq,
        size_t maxrewind,
        pa_memchunk *silence) {

    pa_memblockq* bq;

    pa_assert(base > 0);

    bq = pa_xnew(pa_memblockq, 1);
    bq->blocks = bq->blocks_tail = NULL;
    bq->current_read = bq->current_write = NULL;
    bq->n_blocks = 0;

    bq->base = base;
    bq->read_index = bq->write_index = idx;

    pa_log_debug("memblockq requested: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu maxrewind=%lu",
                 (unsigned long) maxlength, (unsigned long) tlength, (unsigned long) base, (unsigned long) prebuf, (unsigned long) minreq, (unsigned long) maxrewind);

    bq->missing = 0;
    bq->requested = bq->maxlength = bq->tlength = bq->prebuf = bq->minreq = bq->maxrewind = 0;
    bq->in_prebuf = TRUE;

    pa_memblockq_set_maxlength(bq, maxlength);
    pa_memblockq_set_tlength(bq, tlength);
    pa_memblockq_set_minreq(bq, minreq);
    pa_memblockq_set_prebuf(bq, prebuf);
    pa_memblockq_set_maxrewind(bq, maxrewind);

    pa_log_debug("memblockq sanitized: maxlength=%lu, tlength=%lu, base=%lu, prebuf=%lu, minreq=%lu maxrewind=%lu",
                 (unsigned long) bq->maxlength, (unsigned long) bq->tlength, (unsigned long) bq->base, (unsigned long) bq->prebuf, (unsigned long) bq->minreq, (unsigned long) bq->maxrewind);

    if (silence) {
        bq->silence = *silence;
        pa_memblock_ref(bq->silence.memblock);
    } else
        pa_memchunk_reset(&bq->silence);

    bq->mcalign = pa_mcalign_new(bq->base);

    return bq;
}

void pa_memblockq_free(pa_memblockq* bq) {
    pa_assert(bq);

    pa_memblockq_silence(bq);

    if (bq->silence.memblock)
        pa_memblock_unref(bq->silence.memblock);

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

    pa_xfree(bq);
}

static void fix_current_read(pa_memblockq *bq) {
    pa_assert(bq);

    if (PA_UNLIKELY(!bq->blocks)) {
        bq->current_read = NULL;
        return;
    }

    if (PA_UNLIKELY(!bq->current_read))
        bq->current_read = bq->blocks;

    /* Scan left */
    while (PA_UNLIKELY(bq->current_read->index > bq->read_index))

        if (bq->current_read->prev)
            bq->current_read = bq->current_read->prev;
        else
            break;

    /* Scan right */
    while (PA_LIKELY(bq->current_read != NULL) && PA_UNLIKELY(bq->current_read->index + (int64_t) bq->current_read->chunk.length <= bq->read_index))
        bq->current_read = bq->current_read->next;

    /* At this point current_read will either point at or left of the
       next block to play. It may be NULL in case everything in
       the queue was already played */
}

static void fix_current_write(pa_memblockq *bq) {
    pa_assert(bq);

    if (PA_UNLIKELY(!bq->blocks)) {
        bq->current_write = NULL;
        return;
    }

    if (PA_UNLIKELY(!bq->current_write))
        bq->current_write = bq->blocks_tail;

    /* Scan right */
    while (PA_UNLIKELY(bq->current_write->index + (int64_t) bq->current_write->chunk.length <= bq->write_index))

        if (bq->current_write->next)
            bq->current_write = bq->current_write->next;
        else
            break;

    /* Scan left */
    while (PA_LIKELY(bq->current_write != NULL) && PA_UNLIKELY(bq->current_write->index > bq->write_index))
        bq->current_write = bq->current_write->prev;

    /* At this point current_write will either point at or right of
       the next block to write data to. It may be NULL in case
       everything in the queue is still to be played */
}

static void drop_block(pa_memblockq *bq, struct list_item *q) {
    pa_assert(bq);
    pa_assert(q);

    pa_assert(bq->n_blocks >= 1);

    if (q->prev)
        q->prev->next = q->next;
    else {
        pa_assert(bq->blocks == q);
        bq->blocks = q->next;
    }

    if (q->next)
        q->next->prev = q->prev;
    else {
        pa_assert(bq->blocks_tail == q);
        bq->blocks_tail = q->prev;
    }

    if (bq->current_write == q)
        bq->current_write = q->prev;

    if (bq->current_read == q)
        bq->current_read = q->next;

    pa_memblock_unref(q->chunk.memblock);

    if (pa_flist_push(PA_STATIC_FLIST_GET(list_items), q) < 0)
        pa_xfree(q);

    bq->n_blocks--;
}

static void drop_backlog(pa_memblockq *bq) {
    int64_t boundary;
    pa_assert(bq);

    boundary = bq->read_index - (int64_t) bq->maxrewind;

    while (bq->blocks && (bq->blocks->index + (int64_t) bq->blocks->chunk.length <= boundary))
        drop_block(bq, bq->blocks);
}

static pa_bool_t can_push(pa_memblockq *bq, size_t l) {
    int64_t end;

    pa_assert(bq);

    if (bq->read_index > bq->write_index) {
        int64_t d = bq->read_index - bq->write_index;

        if ((int64_t) l > d)
            l -= (size_t) d;
        else
            return TRUE;
    }

    end = bq->blocks_tail ? bq->blocks_tail->index + (int64_t) bq->blocks_tail->chunk.length : bq->write_index;

    /* Make sure that the list doesn't get too long */
    if (bq->write_index + (int64_t) l > end)
        if (bq->write_index + (int64_t) l - bq->read_index > (int64_t) bq->maxlength)
            return FALSE;

    return TRUE;
}

int pa_memblockq_push(pa_memblockq* bq, const pa_memchunk *uchunk) {
    struct list_item *q, *n;
    pa_memchunk chunk;
    int64_t old, delta;

    pa_assert(bq);
    pa_assert(uchunk);
    pa_assert(uchunk->memblock);
    pa_assert(uchunk->length > 0);
    pa_assert(uchunk->index + uchunk->length <= pa_memblock_get_length(uchunk->memblock));

    if (uchunk->length % bq->base)
        return -1;

    if (!can_push(bq, uchunk->length))
        return -1;

    old = bq->write_index;
    chunk = *uchunk;

    fix_current_write(bq);
    q = bq->current_write;

    /* First we advance the q pointer right of where we want to
     * write to */

    if (q) {
        while (bq->write_index + (int64_t) chunk.length > q->index)
            if (q->next)
                q = q->next;
            else
                break;
    }

    if (!q)
        q = bq->blocks_tail;

    /* We go from back to front to look for the right place to add
     * this new entry. Drop data we will overwrite on the way */

    while (q) {

        if (bq->write_index >= q->index + (int64_t) q->chunk.length)
            /* We found the entry where we need to place the new entry immediately after */
            break;
        else if (bq->write_index + (int64_t) chunk.length <= q->index) {
            /* This entry isn't touched at all, let's skip it */
            q = q->prev;
        } else if (bq->write_index <= q->index &&
                   bq->write_index + (int64_t) chunk.length >= q->index + (int64_t) q->chunk.length) {

            /* This entry is fully replaced by the new entry, so let's drop it */

            struct list_item *p;
            p = q;
            q = q->prev;
            drop_block(bq, p);
        } else if (bq->write_index >= q->index) {
            /* The write index points into this memblock, so let's
             * truncate or split it */

            if (bq->write_index + (int64_t) chunk.length < q->index + (int64_t) q->chunk.length) {

                /* We need to save the end of this memchunk */
                struct list_item *p;
                size_t d;

                /* Create a new list entry for the end of thie memchunk */
                if (!(p = pa_flist_pop(PA_STATIC_FLIST_GET(list_items))))
                    p = pa_xnew(struct list_item, 1);

                p->chunk = q->chunk;
                pa_memblock_ref(p->chunk.memblock);

                /* Calculate offset */
                d = (size_t) (bq->write_index + (int64_t) chunk.length - q->index);
                pa_assert(d > 0);

                /* Drop it from the new entry */
                p->index = q->index + (int64_t) d;
                p->chunk.length -= d;

                /* Add it to the list */
                p->prev = q;
                if ((p->next = q->next))
                    q->next->prev = p;
                else
                    bq->blocks_tail = p;
                q->next = p;

                bq->n_blocks++;
            }

            /* Truncate the chunk */
            if (!(q->chunk.length = (size_t) (bq->write_index - q->index))) {
                struct list_item *p;
                p = q;
                q = q->prev;
                drop_block(bq, p);
            }

            /* We had to truncate this block, hence we're now at the right position */
            break;
        } else {
            size_t d;

            pa_assert(bq->write_index + (int64_t)chunk.length > q->index &&
                   bq->write_index + (int64_t)chunk.length < q->index + (int64_t)q->chunk.length &&
                   bq->write_index < q->index);

            /* The job overwrites the current entry at the end, so let's drop the beginning of this entry */

            d = (size_t) (bq->write_index + (int64_t) chunk.length - q->index);
            q->index += (int64_t) d;
            q->chunk.index += d;
            q->chunk.length -= d;

            q = q->prev;
        }
    }

    if (q) {
        pa_assert(bq->write_index >=  q->index + (int64_t)q->chunk.length);
        pa_assert(!q->next || (bq->write_index + (int64_t)chunk.length <= q->next->index));

        /* Try to merge memory blocks */

        if (q->chunk.memblock == chunk.memblock &&
            q->chunk.index + q->chunk.length == chunk.index &&
            bq->write_index == q->index + (int64_t) q->chunk.length) {

            q->chunk.length += chunk.length;
            bq->write_index += (int64_t) chunk.length;
            goto finish;
        }
    } else
        pa_assert(!bq->blocks || (bq->write_index + (int64_t)chunk.length <= bq->blocks->index));

    if (!(n = pa_flist_pop(PA_STATIC_FLIST_GET(list_items))))
        n = pa_xnew(struct list_item, 1);

    n->chunk = chunk;
    pa_memblock_ref(n->chunk.memblock);
    n->index = bq->write_index;
    bq->write_index += (int64_t) n->chunk.length;

    n->next = q ? q->next : bq->blocks;
    n->prev = q;

    if (n->next)
        n->next->prev = n;
    else
        bq->blocks_tail = n;

    if (n->prev)
        n->prev->next = n;
    else
        bq->blocks = n;

    bq->n_blocks++;

finish:

    delta = bq->write_index - old;

    if (delta >= (int64_t) bq->requested) {
        delta -= (int64_t) bq->requested;
        bq->requested = 0;
    } else {
        bq->requested -= (size_t) delta;
        delta = 0;
    }

    bq->missing -= delta;

    return 0;
}

pa_bool_t pa_memblockq_prebuf_active(pa_memblockq *bq) {
    pa_assert(bq);

    if (bq->in_prebuf)
        return pa_memblockq_get_length(bq) < bq->prebuf;
    else
        return bq->prebuf > 0 && bq->read_index >= bq->write_index;
}

static pa_bool_t update_prebuf(pa_memblockq *bq) {
    pa_assert(bq);

    if (bq->in_prebuf) {

        if (pa_memblockq_get_length(bq) < bq->prebuf)
            return TRUE;

        bq->in_prebuf = FALSE;
        return FALSE;
    } else {

        if (bq->prebuf > 0 && bq->read_index >= bq->write_index) {
            bq->in_prebuf = TRUE;
            return TRUE;
        }

        return FALSE;
    }
}

int pa_memblockq_peek(pa_memblockq* bq, pa_memchunk *chunk) {
    int64_t d;
    pa_assert(bq);
    pa_assert(chunk);

    /* We need to pre-buffer */
    if (update_prebuf(bq))
        return -1;

    fix_current_read(bq);

    /* Do we need to spit out silence? */
    if (!bq->current_read || bq->current_read->index > bq->read_index) {

        size_t length;

        /* How much silence shall we return? */
        if (bq->current_read)
            length = (size_t) (bq->current_read->index - bq->read_index);
        else if (bq->write_index > bq->read_index)
            length = (size_t) (bq->write_index - bq->read_index);
        else
            length = 0;

        /* We need to return silence, since no data is yet available */
        if (bq->silence.memblock) {
            *chunk = bq->silence;
            pa_memblock_ref(chunk->memblock);

            if (length > 0 && length < chunk->length)
                chunk->length = length;

        } else {

            /* If the memblockq is empty, return -1, otherwise return
             * the time to sleep */
            if (length <= 0)
                return -1;

            chunk->memblock = NULL;
            chunk->length = length;
        }

        chunk->index = 0;
        return 0;
    }

    /* Ok, let's pass real data to the caller */
    *chunk = bq->current_read->chunk;
    pa_memblock_ref(chunk->memblock);

    pa_assert(bq->read_index >= bq->current_read->index);
    d = bq->read_index - bq->current_read->index;
    chunk->index += (size_t) d;
    chunk->length -= (size_t) d;

    return 0;
}

void pa_memblockq_drop(pa_memblockq *bq, size_t length) {
    int64_t old, delta;
    pa_assert(bq);
    pa_assert(length % bq->base == 0);

    old = bq->read_index;

    while (length > 0) {

        /* Do not drop any data when we are in prebuffering mode */
        if (update_prebuf(bq))
            break;

        fix_current_read(bq);

        if (bq->current_read) {
            int64_t p, d;

            /* We go through this piece by piece to make sure we don't
             * drop more than allowed by prebuf */

            p = bq->current_read->index + (int64_t) bq->current_read->chunk.length;
            pa_assert(p >= bq->read_index);
            d = p - bq->read_index;

            if (d > (int64_t) length)
                d = (int64_t) length;

            bq->read_index += d;
            length -= (size_t) d;

        } else {

            /* The list is empty, there's nothing we could drop */
            bq->read_index += (int64_t) length;
            break;
        }
    }

    drop_backlog(bq);

    delta = bq->read_index - old;
    bq->missing += delta;
}

void pa_memblockq_rewind(pa_memblockq *bq, size_t length) {
    pa_assert(bq);
    pa_assert(length % bq->base == 0);

    /* This is kind of the inverse of pa_memblockq_drop() */

    bq->read_index -= (int64_t) length;
    bq->missing -= (int64_t) length;
}

pa_bool_t pa_memblockq_is_readable(pa_memblockq *bq) {
    pa_assert(bq);

    if (pa_memblockq_prebuf_active(bq))
        return FALSE;

    if (pa_memblockq_get_length(bq) <= 0)
        return FALSE;

    return TRUE;
}

size_t pa_memblockq_get_length(pa_memblockq *bq) {
    pa_assert(bq);

    if (bq->write_index <= bq->read_index)
        return 0;

    return (size_t) (bq->write_index - bq->read_index);
}

size_t pa_memblockq_missing(pa_memblockq *bq) {
    size_t l;
    pa_assert(bq);

    if ((l = pa_memblockq_get_length(bq)) >= bq->tlength)
        return 0;

    l = bq->tlength - l;

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

void pa_memblockq_seek(pa_memblockq *bq, int64_t offset, pa_seek_mode_t seek, pa_bool_t account) {
    int64_t old, delta;
    pa_assert(bq);

    old = bq->write_index;

    switch (seek) {
        case PA_SEEK_RELATIVE:
            bq->write_index += offset;
            break;
        case PA_SEEK_ABSOLUTE:
            bq->write_index = offset;
            break;
        case PA_SEEK_RELATIVE_ON_READ:
            bq->write_index = bq->read_index + offset;
            break;
        case PA_SEEK_RELATIVE_END:
            bq->write_index = (bq->blocks_tail ? bq->blocks_tail->index + (int64_t) bq->blocks_tail->chunk.length : bq->read_index) + offset;
            break;
        default:
            pa_assert_not_reached();
    }

    drop_backlog(bq);

    delta = bq->write_index - old;

    if (account) {
        if (delta >= (int64_t) bq->requested) {
            delta -= (int64_t) bq->requested;
            bq->requested = 0;
        } else if (delta >= 0) {
            bq->requested -= (size_t) delta;
            delta = 0;
        }
    }

    bq->missing -= delta;
}

void pa_memblockq_flush_write(pa_memblockq *bq) {
    int64_t old, delta;
    pa_assert(bq);

    pa_memblockq_silence(bq);

    old = bq->write_index;
    bq->write_index = bq->read_index;

    pa_memblockq_prebuf_force(bq);

    delta = bq->write_index - old;

    if (delta >= (int64_t) bq->requested) {
        delta -= (int64_t) bq->requested;
        bq->requested = 0;
    } else if (delta >= 0) {
        bq->requested -= (size_t) delta;
        delta = 0;
    }

    bq->missing -= delta;
}

void pa_memblockq_flush_read(pa_memblockq *bq) {
    int64_t old, delta;
    pa_assert(bq);

    pa_memblockq_silence(bq);

    old = bq->read_index;
    bq->read_index = bq->write_index;

    pa_memblockq_prebuf_force(bq);

    delta = bq->read_index - old;
    bq->missing += delta;
}

size_t pa_memblockq_get_tlength(pa_memblockq *bq) {
    pa_assert(bq);

    return bq->tlength;
}

size_t pa_memblockq_get_minreq(pa_memblockq *bq) {
    pa_assert(bq);

    return bq->minreq;
}

int64_t pa_memblockq_get_read_index(pa_memblockq *bq) {
    pa_assert(bq);

    return bq->read_index;
}

int64_t pa_memblockq_get_write_index(pa_memblockq *bq) {
    pa_assert(bq);

    return bq->write_index;
}

int pa_memblockq_push_align(pa_memblockq* bq, const pa_memchunk *chunk) {
    pa_memchunk rchunk;

    pa_assert(bq);
    pa_assert(chunk);

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

    if (!can_push(bq, pa_mcalign_csize(bq->mcalign, chunk->length)))
        return -1;

    pa_mcalign_push(bq->mcalign, chunk);

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

        if (r < 0) {
            pa_mcalign_flush(bq->mcalign);
            return -1;
        }
    }

    return 0;
}

void pa_memblockq_prebuf_disable(pa_memblockq *bq) {
    pa_assert(bq);

    bq->in_prebuf = FALSE;
}

void pa_memblockq_prebuf_force(pa_memblockq *bq) {
    pa_assert(bq);

    if (bq->prebuf > 0)
        bq->in_prebuf = TRUE;
}

size_t pa_memblockq_get_maxlength(pa_memblockq *bq) {
    pa_assert(bq);

    return bq->maxlength;
}

size_t pa_memblockq_get_prebuf(pa_memblockq *bq) {
    pa_assert(bq);

    return bq->prebuf;
}

size_t pa_memblockq_pop_missing(pa_memblockq *bq) {
    size_t l;

    pa_assert(bq);

/*     pa_log("pop: %lli", bq->missing); */

    if (bq->missing <= 0)
        return 0;

    l = (size_t) bq->missing;
    bq->missing = 0;
    bq->requested += l;

    return l;
}

void pa_memblockq_set_maxlength(pa_memblockq *bq, size_t maxlength) {
    pa_assert(bq);

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

    if (bq->maxlength < bq->base)
        bq->maxlength = bq->base;

    if (bq->tlength > bq->maxlength)
        pa_memblockq_set_tlength(bq, bq->maxlength);
}

void pa_memblockq_set_tlength(pa_memblockq *bq, size_t tlength) {
    size_t old_tlength;
    pa_assert(bq);

    if (tlength <= 0 || tlength == (size_t) -1)
        tlength = bq->maxlength;

    old_tlength = bq->tlength;
    bq->tlength = ((tlength+bq->base-1)/bq->base)*bq->base;

    if (bq->tlength > bq->maxlength)
        bq->tlength = bq->maxlength;

    if (bq->minreq > bq->tlength)
        pa_memblockq_set_minreq(bq, bq->tlength);

    if (bq->prebuf > bq->tlength+bq->base-bq->minreq)
        pa_memblockq_set_prebuf(bq, bq->tlength+bq->base-bq->minreq);

    bq->missing += (int64_t) bq->tlength - (int64_t) old_tlength;
}

void pa_memblockq_set_minreq(pa_memblockq *bq, size_t minreq) {
    pa_assert(bq);

    bq->minreq = (minreq/bq->base)*bq->base;

    if (bq->minreq > bq->tlength)
        bq->minreq = bq->tlength;

    if (bq->minreq < bq->base)
        bq->minreq = bq->base;

    if (bq->prebuf > bq->tlength+bq->base-bq->minreq)
        pa_memblockq_set_prebuf(bq, bq->tlength+bq->base-bq->minreq);
}

void pa_memblockq_set_prebuf(pa_memblockq *bq, size_t prebuf) {
    pa_assert(bq);

    if (prebuf == (size_t) -1)
        prebuf = bq->tlength+bq->base-bq->minreq;

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

    if (prebuf > 0 && bq->prebuf < bq->base)
        bq->prebuf = bq->base;

    if (bq->prebuf > bq->tlength+bq->base-bq->minreq)
        bq->prebuf = bq->tlength+bq->base-bq->minreq;

    if (bq->prebuf <= 0 || pa_memblockq_get_length(bq) >= bq->prebuf)
        bq->in_prebuf = FALSE;
}

void pa_memblockq_set_maxrewind(pa_memblockq *bq, size_t maxrewind) {
    pa_assert(bq);

    bq->maxrewind = (maxrewind/bq->base)*bq->base;
}

void pa_memblockq_apply_attr(pa_memblockq *bq, const pa_buffer_attr *a) {
    pa_assert(bq);
    pa_assert(a);

    pa_memblockq_set_maxlength(bq, a->maxlength);
    pa_memblockq_set_tlength(bq, a->tlength);
    pa_memblockq_set_prebuf(bq, a->prebuf);
    pa_memblockq_set_minreq(bq, a->minreq);
}

void pa_memblockq_get_attr(pa_memblockq *bq, pa_buffer_attr *a) {
    pa_assert(bq);
    pa_assert(a);

    a->maxlength = (uint32_t) pa_memblockq_get_maxlength(bq);
    a->tlength = (uint32_t) pa_memblockq_get_tlength(bq);
    a->prebuf = (uint32_t) pa_memblockq_get_prebuf(bq);
    a->minreq = (uint32_t) pa_memblockq_get_minreq(bq);
}

int pa_memblockq_splice(pa_memblockq *bq, pa_memblockq *source) {

    pa_assert(bq);
    pa_assert(source);

    pa_memblockq_prebuf_disable(bq);

    for (;;) {
        pa_memchunk chunk;

        if (pa_memblockq_peek(source, &chunk) < 0)
            return 0;

        pa_assert(chunk.length > 0);

        if (chunk.memblock) {

            if (pa_memblockq_push_align(bq, &chunk) < 0) {
                pa_memblock_unref(chunk.memblock);
                return -1;
            }

            pa_memblock_unref(chunk.memblock);
        } else
            pa_memblockq_seek(bq, (int64_t) chunk.length, PA_SEEK_RELATIVE, TRUE);

        pa_memblockq_drop(bq, chunk.length);
    }
}

void pa_memblockq_willneed(pa_memblockq *bq) {
    struct list_item *q;

    pa_assert(bq);

    fix_current_read(bq);

    for (q = bq->current_read; q; q = q->next)
        pa_memchunk_will_need(&q->chunk);
}

void pa_memblockq_set_silence(pa_memblockq *bq, pa_memchunk *silence) {
    pa_assert(bq);

    if (bq->silence.memblock)
        pa_memblock_unref(bq->silence.memblock);

    if (silence) {
        bq->silence = *silence;
        pa_memblock_ref(bq->silence.memblock);
    } else
        pa_memchunk_reset(&bq->silence);
}

pa_bool_t pa_memblockq_is_empty(pa_memblockq *bq) {
    pa_assert(bq);

    return !bq->blocks;
}

void pa_memblockq_silence(pa_memblockq *bq) {
    pa_assert(bq);

    while (bq->blocks)
        drop_block(bq, bq->blocks);

    pa_assert(bq->n_blocks == 0);
}

unsigned pa_memblockq_get_nblocks(pa_memblockq *bq) {
    pa_assert(bq);

    return bq->n_blocks;
}

size_t pa_memblockq_get_base(pa_memblockq *bq) {
    pa_assert(bq);

    return bq->base;
}