Merge commit 'origin/master-tx'
[platform/upstream/pulseaudio.git] / src / pulsecore / packet.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
8   published by the Free Software Foundation; either version 2.1 of the
9   License, 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   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 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 <stdlib.h>
27
28 #include <pulse/xmalloc.h>
29 #include <pulsecore/macro.h>
30
31 #include "packet.h"
32
33 pa_packet* pa_packet_new(size_t length) {
34     pa_packet *p;
35
36     pa_assert(length > 0);
37
38     p = pa_xmalloc(PA_ALIGN(sizeof(pa_packet)) + length);
39     PA_REFCNT_INIT(p);
40     p->length = length;
41     p->data = (uint8_t*) p + PA_ALIGN(sizeof(pa_packet));
42     p->type = PA_PACKET_APPENDED;
43
44     return p;
45 }
46
47 pa_packet* pa_packet_new_dynamic(void* data, size_t length) {
48     pa_packet *p;
49
50     pa_assert(data);
51     pa_assert(length > 0);
52
53     p = pa_xnew(pa_packet, 1);
54     PA_REFCNT_INIT(p);
55     p->length = length;
56     p->data = data;
57     p->type = PA_PACKET_DYNAMIC;
58
59     return p;
60 }
61
62 pa_packet* pa_packet_ref(pa_packet *p) {
63     pa_assert(p);
64     pa_assert(PA_REFCNT_VALUE(p) >= 1);
65
66     PA_REFCNT_INC(p);
67     return p;
68 }
69
70 void pa_packet_unref(pa_packet *p) {
71     pa_assert(p);
72     pa_assert(PA_REFCNT_VALUE(p) >= 1);
73
74     if (PA_REFCNT_DEC(p) <= 0) {
75         if (p->type == PA_PACKET_DYNAMIC)
76             pa_xfree(p->data);
77         pa_xfree(p);
78     }
79 }