]> code.delx.au - pulseaudio/blob - src/pulsecore/queue.c
1dd0f6061bebb6e03ab7ed98869b4c71362c1f16
[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 <assert.h>
29 #include <stdlib.h>
30
31 #include <pulse/xmalloc.h>
32
33 #include "queue.h"
34
35 struct queue_entry {
36 struct queue_entry *next;
37 void *data;
38 };
39
40 struct pa_queue {
41 struct queue_entry *front, *back;
42 unsigned length;
43 };
44
45 pa_queue* pa_queue_new(void) {
46 pa_queue *q = pa_xnew(pa_queue, 1);
47 q->front = q->back = NULL;
48 q->length = 0;
49 return q;
50 }
51
52 void pa_queue_free(pa_queue* q, void (*destroy)(void *p, void *userdata), void *userdata) {
53 struct queue_entry *e;
54 assert(q);
55
56 e = q->front;
57 while (e) {
58 struct queue_entry *n = e->next;
59
60 if (destroy)
61 destroy(e->data, userdata);
62
63 pa_xfree(e);
64 e = n;
65 }
66
67 pa_xfree(q);
68 }
69
70 void pa_queue_push(pa_queue *q, void *p) {
71 struct queue_entry *e;
72
73 e = pa_xnew(struct queue_entry, 1);
74 e->data = p;
75 e->next = NULL;
76
77 if (q->back)
78 q->back->next = e;
79 else {
80 assert(!q->front);
81 q->front = e;
82 }
83
84 q->back = e;
85 q->length++;
86 }
87
88 void* pa_queue_pop(pa_queue *q) {
89 void *p;
90 struct queue_entry *e;
91 assert(q);
92
93 if (!(e = q->front))
94 return NULL;
95
96 q->front = e->next;
97 if (q->back == e)
98 q->back = NULL;
99
100 p = e->data;
101 pa_xfree(e);
102
103 q->length--;
104
105 return p;
106 }
107
108 int pa_queue_is_empty(pa_queue *q) {
109 assert(q);
110 return q->length == 0;
111 }