Merge commit 'origin/master-tx'
[platform/upstream/pulseaudio.git] / src / pulsecore / shared.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
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 published
8   by the Free Software Foundation; either version 2.1 of the License,
9   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   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   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 <pulse/xmalloc.h>
27 #include <pulsecore/log.h>
28 #include <pulsecore/macro.h>
29
30 #include "shared.h"
31
32 typedef struct pa_shared {
33     char *name;  /* Points to memory allocated by the shared property system */
34     void *data;  /* Points to memory maintained by the caller */
35 } pa_shared;
36
37 /* Allocate a new shared property object */
38 static pa_shared* shared_new(const char *name, void *data) {
39     pa_shared* p;
40
41     pa_assert(name);
42     pa_assert(data);
43
44     p = pa_xnew(pa_shared, 1);
45     p->name = pa_xstrdup(name);
46     p->data = data;
47
48     return p;
49 }
50
51 /* Free a shared property object */
52 static void shared_free(pa_shared *p) {
53     pa_assert(p);
54
55     pa_xfree(p->name);
56     pa_xfree(p);
57 }
58
59 void* pa_shared_get(pa_core *c, const char *name) {
60     pa_shared *p;
61
62     pa_assert(c);
63     pa_assert(name);
64     pa_assert(c->shared);
65
66     if (!(p = pa_hashmap_get(c->shared, name)))
67         return NULL;
68
69     return p->data;
70 }
71
72 int pa_shared_set(pa_core *c, const char *name, void *data) {
73     pa_shared *p;
74
75     pa_assert(c);
76     pa_assert(name);
77     pa_assert(data);
78     pa_assert(c->shared);
79
80     if (pa_hashmap_get(c->shared, name))
81         return -1;
82
83     p = shared_new(name, data);
84     pa_hashmap_put(c->shared, p->name, p);
85     return 0;
86 }
87
88 int pa_shared_remove(pa_core *c, const char *name) {
89     pa_shared *p;
90
91     pa_assert(c);
92     pa_assert(name);
93     pa_assert(c->shared);
94
95     if (!(p = pa_hashmap_remove(c->shared, name)))
96         return -1;
97
98     shared_free(p);
99     return 0;
100 }
101
102 void pa_shared_dump(pa_core *c, pa_strbuf *s) {
103     void *state = NULL;
104     pa_shared *p;
105
106     pa_assert(c);
107     pa_assert(s);
108
109     while ((p = pa_hashmap_iterate(c->shared, &state, NULL)))
110         pa_strbuf_printf(s, "[%s] -> [%p]\n", p->name, p->data);
111 }
112
113 int pa_shared_replace(pa_core *c, const char *name, void *data) {
114     pa_assert(c);
115     pa_assert(name);
116
117     pa_shared_remove(c, name);
118     return pa_shared_set(c, name, data);
119 }