]>
code.delx.au - pulseaudio/blob - polyp/memchunk.c
920189e28f72f7557e9bd20a907b45120fc462b1
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
34 void pa_memchunk_make_writable(struct pa_memchunk
*c
, struct pa_memblock_stat
*s
) {
35 struct pa_memblock
*n
;
36 assert(c
&& c
->memblock
&& c
->memblock
->ref
>= 1);
38 if (c
->memblock
->ref
== 1)
41 n
= pa_memblock_new(c
->length
, s
);
43 memcpy(n
->data
, c
->memblock
->data
+c
->index
, c
->length
);
44 pa_memblock_unref(c
->memblock
);
52 struct pa_memchunk chunk
;
55 struct pa_memblock_stat
*memblock_stat
;
58 struct pa_mcalign
*pa_mcalign_new(size_t base
, struct pa_memblock_stat
*s
) {
62 m
= pa_xmalloc(sizeof(struct pa_mcalign
));
64 m
->chunk
.memblock
= NULL
;
65 m
->chunk
.length
= m
->chunk
.index
= 0;
72 void pa_mcalign_free(struct pa_mcalign
*m
) {
77 if (m
->chunk
.memblock
)
78 pa_memblock_unref(m
->chunk
.memblock
);
83 void pa_mcalign_push(struct pa_mcalign
*m
, const struct pa_memchunk
*c
) {
84 assert(m
&& c
&& !m
->chunk
.memblock
&& c
->memblock
&& c
->length
);
87 pa_memblock_ref(m
->chunk
.memblock
);
90 int pa_mcalign_pop(struct pa_mcalign
*m
, struct pa_memchunk
*c
) {
92 assert(m
&& c
&& m
->base
> m
->buffer_fill
);
94 if (!m
->chunk
.memblock
)
98 size_t l
= m
->base
- m
->buffer_fill
;
99 if (l
> m
->chunk
.length
)
101 assert(m
->buffer
&& l
);
103 memcpy(m
->buffer
+ m
->buffer_fill
, m
->chunk
.memblock
->data
+ m
->chunk
.index
, l
);
106 m
->chunk
.length
-= l
;
108 if (m
->chunk
.length
== 0) {
109 m
->chunk
.length
= m
->chunk
.index
= 0;
110 pa_memblock_unref(m
->chunk
.memblock
);
111 m
->chunk
.memblock
= NULL
;
114 assert(m
->buffer_fill
<= m
->base
);
115 if (m
->buffer_fill
== m
->base
) {
116 c
->memblock
= pa_memblock_new_dynamic(m
->buffer
, m
->base
, m
->memblock_stat
);
129 m
->buffer_fill
= m
->chunk
.length
% m
->base
;
131 if (m
->buffer_fill
) {
133 m
->buffer
= pa_xmalloc(m
->base
);
134 m
->chunk
.length
-= m
->buffer_fill
;
135 memcpy(m
->buffer
, m
->chunk
.memblock
->data
+ m
->chunk
.index
+ m
->chunk
.length
, m
->buffer_fill
);
138 if (m
->chunk
.length
) {
140 pa_memblock_ref(c
->memblock
);
145 m
->chunk
.length
= m
->chunk
.index
= 0;
146 pa_memblock_unref(m
->chunk
.memblock
);
147 m
->chunk
.memblock
= NULL
;