Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
86 | f9daq | 1 | //**************************************************************************** |
2 | // Copyright (C) 2000-2004 ARW Elektronik Germany |
||
3 | // |
||
4 | // |
||
5 | // This program is free software; you can redistribute it and/or modify |
||
6 | // it under the terms of the GNU General Public License as published by |
||
7 | // the Free Software Foundation; either version 2 of the License, or |
||
8 | // (at your option) any later version. |
||
9 | // |
||
10 | // This program is distributed in the hope that it will be useful, |
||
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | // GNU General Public License for more details. |
||
14 | // |
||
15 | // You should have received a copy of the GNU General Public License |
||
16 | // along with this program; if not, write to the Free Software |
||
17 | // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
18 | // |
||
19 | // This product is not authorized for use as critical component in |
||
20 | // life support systems without the express written approval of |
||
21 | // ARW Elektronik Germany. |
||
22 | // |
||
23 | // Please announce changes and hints to ARW Elektronik |
||
24 | // |
||
25 | // Maintainer(s): Klaus Hitschler (klaus.hitschler@gmx.de) |
||
26 | // |
||
27 | //**************************************************************************** |
||
28 | |||
29 | //**************************************************************************** |
||
30 | // |
||
31 | // list.c - provide list management functions to the driver, |
||
32 | // like C++ templates. |
||
33 | // |
||
34 | // $Log: list.c,v $ |
||
35 | // Revision 1.6 2004/08/12 19:59:19 klaus |
||
36 | // conversion to kernel-version 2.6, released version 6.0 |
||
37 | // |
||
38 | // Revision 1.5 2003/05/11 11:12:03 klaus |
||
39 | // matched to kernel 2.4 PCI handling, debug messages improved |
||
40 | // |
||
41 | // Revision 1.4 2001/11/20 20:12:50 klaus |
||
42 | // included new header and CVS log |
||
43 | // |
||
44 | // |
||
45 | // created by D. Muehlenberg and H.J.Mathes 1999 |
||
46 | // added getNextNode and getPrevNode AR 18.02.2000 |
||
47 | // MODVERSIONS included AR 24.04.2000 |
||
48 | // |
||
49 | //**************************************************************************** |
||
50 | |||
51 | #include "common.h" /* must be the first include */ |
||
52 | |||
53 | #include "list.h" |
||
54 | |||
55 | Node *newNode(void) |
||
56 | { |
||
57 | Node *mynode; |
||
58 | |||
59 | mynode = (Node *)kmalloc(sizeof(Node), GFP_ATOMIC); |
||
60 | if (!mynode) return NULL; |
||
61 | mynode->pred = NULL; |
||
62 | mynode->succ = NULL; |
||
63 | mynode->data = NULL; |
||
64 | |||
65 | return mynode; |
||
66 | } |
||
67 | |||
68 | Node *addNode(Node *pos, void *insert, enum NodePosition p) |
||
69 | { |
||
70 | Node *mynode; |
||
71 | mynode = (Node *)kmalloc(sizeof(Node), GFP_ATOMIC); |
||
72 | if (!mynode) return NULL; |
||
73 | if (p == CurrentLeft) { |
||
74 | mynode->pred = pos; |
||
75 | mynode->succ = pos->succ; |
||
76 | pos->succ = mynode; |
||
77 | } else { |
||
78 | mynode->pred = pos->pred; |
||
79 | pos->pred = mynode; |
||
80 | mynode->succ = pos; |
||
81 | } |
||
82 | mynode->data = insert; |
||
83 | return mynode; |
||
84 | } |
||
85 | |||
86 | Node *delNodeAndData(Node *pos) |
||
87 | { |
||
88 | Node *tail = NULL; |
||
89 | if (!pos->pred) return NULL; |
||
90 | /* can't delete head */ |
||
91 | if (!pos->succ) { |
||
92 | tail = pos->pred; |
||
93 | tail->succ = NULL; |
||
94 | } else { |
||
95 | pos->pred->succ = pos->succ; |
||
96 | pos->succ->pred = pos->pred; |
||
97 | } |
||
98 | kfree_s(pos->data, sizeof(*pos->data)); // FREE(pos->data); |
||
99 | kfree_s(pos, sizeof(*pos)); // FREE(pos); |
||
100 | return tail; |
||
101 | } |
||
102 | |||
103 | Node *delNode(Node *pos) |
||
104 | { |
||
105 | Node *tail = NULL; |
||
106 | if (!pos) return NULL; |
||
107 | if (!pos->pred) return NULL; |
||
108 | /* can't delete head */ |
||
109 | if (!pos->succ) { |
||
110 | tail = pos->pred; |
||
111 | tail->succ = NULL; |
||
112 | } else { |
||
113 | pos->pred->succ = pos->succ; |
||
114 | pos->succ->pred = pos->pred; |
||
115 | } |
||
116 | kfree_s(pos, sizeof(*pos)); // FREE(pos); |
||
117 | return tail; |
||
118 | } |
||
119 | /* ------------------------------ */ |
||
120 | |||
121 | List *newList(void) |
||
122 | { |
||
123 | List *ret = (List *)kmalloc(sizeof(List), GFP_ATOMIC); |
||
124 | if (!ret) return NULL; |
||
125 | ret->head = ret->tail = newNode(); |
||
126 | if (!ret->head) { |
||
127 | kfree_s(ret, sizeof(*ret)); // FREE(ret); |
||
128 | return NULL; |
||
129 | } |
||
130 | ret->nodes = 0; |
||
131 | return ret; |
||
132 | } |
||
133 | |||
134 | void deleteList(List *l, void (*delete)(void *)) |
||
135 | { |
||
136 | if (l) { |
||
137 | resetList(l,delete); |
||
138 | kfree_s(l->head, sizeof(*l->head)); // FREE(l->head); |
||
139 | kfree_s(l, sizeof(*l)); // FREE(l); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | Node *addTail(List *list, void *insert) |
||
144 | { |
||
145 | Node *tail; |
||
146 | if (!list) return NULL; |
||
147 | tail = addNode(list->tail,insert,CurrentLeft); |
||
148 | if (!tail) |
||
149 | { |
||
150 | #ifdef __DEBUG__ |
||
151 | printk("addTail(): addNode failed!\n"); |
||
152 | #endif |
||
153 | return NULL; |
||
154 | } |
||
155 | list->tail = tail; |
||
156 | list->nodes++; |
||
157 | return tail; |
||
158 | } |
||
159 | |||
160 | Node *addHead(List *list, void *insert) |
||
161 | { |
||
162 | Node *node; |
||
163 | if (!list) return NULL; |
||
164 | if (!list->head->succ) { |
||
165 | return addTail(list,insert); |
||
166 | } else { |
||
167 | node = addNode(list->head,insert,CurrentRight); |
||
168 | } |
||
169 | if (node) list->nodes++; |
||
170 | return node; |
||
171 | } |
||
172 | |||
173 | Node *searchList(List *list, void *c, int (*comp)(void *, void *)) |
||
174 | { |
||
175 | register Node *n; |
||
176 | |||
177 | if (!list) return NULL; |
||
178 | n = list->head->succ; |
||
179 | while (n) { |
||
180 | if (!comp(c,n->data)) { |
||
181 | return n; |
||
182 | } |
||
183 | n = n->succ; |
||
184 | } |
||
185 | return NULL; |
||
186 | } |
||
187 | |||
188 | void delNodeAndDataInList(List *l, Node *pos) |
||
189 | { |
||
190 | Node *n; |
||
191 | if (!l) return; |
||
192 | if ((n = delNodeAndData(pos))) l->tail = n; |
||
193 | l->nodes--; |
||
194 | } |
||
195 | |||
196 | void delNodeInList(List *l, Node *pos) |
||
197 | { |
||
198 | Node *n; |
||
199 | if (!l) return; |
||
200 | if ((n = delNode(pos))) l->tail = n; |
||
201 | l->nodes--; |
||
202 | } |
||
203 | |||
204 | int emptyList(List *l) |
||
205 | { |
||
206 | if (!l) return 1; |
||
207 | return l->head->succ ? 0 : 1; |
||
208 | } |
||
209 | |||
210 | void delTailAndData(List *l) |
||
211 | { |
||
212 | Node *t; |
||
213 | if (!l) return; |
||
214 | t = l->tail; |
||
215 | if (emptyList(l)) return; |
||
216 | l->tail = l->tail->pred; |
||
217 | l->tail->succ = NULL; |
||
218 | kfree_s(t->data, sizeof(*t->data)); // FREE(t->data); |
||
219 | kfree_s(t, sizeof(*t)); // FREE(t); |
||
220 | l->nodes--; |
||
221 | } |
||
222 | |||
223 | void delTail(List *l) |
||
224 | { |
||
225 | Node *t; |
||
226 | if (!l) return; |
||
227 | t = l->tail; |
||
228 | if (emptyList(l)) return; |
||
229 | l->tail = l->tail->pred; |
||
230 | l->tail->succ = NULL; |
||
231 | kfree_s(t, sizeof(*t)); // FREE(t); |
||
232 | l->nodes--; |
||
233 | } |
||
234 | |||
235 | void resetList(List *l, void (*delete)(void *)) |
||
236 | { |
||
237 | register Node *n; |
||
238 | register void *data; |
||
239 | |||
240 | if (!l) return; |
||
241 | n = l->tail; |
||
242 | while (n->pred) { |
||
243 | data = n->data; |
||
244 | if (delete) { |
||
245 | delete(data); |
||
246 | } |
||
247 | else |
||
248 | { |
||
249 | kfree_s(data, sizeof(*data)); // FREE(data); |
||
250 | } |
||
251 | #if 0 |
||
252 | n = delNode(n); |
||
253 | #endif |
||
254 | delNode(n); |
||
255 | n = n->pred; |
||
256 | } |
||
257 | l->tail = l->head; |
||
258 | l->nodes = 0; |
||
259 | } |
||
260 | |||
261 | Node *getFirstNode(List *l) |
||
262 | { |
||
263 | if (!l) return NULL; |
||
264 | return l->head->succ; |
||
265 | } |
||
266 | |||
267 | Node *getNextNode(Node *n) |
||
268 | { |
||
269 | if (n) |
||
270 | return n->succ; |
||
271 | else |
||
272 | return NULL; |
||
273 | } |
||
274 | |||
275 | Node *getPrevNode(Node *n) |
||
276 | { |
||
277 | if (n) |
||
278 | return n->pred; |
||
279 | else |
||
280 | return NULL; |
||
281 | } |
||
282 | |||
283 | void *getContent(Node *n) |
||
284 | { |
||
285 | return n->data; |
||
286 | } |
||
287 | |||
288 | Node *getLastNode(List *l) |
||
289 | { |
||
290 | if (!l) return NULL; |
||
291 | return l->tail; |
||
292 | } |
||
293 | |||
294 | void delFirstNode(List *l) |
||
295 | { |
||
296 | if (!l) return; |
||
297 | delNodeInList(l,getFirstNode(l)); |
||
298 | } |
||
299 | |||
300 | void delFirstNodeAndData(List *l) |
||
301 | { |
||
302 | if (!l) return; |
||
303 | delNodeAndDataInList(l,getFirstNode(l)); |
||
304 | } |
||
305 | |||
306 | void delLastNode(List *l) |
||
307 | { |
||
308 | if (!l) return; |
||
309 | delNodeInList(l,getLastNode(l)); |
||
310 | } |
||
311 | |||
312 | void delLastNodeAndData(List *l) |
||
313 | { |
||
314 | if (!l) return; |
||
315 | delNodeAndDataInList(l,getLastNode(l)); |
||
316 | } |
||
317 | |||
318 | #ifndef USE_MACROS |
||
319 | int getNumOfNodesInList(List *l) |
||
320 | { |
||
321 | if (!l) return 0; |
||
322 | return l->nodes; |
||
323 | } |
||
324 | #endif |
||
325 | |||
326 | void printList(List *l, char *(*StructToString)(void *)) |
||
327 | { |
||
328 | register Node *n = l->head; |
||
329 | int i=1; |
||
330 | if (!l) return; |
||
331 | if (!n->succ) |
||
332 | { |
||
333 | printk("List empty!\n"); |
||
334 | return; |
||
335 | } |
||
336 | while (n->succ) |
||
337 | { |
||
338 | printk("Node %d: %s",i++,StructToString(n->succ->data)); |
||
339 | n = n->succ; |
||
340 | } |
||
341 | printk("\n"); |
||
342 | } |
||
343 | |||
344 | #undef printk |