Turn device ports into reference counted objects
[platform/upstream/pulseaudio.git] / src / pulsecore / device-port.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
5   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6   Copyright 2011 David Henningsson, Canonical Ltd.
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2.1 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24
25 #include "device-port.h"
26
27 PA_DEFINE_PUBLIC_CLASS(pa_device_port, pa_object);
28
29 static void device_port_free(pa_object *o) {
30     pa_device_port *p = PA_DEVICE_PORT(o);
31
32     pa_assert(p);
33     pa_assert(pa_device_port_refcnt(p) == 0);
34
35     pa_xfree(p->name);
36     pa_xfree(p->description);
37     pa_xfree(p);
38 }
39
40
41 pa_device_port *pa_device_port_new(const char *name, const char *description, size_t extra) {
42     pa_device_port *p;
43
44     pa_assert(name);
45
46     p = PA_DEVICE_PORT(pa_object_new_internal(PA_ALIGN(sizeof(pa_device_port)) + extra, pa_device_port_type_id, pa_device_port_check_type));
47     p->parent.free = device_port_free;
48
49     p->name = pa_xstrdup(name);
50     p->description = pa_xstrdup(description);
51     p->priority = 0;
52     p->available = PA_PORT_AVAILABLE_UNKNOWN;
53
54     return p;
55 }
56
57 void pa_device_port_hashmap_free(pa_hashmap *h) {
58     pa_device_port *p;
59
60     pa_assert(h);
61
62     while ((p = pa_hashmap_steal_first(h)))
63         pa_device_port_unref(p);
64
65     pa_hashmap_free(h, NULL, NULL);
66 }