]>
code.delx.au - pulseaudio/blob - polyp/memchunk.c
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
33 void pa_memchunk_make_writable(struct pa_memchunk
*c
) {
34 struct pa_memblock
*n
;
35 assert(c
&& c
->memblock
&& c
->memblock
->ref
>= 1);
37 if (c
->memblock
->ref
== 1)
40 n
= pa_memblock_new(c
->length
);
42 memcpy(n
->data
, c
->memblock
->data
+c
->index
, c
->length
);
43 pa_memblock_unref(c
->memblock
);
51 struct pa_memchunk chunk
;
56 struct pa_mcalign
*pa_mcalign_new(size_t base
) {
60 m
= malloc(sizeof(struct pa_mcalign
));
63 m
->chunk
.memblock
= NULL
;
64 m
->chunk
.length
= m
->chunk
.index
= 0;
70 void pa_mcalign_free(struct pa_mcalign
*m
) {
75 if (m
->chunk
.memblock
)
76 pa_memblock_unref(m
->chunk
.memblock
);
81 void pa_mcalign_push(struct pa_mcalign
*m
, const struct pa_memchunk
*c
) {
82 assert(m
&& c
&& !m
->chunk
.memblock
&& c
->memblock
&& c
->length
);
85 pa_memblock_ref(m
->chunk
.memblock
);
88 int pa_mcalign_pop(struct pa_mcalign
*m
, struct pa_memchunk
*c
) {
89 assert(m
&& c
&& m
->base
> m
->buffer_fill
);
92 if (!m
->chunk
.memblock
)
96 size_t l
= m
->base
- m
->buffer_fill
;
97 if (l
> m
->chunk
.length
)
99 assert(m
->buffer
&& l
);
101 memcpy(m
->buffer
+ m
->buffer_fill
, m
->chunk
.memblock
->data
+ m
->chunk
.index
, l
);
104 m
->chunk
.length
-= l
;
106 if (m
->chunk
.length
== 0) {
107 m
->chunk
.length
= m
->chunk
.index
= 0;
108 pa_memblock_unref(m
->chunk
.memblock
);
109 m
->chunk
.memblock
= NULL
;
112 assert(m
->buffer_fill
<= m
->base
);
113 if (m
->buffer_fill
== m
->base
) {
114 c
->memblock
= pa_memblock_new_dynamic(m
->buffer
, m
->base
);
127 m
->buffer_fill
= m
->chunk
.length
% m
->base
;
129 if (m
->buffer_fill
) {
131 m
->buffer
= malloc(m
->base
);
133 m
->chunk
.length
-= m
->buffer_fill
;
134 memcpy(m
->buffer
, m
->chunk
.memblock
->data
+ m
->chunk
.index
+ m
->chunk
.length
, m
->buffer_fill
);
137 if (m
->chunk
.length
) {
139 pa_memblock_ref(c
->memblock
);
144 m
->chunk
.length
= m
->chunk
.index
= 0;
145 pa_memblock_unref(m
->chunk
.memblock
);
146 m
->chunk
.memblock
= NULL
;