summaryrefslogtreecommitdiff
path: root/src/memchunk.c
blob: d27ca61aa12a943e7644217ead31e83871703882 (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
/* $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 <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "memchunk.h"

void pa_memchunk_make_writable(struct pa_memchunk *c) {
    struct pa_memblock *n;
    assert(c && c->memblock && c->memblock->ref >= 1);

    if (c->memblock->ref == 1)
        return;
    
    n = pa_memblock_new(c->length);
    assert(n);
    memcpy(n->data, c->memblock->data+c->index, c->length);
    pa_memblock_unref(c->memblock);
    c->memblock = n;
    c->index = 0;
}


struct pa_mcalign {
    size_t base;
    struct pa_memchunk chunk;
    uint8_t *buffer;
    size_t buffer_fill;
};

struct pa_mcalign *pa_mcalign_new(size_t base) {
    struct pa_mcalign *m;
    assert(base);

    m = malloc(sizeof(struct pa_mcalign));
    assert(m);
    m->base = base;
    m->chunk.memblock = NULL;
    m->chunk.length = m->chunk.index = 0;
    m->buffer = NULL;
    m->buffer_fill = 0;
    return m;
}

void pa_mcalign_free(struct pa_mcalign *m) {
    assert(m);

    free(m->buffer);
    
    if (m->chunk.memblock)
        pa_memblock_unref(m->chunk.memblock);
    
    free(m);
}

void pa_mcalign_push(struct pa_mcalign *m, const struct pa_memchunk *c) {
    assert(m && c && !m->chunk.memblock && c->memblock && c->length);

    m->chunk = *c;
    pa_memblock_ref(m->chunk.memblock);
}

int pa_mcalign_pop(struct pa_mcalign *m, struct pa_memchunk *c) {
    assert(m && c && m->base > m->buffer_fill);
    int ret;

    if (!m->chunk.memblock)
        return -1;

    if (m->buffer_fill) {
        size_t l = m->base - m->buffer_fill;
        if (l > m->chunk.length)
            l = m->chunk.length;
        assert(m->buffer && l);

        memcpy(m->buffer + m->buffer_fill, m->chunk.memblock->data + m->chunk.index, l);
        m->buffer_fill += l;
        m->chunk.index += l;
        m->chunk.length -= l;

        if (m->chunk.length == 0) {
            m->chunk.length = m->chunk.index = 0;
            pa_memblock_unref(m->chunk.memblock);
            m->chunk.memblock = NULL;
        }

        assert(m->buffer_fill <= m->base);
        if (m->buffer_fill == m->base) {
            c->memblock = pa_memblock_new_dynamic(m->buffer, m->base);
            assert(c->memblock);
            c->index = 0;
            c->length = m->base;
            m->buffer = NULL;
            m->buffer_fill = 0;

            return 0;
        }

        return -1;
    }

    m->buffer_fill = m->chunk.length % m->base;

    if (m->buffer_fill) {
        assert(!m->buffer);
        m->buffer = malloc(m->base);
        assert(m->buffer);
        m->chunk.length -= m->buffer_fill;
        memcpy(m->buffer, m->chunk.memblock->data + m->chunk.index + m->chunk.length, m->buffer_fill);
    }

    if (m->chunk.length) {
        *c = m->chunk;
        pa_memblock_ref(c->memblock);
        ret = 0;
    } else
        ret = -1;
    
    m->chunk.length = m->chunk.index = 0;
    pa_memblock_unref(m->chunk.memblock);
    m->chunk.memblock = NULL;

    return ret;
}