]> code.delx.au - pulseaudio/blob - polyp/memblock.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / 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 "memblock.h"
32 #include "xmalloc.h"
33
34 static void stat_add(struct pa_memblock*m, struct pa_memblock_stat *s) {
35 assert(m);
36
37 m->stat = pa_memblock_stat_ref(s);
38 s->total++;
39 s->allocated++;
40 s->total_size += m->length;
41 s->allocated_size += m->length;
42 }
43
44 static void stat_remove(struct pa_memblock *m) {
45 assert(m);
46
47 if (!m->stat)
48 return;
49
50 m->stat->total--;
51 m->stat->total_size -= m->length;
52
53 pa_memblock_stat_unref(m->stat);
54 m->stat = NULL;
55 }
56
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;
60 b->ref = 1;
61 b->length = length;
62 b->data = b+1;
63 b->free_cb = NULL;
64 stat_add(b, s);
65 return b;
66 }
67
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;
71 b->ref = 1;
72 b->length = length;
73 b->data = d;
74 b->free_cb = NULL;
75 stat_add(b, s);
76 return b;
77 }
78
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;
82 b->ref = 1;
83 b->length = length;
84 b->data = d;
85 b->free_cb = NULL;
86 stat_add(b, s);
87 return b;
88 }
89
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;
95 b->ref = 1;
96 b->length = length;
97 b->data = d;
98 b->free_cb = free_cb;
99 stat_add(b, s);
100 return b;
101 }
102
103 struct pa_memblock* pa_memblock_ref(struct pa_memblock*b) {
104 assert(b && b->ref >= 1);
105 b->ref++;
106 return b;
107 }
108
109 void pa_memblock_unref(struct pa_memblock*b) {
110 assert(b && b->ref >= 1);
111
112 if ((--(b->ref)) == 0) {
113 stat_remove(b);
114
115 if (b->type == PA_MEMBLOCK_USER) {
116 assert(b->free_cb);
117 b->free_cb(b->data);
118 } else if (b->type == PA_MEMBLOCK_DYNAMIC)
119 pa_xfree(b->data);
120
121 pa_xfree(b);
122 }
123 }
124
125 void pa_memblock_unref_fixed(struct pa_memblock *b) {
126 assert(b && b->ref >= 1 && b->type == PA_MEMBLOCK_FIXED);
127
128 if (b->ref == 1)
129 pa_memblock_unref(b);
130 else {
131 b->data = pa_xmemdup(b->data, b->length);
132 b->type = PA_MEMBLOCK_DYNAMIC;
133 b->ref--;
134 }
135 }
136
137 struct pa_memblock_stat* pa_memblock_stat_new(void) {
138 struct pa_memblock_stat *s;
139
140 s = pa_xmalloc(sizeof(struct pa_memblock_stat));
141 s->ref = 1;
142 s->total = s->total_size = s->allocated = s->allocated_size = 0;
143
144 return s;
145 }
146
147 void pa_memblock_stat_unref(struct pa_memblock_stat *s) {
148 assert(s && s->ref >= 1);
149
150 if (!(--(s->ref))) {
151 assert(!s->total);
152 pa_xfree(s);
153 }
154 }
155
156 struct pa_memblock_stat * pa_memblock_stat_ref(struct pa_memblock_stat *s) {
157 assert(s);
158 s->ref++;
159 return s;
160 }