]> code.delx.au - pulseaudio/blob - src/pulsecore/memblock.c
big s/polyp/pulse/g
[pulseaudio] / src / pulsecore / memblock.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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.
10
11 PulseAudio 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.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <string.h>
30
31 #include <pulse/xmalloc.h>
32
33 #include "memblock.h"
34
35 static void stat_add(pa_memblock*m, pa_memblock_stat *s) {
36 assert(m);
37
38 if (!s) {
39 m->stat = NULL;
40 return;
41 }
42
43 m->stat = pa_memblock_stat_ref(s);
44 s->total++;
45 s->allocated++;
46 s->total_size += m->length;
47 s->allocated_size += m->length;
48 }
49
50 static void stat_remove(pa_memblock *m) {
51 assert(m);
52
53 if (!m->stat)
54 return;
55
56 m->stat->total--;
57 m->stat->total_size -= m->length;
58
59 pa_memblock_stat_unref(m->stat);
60 m->stat = NULL;
61 }
62
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;
66 b->ref = 1;
67 b->length = length;
68 b->data = b+1;
69 b->free_cb = NULL;
70 b->read_only = 0;
71 stat_add(b, s);
72 return b;
73 }
74
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;
78 b->ref = 1;
79 b->length = length;
80 b->data = d;
81 b->free_cb = NULL;
82 b->read_only = 0;
83 stat_add(b, s);
84 return b;
85 }
86
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;
90 b->ref = 1;
91 b->length = length;
92 b->data = d;
93 b->free_cb = NULL;
94 b->read_only = read_only;
95 stat_add(b, s);
96 return b;
97 }
98
99 pa_memblock *pa_memblock_new_user(void *d, size_t length, void (*free_cb)(void *p), int read_only, pa_memblock_stat*s) {
100 pa_memblock *b;
101 assert(d && length && free_cb);
102 b = pa_xmalloc(sizeof(pa_memblock));
103 b->type = PA_MEMBLOCK_USER;
104 b->ref = 1;
105 b->length = length;
106 b->data = d;
107 b->free_cb = free_cb;
108 b->read_only = read_only;
109 stat_add(b, s);
110 return b;
111 }
112
113 pa_memblock* pa_memblock_ref(pa_memblock*b) {
114 assert(b);
115 assert(b->ref >= 1);
116
117 b->ref++;
118 return b;
119 }
120
121 void pa_memblock_unref(pa_memblock*b) {
122 assert(b);
123 assert(b->ref >= 1);
124
125 if ((--(b->ref)) == 0) {
126 stat_remove(b);
127
128 if (b->type == PA_MEMBLOCK_USER) {
129 assert(b->free_cb);
130 b->free_cb(b->data);
131 } else if (b->type == PA_MEMBLOCK_DYNAMIC)
132 pa_xfree(b->data);
133
134 pa_xfree(b);
135 }
136 }
137
138 void pa_memblock_unref_fixed(pa_memblock *b) {
139 assert(b && b->ref >= 1 && b->type == PA_MEMBLOCK_FIXED);
140
141 if (b->ref == 1)
142 pa_memblock_unref(b);
143 else {
144 b->data = pa_xmemdup(b->data, b->length);
145 b->type = PA_MEMBLOCK_DYNAMIC;
146 b->ref--;
147 }
148 }
149
150 pa_memblock_stat* pa_memblock_stat_new(void) {
151 pa_memblock_stat *s;
152
153 s = pa_xmalloc(sizeof(pa_memblock_stat));
154 s->ref = 1;
155 s->total = s->total_size = s->allocated = s->allocated_size = 0;
156
157 return s;
158 }
159
160 void pa_memblock_stat_unref(pa_memblock_stat *s) {
161 assert(s && s->ref >= 1);
162
163 if (!(--(s->ref))) {
164 assert(!s->total);
165 pa_xfree(s);
166 }
167 }
168
169 pa_memblock_stat * pa_memblock_stat_ref(pa_memblock_stat *s) {
170 assert(s);
171 s->ref++;
172 return s;
173 }