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, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <pulse/xmalloc.h>
25#include <pulsecore/macro.h>
26
27#include "shared.h"
28
29typedef struct pa_shared {
30    char *name;  /* Points to memory allocated by the shared property system */
31    void *data;  /* Points to memory maintained by the caller */
32} pa_shared;
33
34/* Allocate a new shared property object */
35static pa_shared* shared_new(const char *name, void *data) {
36    pa_shared* p;
37
38    pa_assert(name);
39    pa_assert(data);
40
41    p = pa_xnew(pa_shared, 1);
42    p->name = pa_xstrdup(name);
43    p->data = data;
44
45    return p;
46}
47
48/* Free a shared property object */
49static void shared_free(pa_shared *p) {
50    pa_assert(p);
51
52    pa_xfree(p->name);
53    pa_xfree(p);
54}
55
56void* pa_shared_get(pa_core *c, const char *name) {
57    pa_shared *p;
58
59    pa_assert(c);
60    pa_assert(name);
61    pa_assert(c->shared);
62
63    if (!(p = pa_hashmap_get(c->shared, name)))
64        return NULL;
65
66    return p->data;
67}
68
69int pa_shared_set(pa_core *c, const char *name, void *data) {
70    pa_shared *p;
71
72    pa_assert(c);
73    pa_assert(name);
74    pa_assert(data);
75    pa_assert(c->shared);
76
77    if (pa_hashmap_get(c->shared, name))
78        return -1;
79
80    p = shared_new(name, data);
81    pa_hashmap_put(c->shared, p->name, p);
82    return 0;
83}
84
85int pa_shared_remove(pa_core *c, const char *name) {
86    pa_shared *p;
87
88    pa_assert(c);
89    pa_assert(name);
90    pa_assert(c->shared);
91
92    if (!(p = pa_hashmap_remove(c->shared, name)))
93        return -1;
94
95    shared_free(p);
96    return 0;
97}
98
99void pa_shared_dump(pa_core *c, pa_strbuf *s) {
100    void *state = NULL;
101    pa_shared *p;
102
103    pa_assert(c);
104    pa_assert(s);
105
106    while ((p = pa_hashmap_iterate(c->shared, &state, NULL)))
107        pa_strbuf_printf(s, "[%s] -> [%p]\n", p->name, p->data);
108}
109
110int pa_shared_replace(pa_core *c, const char *name, void *data) {
111    pa_assert(c);
112    pa_assert(name);
113
114    (void) pa_shared_remove(c, name);
115    return pa_shared_set(c, name, data);
116}
117