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