8da5352527d12d43a9609992a146f87f9a21c312
[profile/ivi/pulseaudio.git] / 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 "memblock.h"
32 #include "xmalloc.h"
33
34 static void stat_add(pa_memblock*m, pa_memblock_stat *s) {
35     assert(m);
36
37     if (!s) {
38         m->stat = NULL;
39         return;
40     }
41
42     m->stat = pa_memblock_stat_ref(s);
43     s->total++;
44     s->allocated++;
45     s->total_size += m->length;
46     s->allocated_size += m->length;
47 }
48
49 static void stat_remove(pa_memblock *m) {
50     assert(m);
51
52     if (!m->stat)
53         return;
54
55     m->stat->total--;
56     m->stat->total_size -= m->length;
57     
58     pa_memblock_stat_unref(m->stat);
59     m->stat = NULL;
60 }
61
62 pa_memblock *pa_memblock_new(size_t length, pa_memblock_stat*s) {
63     pa_memblock *b = pa_xmalloc(sizeof(pa_memblock)+length);
64     b->type = PA_MEMBLOCK_APPENDED;
65     b->ref = 1;
66     b->length = length;
67     b->data = b+1;
68     b->free_cb = NULL;
69     b->read_only = 0;
70     stat_add(b, s);
71     return b;
72 }
73
74 pa_memblock *pa_memblock_new_dynamic(void *d, size_t length, pa_memblock_stat*s) {
75     pa_memblock *b = pa_xmalloc(sizeof(pa_memblock));
76     b->type = PA_MEMBLOCK_DYNAMIC;
77     b->ref = 1;
78     b->length = length;
79     b->data = d;
80     b->free_cb = NULL;
81     b->read_only = 0;
82     stat_add(b, s);
83     return b;
84 }
85
86 pa_memblock *pa_memblock_new_fixed(void *d, size_t length, int read_only, pa_memblock_stat*s) {
87     pa_memblock *b = pa_xmalloc(sizeof(pa_memblock));
88     b->type = PA_MEMBLOCK_FIXED;
89     b->ref = 1;
90     b->length = length;
91     b->data = d;
92     b->free_cb = NULL;
93     b->read_only = read_only;
94     stat_add(b, s);
95     return b;
96 }
97
98 pa_memblock *pa_memblock_new_user(void *d, size_t length, void (*free_cb)(void *p), int read_only, pa_memblock_stat*s) {
99     pa_memblock *b;
100     assert(d && length && free_cb);
101     b = pa_xmalloc(sizeof(pa_memblock));
102     b->type = PA_MEMBLOCK_USER;
103     b->ref = 1;
104     b->length = length;
105     b->data = d;
106     b->free_cb = free_cb;
107     b->read_only = read_only;
108     stat_add(b, s);
109     return b;
110 }
111
112 pa_memblock* pa_memblock_ref(pa_memblock*b) {
113     assert(b && b->ref >= 1);
114     b->ref++;
115     return b;
116 }
117
118 void pa_memblock_unref(pa_memblock*b) {
119     assert(b && b->ref >= 1);
120
121     if ((--(b->ref)) == 0) {
122         stat_remove(b);
123
124         if (b->type == PA_MEMBLOCK_USER) {
125             assert(b->free_cb);
126             b->free_cb(b->data);
127         } else if (b->type == PA_MEMBLOCK_DYNAMIC)
128             pa_xfree(b->data);
129
130         pa_xfree(b);
131     }
132 }
133
134 void pa_memblock_unref_fixed(pa_memblock *b) {
135     assert(b && b->ref >= 1 && b->type == PA_MEMBLOCK_FIXED);
136
137     if (b->ref == 1)
138         pa_memblock_unref(b);
139     else {
140         b->data = pa_xmemdup(b->data, b->length);
141         b->type = PA_MEMBLOCK_DYNAMIC;
142         b->ref--;
143     }
144 }
145
146 pa_memblock_stat* pa_memblock_stat_new(void) {
147     pa_memblock_stat *s;
148
149     s = pa_xmalloc(sizeof(pa_memblock_stat));
150     s->ref = 1;
151     s->total = s->total_size = s->allocated = s->allocated_size = 0;
152
153     return s;
154 }
155
156 void pa_memblock_stat_unref(pa_memblock_stat *s) {
157     assert(s && s->ref >= 1);
158
159     if (!(--(s->ref))) {
160         assert(!s->total);
161         pa_xfree(s);
162     }
163 }
164
165 pa_memblock_stat * pa_memblock_stat_ref(pa_memblock_stat *s) {
166     assert(s);
167     s->ref++;
168     return s;
169 }