Devin Anderson jackmp-fix-ffado-midi-2.diff patch.
[jack2.git] / linux / alsa / jslist.h
blobcc145f073e9803ec98544810463c80466604f841
1 /*
2 Based on gslist.c from glib-1.2.9 (LGPL).
4 Adaption to JACK, Copyright (C) 2002 Kai Vehmanen.
5 - replaced use of gtypes with normal ANSI C types
6 - glib's memery allocation routines replaced with
7 malloc/free calls
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 $Id: jslist.h,v 1.2 2005/11/23 11:24:29 letz Exp $
26 #ifndef __jack_jslist_h__
27 #define __jack_jslist_h__
29 #include <stdlib.h>
31 typedef struct _JSList JSList;
33 typedef int (*JCompareFunc) (void* a,
34 void* b);
35 struct _JSList
37 void *data;
38 JSList *next;
41 static __inline__
42 JSList*
43 jack_slist_alloc (void)
45 JSList *new_list;
47 new_list = (JSList*)malloc(sizeof(JSList));
48 new_list->data = NULL;
49 new_list->next = NULL;
51 return new_list;
54 static __inline__
55 JSList*
56 jack_slist_prepend (JSList *list,
57 void *data)
59 JSList *new_list;
61 new_list = (JSList*)malloc(sizeof(JSList));
62 new_list->data = data;
63 new_list->next = list;
65 return new_list;
68 #define jack_slist_next(slist) ((slist) ? (((JSList *)(slist))->next) : NULL)
69 static __inline__
70 JSList*
71 jack_slist_last (JSList *list)
73 if (list) {
74 while (list->next)
75 list = list->next;
78 return list;
81 static __inline__
82 JSList*
83 jack_slist_remove_link (JSList *list,
84 JSList *link)
86 JSList *tmp;
87 JSList *prev;
89 prev = NULL;
90 tmp = list;
92 while (tmp) {
93 if (tmp == link) {
94 if (prev)
95 prev->next = tmp->next;
96 if (list == tmp)
97 list = list->next;
99 tmp->next = NULL;
100 break;
103 prev = tmp;
104 tmp = tmp->next;
107 return list;
110 static __inline__
111 void
112 jack_slist_free (JSList *list)
114 while (list) {
115 JSList *next = list->next;
116 free(list);
117 list = next;
121 static __inline__
122 void
123 jack_slist_free_1 (JSList *list)
125 if (list) {
126 free(list);
130 static __inline__
131 JSList*
132 jack_slist_remove (JSList *list,
133 void *data)
135 JSList *tmp;
136 JSList *prev;
138 prev = NULL;
139 tmp = list;
141 while (tmp) {
142 if (tmp->data == data) {
143 if (prev)
144 prev->next = tmp->next;
145 if (list == tmp)
146 list = list->next;
148 tmp->next = NULL;
149 jack_slist_free (tmp);
151 break;
154 prev = tmp;
155 tmp = tmp->next;
158 return list;
161 static __inline__
162 unsigned int
163 jack_slist_length (JSList *list)
165 unsigned int length;
167 length = 0;
168 while (list) {
169 length++;
170 list = list->next;
173 return length;
176 static __inline__
177 JSList*
178 jack_slist_find (JSList *list,
179 void *data)
181 while (list) {
182 if (list->data == data)
183 break;
184 list = list->next;
187 return list;
190 static __inline__
191 JSList*
192 jack_slist_copy (JSList *list)
194 JSList *new_list = NULL;
196 if (list) {
197 JSList *last;
199 new_list = jack_slist_alloc ();
200 new_list->data = list->data;
201 last = new_list;
202 list = list->next;
203 while (list) {
204 last->next = jack_slist_alloc ();
205 last = last->next;
206 last->data = list->data;
207 list = list->next;
211 return new_list;
214 static __inline__
215 JSList*
216 jack_slist_append (JSList *list,
217 void *data)
219 JSList *new_list;
220 JSList *last;
222 new_list = jack_slist_alloc ();
223 new_list->data = data;
225 if (list) {
226 last = jack_slist_last (list);
227 last->next = new_list;
229 return list;
230 } else
231 return new_list;
234 static __inline__
235 JSList*
236 jack_slist_sort_merge (JSList *l1,
237 JSList *l2,
238 JCompareFunc compare_func)
240 JSList list, *l;
242 l = &list;
244 while (l1 && l2) {
245 if (compare_func(l1->data, l2->data) < 0) {
246 l = l->next = l1;
247 l1 = l1->next;
248 } else {
249 l = l->next = l2;
250 l2 = l2->next;
253 l->next = l1 ? l1 : l2;
255 return list.next;
258 static __inline__
259 JSList*
260 jack_slist_sort (JSList *list,
261 JCompareFunc compare_func)
263 JSList *l1, *l2;
265 if (!list)
266 return NULL;
267 if (!list->next)
268 return list;
270 l1 = list;
271 l2 = list->next;
273 while ((l2 = l2->next) != NULL) {
274 if ((l2 = l2->next) == NULL)
275 break;
276 l1 = l1->next;
278 l2 = l1->next;
279 l1->next = NULL;
281 return jack_slist_sort_merge (jack_slist_sort (list, compare_func),
282 jack_slist_sort (l2, compare_func),
283 compare_func);
286 #endif /* __jack_jslist_h__ */