]>
code.delx.au - pulseaudio/blob - polyp/memblock.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
34 static void stat_add(struct pa_memblock
*m
, struct pa_memblock_stat
*s
) {
37 m
->stat
= pa_memblock_stat_ref(s
);
40 s
->total_size
+= m
->length
;
41 s
->allocated_size
+= m
->length
;
44 static void stat_remove(struct pa_memblock
*m
) {
51 m
->stat
->total_size
-= m
->length
;
53 pa_memblock_stat_unref(m
->stat
);
57 struct pa_memblock
*pa_memblock_new(size_t length
, struct pa_memblock_stat
*s
) {
58 struct pa_memblock
*b
= pa_xmalloc(sizeof(struct pa_memblock
)+length
);
59 b
->type
= PA_MEMBLOCK_APPENDED
;
68 struct pa_memblock
*pa_memblock_new_fixed(void *d
, size_t length
, struct pa_memblock_stat
*s
) {
69 struct pa_memblock
*b
= pa_xmalloc(sizeof(struct pa_memblock
));
70 b
->type
= PA_MEMBLOCK_FIXED
;
79 struct pa_memblock
*pa_memblock_new_dynamic(void *d
, size_t length
, struct pa_memblock_stat
*s
) {
80 struct pa_memblock
*b
= pa_xmalloc(sizeof(struct pa_memblock
));
81 b
->type
= PA_MEMBLOCK_DYNAMIC
;
90 struct pa_memblock
*pa_memblock_new_user(void *d
, size_t length
, void (*free_cb
)(void *p
), struct pa_memblock_stat
*s
) {
91 struct pa_memblock
*b
;
92 assert(d
&& length
&& free_cb
);
93 b
= pa_xmalloc(sizeof(struct pa_memblock
));
94 b
->type
= PA_MEMBLOCK_USER
;
103 struct pa_memblock
* pa_memblock_ref(struct pa_memblock
*b
) {
104 assert(b
&& b
->ref
>= 1);
109 void pa_memblock_unref(struct pa_memblock
*b
) {
110 assert(b
&& b
->ref
>= 1);
112 if ((--(b
->ref
)) == 0) {
115 if (b
->type
== PA_MEMBLOCK_USER
) {
118 } else if (b
->type
== PA_MEMBLOCK_DYNAMIC
)
125 void pa_memblock_unref_fixed(struct pa_memblock
*b
) {
126 assert(b
&& b
->ref
>= 1 && b
->type
== PA_MEMBLOCK_FIXED
);
129 pa_memblock_unref(b
);
131 b
->data
= pa_xmemdup(b
->data
, b
->length
);
132 b
->type
= PA_MEMBLOCK_DYNAMIC
;
137 struct pa_memblock_stat
* pa_memblock_stat_new(void) {
138 struct pa_memblock_stat
*s
;
140 s
= pa_xmalloc(sizeof(struct pa_memblock_stat
));
142 s
->total
= s
->total_size
= s
->allocated
= s
->allocated_size
= 0;
147 void pa_memblock_stat_unref(struct pa_memblock_stat
*s
) {
148 assert(s
&& s
->ref
>= 1);
156 struct pa_memblock_stat
* pa_memblock_stat_ref(struct pa_memblock_stat
*s
) {