]> code.delx.au - pulseaudio/blob - src/pulsecore/queue.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / queue.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
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
10 published by the Free Software Foundation; either version 2.1 of the
11 License, 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 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License 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 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/flist.h>
33
34 #include "queue.h"
35
36 PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
37
38 struct queue_entry {
39 struct queue_entry *next;
40 void *data;
41 };
42
43 struct pa_queue {
44 struct queue_entry *front, *back;
45 unsigned length;
46 };
47
48 pa_queue* pa_queue_new(void) {
49 pa_queue *q = pa_xnew(pa_queue, 1);
50
51 q->front = q->back = NULL;
52 q->length = 0;
53
54 return q;
55 }
56
57 void pa_queue_free(pa_queue* q, void (*destroy)(void *p, void *userdata), void *userdata) {
58 void *data;
59 pa_assert(q);
60
61 while ((data = pa_queue_pop(q)))
62 if (destroy)
63 destroy(data, userdata);
64
65 pa_assert(!q->front);
66 pa_assert(!q->back);
67 pa_assert(q->length == 0);
68
69 pa_xfree(q);
70 }
71
72 void pa_queue_push(pa_queue *q, void *p) {
73 struct queue_entry *e;
74
75 pa_assert(q);
76 pa_assert(p);
77
78 if (!(e = pa_flist_pop(PA_STATIC_FLIST_GET(entries))))
79 e = pa_xnew(struct queue_entry, 1);
80
81 e->data = p;
82 e->next = NULL;
83
84 if (q->back) {
85 pa_assert(q->front);
86 q->back->next = e;
87 } else {
88 pa_assert(!q->front);
89 q->front = e;
90 }
91
92 q->back = e;
93 q->length++;
94 }
95
96 void* pa_queue_pop(pa_queue *q) {
97 void *p;
98 struct queue_entry *e;
99 pa_assert(q);
100
101 if (!(e = q->front))
102 return NULL;
103
104 q->front = e->next;
105
106 if (q->back == e) {
107 pa_assert(!e->next);
108 q->back = NULL;
109 }
110
111 p = e->data;
112
113 if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
114 pa_xfree(e);
115
116 q->length--;
117
118 return p;
119 }
120
121 int pa_queue_is_empty(pa_queue *q) {
122 pa_assert(q);
123
124 return q->length == 0;
125 }