]> code.delx.au - pulseaudio/blob - src/polypcore/memblock.c
Cleaned up the includes after the restructuring. Indicate which headers are
[pulseaudio] / src / polypcore / memblock.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
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 <polypcore/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 && b->ref >= 1);
115 b->ref++;
116 return b;
117 }
118
119 void pa_memblock_unref(pa_memblock*b) {
120 assert(b && b->ref >= 1);
121
122 if ((--(b->ref)) == 0) {
123 stat_remove(b);
124
125 if (b->type == PA_MEMBLOCK_USER) {
126 assert(b->free_cb);
127 b->free_cb(b->data);
128 } else if (b->type == PA_MEMBLOCK_DYNAMIC)
129 pa_xfree(b->data);
130
131 pa_xfree(b);
132 }
133 }
134
135 void pa_memblock_unref_fixed(pa_memblock *b) {
136 assert(b && b->ref >= 1 && b->type == PA_MEMBLOCK_FIXED);
137
138 if (b->ref == 1)
139 pa_memblock_unref(b);
140 else {
141 b->data = pa_xmemdup(b->data, b->length);
142 b->type = PA_MEMBLOCK_DYNAMIC;
143 b->ref--;
144 }
145 }
146
147 pa_memblock_stat* pa_memblock_stat_new(void) {
148 pa_memblock_stat *s;
149
150 s = pa_xmalloc(sizeof(pa_memblock_stat));
151 s->ref = 1;
152 s->total = s->total_size = s->allocated = s->allocated_size = 0;
153
154 return s;
155 }
156
157 void pa_memblock_stat_unref(pa_memblock_stat *s) {
158 assert(s && s->ref >= 1);
159
160 if (!(--(s->ref))) {
161 assert(!s->total);
162 pa_xfree(s);
163 }
164 }
165
166 pa_memblock_stat * pa_memblock_stat_ref(pa_memblock_stat *s) {
167 assert(s);
168 s->ref++;
169 return s;
170 }