Add default-monitor-time-sec
[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, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25
26 #include <pulse/xmalloc.h>
27 #include <pulsecore/macro.h>
28 #include <pulsecore/refcnt.h>
29 #include <pulsecore/flist.h>
30
31 #include "packet.h"
32
33 #define MAX_APPENDED_SIZE 128
34
35 struct pa_packet {
36     PA_REFCNT_DECLARE;
37     enum { PA_PACKET_APPENDED, PA_PACKET_DYNAMIC } type;
38     size_t length;
39     uint8_t *data;
40     union {
41         uint8_t appended[MAX_APPENDED_SIZE];
42     } per_type;
43 };
44
45 PA_STATIC_FLIST_DECLARE(packets, 0, pa_xfree);
46
47 pa_packet* pa_packet_new(size_t length) {
48     pa_packet *p;
49
50     pa_assert(length > 0);
51
52     if (!(p = pa_flist_pop(PA_STATIC_FLIST_GET(packets))))
53         p = pa_xnew(pa_packet, 1);
54     PA_REFCNT_INIT(p);
55     p->length = length;
56     if (length > MAX_APPENDED_SIZE) {
57         p->data = pa_xmalloc(length);
58         p->type = PA_PACKET_DYNAMIC;
59     } else {
60         p->data = p->per_type.appended;
61         p->type = PA_PACKET_APPENDED;
62     }
63
64     return p;
65 }
66
67 pa_packet* pa_packet_new_data(const void* data, size_t length) {
68     pa_packet *p = pa_packet_new(length);
69
70     pa_assert(data);
71     pa_assert(length > 0);
72
73     memcpy(p->data, data, length);
74
75     return p;
76 }
77
78 pa_packet* pa_packet_new_dynamic(void* data, size_t length) {
79     pa_packet *p;
80
81     pa_assert(data);
82     pa_assert(length > 0);
83
84     if (!(p = pa_flist_pop(PA_STATIC_FLIST_GET(packets))))
85         p = pa_xnew(pa_packet, 1);
86     PA_REFCNT_INIT(p);
87     p->length = length;
88     p->data = data;
89     p->type = PA_PACKET_DYNAMIC;
90
91     return p;
92 }
93
94 const void* pa_packet_data(pa_packet *p, size_t *l) {
95     pa_assert(PA_REFCNT_VALUE(p) >= 1);
96     pa_assert(p->data);
97     pa_assert(l);
98
99     *l = p->length;
100
101     return p->data;
102 }
103
104 pa_packet* pa_packet_ref(pa_packet *p) {
105     pa_assert(p);
106     pa_assert(PA_REFCNT_VALUE(p) >= 1);
107
108     PA_REFCNT_INC(p);
109     return p;
110 }
111
112 void pa_packet_unref(pa_packet *p) {
113     pa_assert(p);
114     pa_assert(PA_REFCNT_VALUE(p) >= 1);
115
116     if (PA_REFCNT_DEC(p) <= 0) {
117         if (p->type == PA_PACKET_DYNAMIC)
118             pa_xfree(p->data);
119         if (pa_flist_push(PA_STATIC_FLIST_GET(packets), p) < 0)
120             pa_xfree(p);
121     }
122 }