]>
code.delx.au - pulseaudio/blob - src/polypcore/memblock.c
2c0bef57c4ad53bd316a4f86673c1b7e0df47e52
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 Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <polypcore/xmalloc.h>
35 static void stat_add(pa_memblock
*m
, pa_memblock_stat
*s
) {
43 m
->stat
= pa_memblock_stat_ref(s
);
46 s
->total_size
+= m
->length
;
47 s
->allocated_size
+= m
->length
;
50 static void stat_remove(pa_memblock
*m
) {
57 m
->stat
->total_size
-= m
->length
;
59 pa_memblock_stat_unref(m
->stat
);
63 pa_memblock
*pa_memblock_new(size_t length
, pa_memblock_stat
*s
) {
64 pa_memblock
*b
= pa_xmalloc(sizeof(pa_memblock
)+length
);
65 b
->type
= PA_MEMBLOCK_APPENDED
;
75 pa_memblock
*pa_memblock_new_dynamic(void *d
, size_t length
, pa_memblock_stat
*s
) {
76 pa_memblock
*b
= pa_xmalloc(sizeof(pa_memblock
));
77 b
->type
= PA_MEMBLOCK_DYNAMIC
;
87 pa_memblock
*pa_memblock_new_fixed(void *d
, size_t length
, int read_only
, pa_memblock_stat
*s
) {
88 pa_memblock
*b
= pa_xmalloc(sizeof(pa_memblock
));
89 b
->type
= PA_MEMBLOCK_FIXED
;
94 b
->read_only
= read_only
;
99 pa_memblock
*pa_memblock_new_user(void *d
, size_t length
, void (*free_cb
)(void *p
), int read_only
, pa_memblock_stat
*s
) {
101 assert(d
&& length
&& free_cb
);
102 b
= pa_xmalloc(sizeof(pa_memblock
));
103 b
->type
= PA_MEMBLOCK_USER
;
107 b
->free_cb
= free_cb
;
108 b
->read_only
= read_only
;
113 pa_memblock
* pa_memblock_ref(pa_memblock
*b
) {
114 assert(b
&& b
->ref
>= 1);
119 void pa_memblock_unref(pa_memblock
*b
) {
120 assert(b
&& b
->ref
>= 1);
122 if ((--(b
->ref
)) == 0) {
125 if (b
->type
== PA_MEMBLOCK_USER
) {
128 } else if (b
->type
== PA_MEMBLOCK_DYNAMIC
)
135 void pa_memblock_unref_fixed(pa_memblock
*b
) {
136 assert(b
&& b
->ref
>= 1 && b
->type
== PA_MEMBLOCK_FIXED
);
139 pa_memblock_unref(b
);
141 b
->data
= pa_xmemdup(b
->data
, b
->length
);
142 b
->type
= PA_MEMBLOCK_DYNAMIC
;
147 pa_memblock_stat
* pa_memblock_stat_new(void) {
150 s
= pa_xmalloc(sizeof(pa_memblock_stat
));
152 s
->total
= s
->total_size
= s
->allocated
= s
->allocated_size
= 0;
157 void pa_memblock_stat_unref(pa_memblock_stat
*s
) {
158 assert(s
&& s
->ref
>= 1);
166 pa_memblock_stat
* pa_memblock_stat_ref(pa_memblock_stat
*s
) {