Jack2: jslist.h Source File

Jack2 1.9.12

linux/alsa/jslist.h
1 /*
2 Based on gslist.c from glib-1.2.9 (LGPL).
3
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
8
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.
13
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.
18
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.
22
23 $Id: jslist.h,v 1.2 2005/11/23 11:24:29 letz Exp $
24 */
25
26 #ifndef __jack_jslist_h__
27 #define __jack_jslist_h__
28
29 #include <stdlib.h>
30
31 typedef struct _JSList JSList;
32
33 typedef int (*JCompareFunc) (void* a,
34 void* b);
35 struct _JSList
36 {
37 void *data;
38 JSList *next;
39 };
40
41 static __inline__
42 JSList*
43 jack_slist_alloc (void)
44 {
45 JSList *new_list;
46
47 new_list = (JSList*)malloc(sizeof(JSList));
48 new_list->data = NULL;
49 new_list->next = NULL;
50
51 return new_list;
52 }
53
54 static __inline__
55 JSList*
56 jack_slist_prepend (JSList *list,
57 void *data)
58 {
59 JSList *new_list;
60
61 new_list = (JSList*)malloc(sizeof(JSList));
62 new_list->data = data;
63 new_list->next = list;
64
65 return new_list;
66 }
67
68 #define jack_slist_next(slist) ((slist) ? (((JSList *)(slist))->next) : NULL)
69 static __inline__
70 JSList*
71 jack_slist_last (JSList *list)
72 {
73 if (list) {
74 while (list->next)
75 list = list->next;
76 }
77
78 return list;
79 }
80
81 static __inline__
82 JSList*
83 jack_slist_remove_link (JSList *list,
84 JSList *link)
85 {
86 JSList *tmp;
87 JSList *prev;
88
89 prev = NULL;
90 tmp = list;
91
92 while (tmp) {
93 if (tmp == link) {
94 if (prev)
95 prev->next = tmp->next;
96 if (list == tmp)
97 list = list->next;
98
99 tmp->next = NULL;
100 break;
101 }
102
103 prev = tmp;
104 tmp = tmp->next;
105 }
106
107 return list;
108 }
109
110 static __inline__
111 void
112 jack_slist_free (JSList *list)
113 {
114 while (list) {
115 JSList *next = list->next;
116 free(list);
117 list = next;
118 }
119 }
120
121 static __inline__
122 void
123 jack_slist_free_1 (JSList *list)
124 {
125 if (list) {
126 free(list);
127 }
128 }
129
130 static __inline__
131 JSList*
132 jack_slist_remove (JSList *list,
133 void *data)
134 {
135 JSList *tmp;
136 JSList *prev;
137
138 prev = NULL;
139 tmp = list;
140
141 while (tmp) {
142 if (tmp->data == data) {
143 if (prev)
144 prev->next = tmp->next;
145 if (list == tmp)
146 list = list->next;
147
148 tmp->next = NULL;
149 jack_slist_free (tmp);
150
151 break;
152 }
153
154 prev = tmp;
155 tmp = tmp->next;
156 }
157
158 return list;
159 }
160
161 static __inline__
162 unsigned int
163 jack_slist_length (JSList *list)
164 {
165 unsigned int length;
166
167 length = 0;
168 while (list) {
169 length++;
170 list = list->next;
171 }
172
173 return length;
174 }
175
176 static __inline__
177 JSList*
178 jack_slist_find (JSList *list,
179 void *data)
180 {
181 while (list) {
182 if (list->data == data)
183 break;
184 list = list->next;
185 }
186
187 return list;
188 }
189
190 static __inline__
191 JSList*
192 jack_slist_copy (JSList *list)
193 {
194 JSList *new_list = NULL;
195
196 if (list) {
197 JSList *last;
198
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;
208 }
209 }
210
211 return new_list;
212 }
213
214 static __inline__
215 JSList*
216 jack_slist_append (JSList *list,
217 void *data)
218 {
219 JSList *new_list;
220 JSList *last;
221
222 new_list = jack_slist_alloc ();
223 new_list->data = data;
224
225 if (list) {
226 last = jack_slist_last (list);
227 last->next = new_list;
228
229 return list;
230 } else
231 return new_list;
232 }
233
234 static __inline__
235 JSList*
236 jack_slist_sort_merge (JSList *l1,
237 JSList *l2,
238 JCompareFunc compare_func)
239 {
240 JSList list, *l;
241
242 l = &list;
243
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;
251 }
252 }
253 l->next = l1 ? l1 : l2;
254
255 return list.next;
256 }
257
258 static __inline__
259 JSList*
260 jack_slist_sort (JSList *list,
261 JCompareFunc compare_func)
262 {
263 JSList *l1, *l2;
264
265 if (!list)
266 return NULL;
267 if (!list->next)
268 return list;
269
270 l1 = list;
271 l2 = list->next;
272
273 while ((l2 = l2->next) != NULL) {
274 if ((l2 = l2->next) == NULL)
275 break;
276 l1 = l1->next;
277 }
278 l2 = l1->next;
279 l1->next = NULL;
280
281 return jack_slist_sort_merge (jack_slist_sort (list, compare_func),
282 jack_slist_sort (l2, compare_func),
283 compare_func);
284 }
285
286 #endif /* __jack_jslist_h__ */
_JSList
Definition: linux/alsa/jslist.h:35