rename configuration file
[profile/ivi/pulseaudio.git] / src / 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 General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   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   General Public License for more details.
15  
16   You should have received a copy of the GNU General Public License
17   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
33 static unsigned memblock_count = 0, memblock_total = 0;
34
35 struct pa_memblock *pa_memblock_new(size_t length) {
36     struct pa_memblock *b = malloc(sizeof(struct pa_memblock)+length);
37     b->type = PA_MEMBLOCK_APPENDED;
38     b->ref = 1;
39     b->length = length;
40     b->data = b+1;
41     memblock_count++;
42     memblock_total += length;
43     return b;
44 }
45
46 struct pa_memblock *pa_memblock_new_fixed(void *d, size_t length) {
47     struct pa_memblock *b = malloc(sizeof(struct pa_memblock));
48     b->type = PA_MEMBLOCK_FIXED;
49     b->ref = 1;
50     b->length = length;
51     b->data = d;
52     memblock_count++;
53     memblock_total += length;
54     return b;
55 }
56
57 struct pa_memblock *pa_memblock_new_dynamic(void *d, size_t length) {
58     struct pa_memblock *b = malloc(sizeof(struct pa_memblock));
59     b->type = PA_MEMBLOCK_DYNAMIC;
60     b->ref = 1;
61     b->length = length;
62     b->data = d;
63     memblock_count++;
64     memblock_total += length;
65     return b;
66 }
67
68 struct pa_memblock* pa_memblock_ref(struct pa_memblock*b) {
69     assert(b && b->ref >= 1);
70     b->ref++;
71     return b;
72 }
73
74 void pa_memblock_unref(struct pa_memblock*b) {
75     assert(b && b->ref >= 1);
76     b->ref--;
77
78     if (b->ref == 0) {
79         if (b->type == PA_MEMBLOCK_DYNAMIC)
80             free(b->data);
81
82         memblock_count--;
83         memblock_total -= b->length;
84
85         free(b);
86     }
87 }
88
89 void pa_memblock_unref_fixed(struct pa_memblock *b) {
90     void *d;
91     
92     assert(b && b->ref >= 1);
93
94     if (b->ref == 1) {
95         pa_memblock_unref(b);
96         return;
97     } else {
98         d = malloc(b->length);
99         assert(d);
100         memcpy(d, b->data, b->length);
101         b->data = d;
102         b->type = PA_MEMBLOCK_DYNAMIC;
103         b->ref--;
104     }
105 }
106
107 unsigned pa_memblock_get_count(void) {
108     return memblock_count;
109 }
110
111 unsigned pa_memblock_get_total(void) {
112     return memblock_total;
113 }