summaryrefslogtreecommitdiff
path: root/src/pulsecore/mcalign.c
blob: e12f84f8a7be685a5c1a21858d39d5da98ccfe05 (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
/* $Id$ */

/***
  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
  Lesser 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pulse/xmalloc.h>
#include <pulsecore/macro.h>

#include "mcalign.h"

struct pa_mcalign {
    size_t base;
    pa_memchunk leftover, current;
};

pa_mcalign *pa_mcalign_new(size_t base) {
    pa_mcalign *m;
    pa_assert(base);

    m = pa_xnew(pa_mcalign, 1);

    m->base = base;
    pa_memchunk_reset(&m->leftover);
    pa_memchunk_reset(&m->current);

    return m;
}

void pa_mcalign_free(pa_mcalign *m) {
    pa_assert(m);

    if (m->leftover.memblock)
        pa_memblock_unref(m->leftover.memblock);

    if (m->current.memblock)
        pa_memblock_unref(m->current.memblock);

    pa_xfree(m);
}

void pa_mcalign_push(pa_mcalign *m, const pa_memchunk *c) {
    pa_assert(m);
    pa_assert(c);

    pa_assert(c->memblock);
    pa_assert(c->length > 0);

    pa_assert(!m->current.memblock);

    /* Append to the leftover memory block */
    if (m->leftover.memblock) {

        /* Try to merge */
        if (m->leftover.memblock == c->memblock &&
            m->leftover.index + m->leftover.length == c->index) {

            /* Merge */
            m->leftover.length += c->length;

            /* If the new chunk is larger than m->base, move it to current */
            if (m->leftover.length >= m->base) {
                m->current = m->leftover;
                pa_memchunk_reset(&m->leftover);
            }

        } else {
            size_t l;
            void *lo_data, *m_data;

            /* We have to copy */
            pa_assert(m->leftover.length < m->base);
            l = m->base - m->leftover.length;

            if (l > c->length)
                l = c->length;

            /* Can we use the current block? */
            pa_memchunk_make_writable(&m->leftover, m->base);

            lo_data = pa_memblock_acquire(m->leftover.memblock);
            m_data = pa_memblock_acquire(c->memblock);
            memcpy((uint8_t*) lo_data + m->leftover.index + m->leftover.length, (uint8_t*) m_data + c->index, l);
            pa_memblock_release(m->leftover.memblock);
            pa_memblock_release(c->memblock);
            m->leftover.length += l;

            pa_assert(m->leftover.length <= m->base);
            pa_assert(m->leftover.length <= pa_memblock_get_length(m->leftover.memblock));

            if (c->length > l) {
                /* Save the remainder of the memory block */
                m->current = *c;
                m->current.index += l;
                m->current.length -= l;
                pa_memblock_ref(m->current.memblock);
            }
        }
    } else {
        /* Nothing to merge or copy, just store it */

        if (c->length >= m->base)
            m->current = *c;
        else
            m->leftover = *c;

        pa_memblock_ref(c->memblock);
    }
}

int pa_mcalign_pop(pa_mcalign *m, pa_memchunk *c) {
    pa_assert(m);
    pa_assert(c);

    /* First test if there's a leftover memory block available */
    if (m->leftover.memblock) {
        pa_assert(m->leftover.length > 0);
        pa_assert(m->leftover.length <= m->base);

        /* The leftover memory block is not yet complete */
        if (m->leftover.length < m->base)
            return -1;

        /* Return the leftover memory block */
        *c = m->leftover;
        pa_memchunk_reset(&m->leftover);

        /* If the current memblock is too small move it the leftover */
        if (m->current.memblock && m->current.length < m->base) {
            m->leftover = m->current;
            pa_memchunk_reset(&m->current);
        }

        return 0;
    }

    /* Now let's see if there is other data available */
    if (m->current.memblock) {
        size_t l;
        pa_assert(m->current.length >= m->base);

        /* The length of the returned memory block */
        l = m->current.length;
        l /= m->base;
        l *= m->base;
        pa_assert(l > 0);

        /* Prepare the returned block */
        *c = m->current;
        pa_memblock_ref(c->memblock);
        c->length = l;

        /* Drop that from the current memory block */
        pa_assert(l <= m->current.length);
        m->current.index += l;
        m->current.length -= l;

        /* In case the whole block was dropped ... */
        if (m->current.length == 0)
            pa_memblock_unref(m->current.memblock);
        else {
            /* Move the raimainder to leftover */
            pa_assert(m->current.length < m->base && !m->leftover.memblock);

            m->leftover = m->current;
        }

        pa_memchunk_reset(&m->current);

        return 0;
    }

    /* There's simply nothing */
    return -1;
}

size_t pa_mcalign_csize(pa_mcalign *m, size_t l) {
    pa_assert(m);
    pa_assert(l > 0);

    pa_assert(!m->current.memblock);

    if (m->leftover.memblock)
        l += m->leftover.length;

    return (l/m->base)*m->base;
}

void pa_mcalign_flush(pa_mcalign *m) {
    pa_memchunk chunk;
    pa_assert(m);

    while (pa_mcalign_pop(m, &chunk) >= 0)
        pa_memblock_unref(chunk.memblock);
}