brcm47xx: update bcma and ssb to master-2011-07-21
[openwrt.git] / package / ead / src / list.h
1 /* GPL v2, adapted from the Linux kernel */
2 #ifndef _LINUX_LIST_H
3 #define _LINUX_LIST_H
4
5 #include <stddef.h>
6 /**
7 * container_of - cast a member of a structure out to the containing structure
8 * @ptr: the pointer to the member.
9 * @type: the type of the container struct this is embedded in.
10 * @member: the name of the member within the struct.
11 *
12 */
13 #ifndef container_of
14 #define container_of(ptr, type, member) ( \
15 (type *)( (char *)ptr - offsetof(type,member) ))
16 #endif
17
18
19 /*
20 * Simple doubly linked list implementation.
21 *
22 * Some of the internal functions ("__xxx") are useful when
23 * manipulating whole lists rather than single entries, as
24 * sometimes we already know the next/prev entries and we can
25 * generate better code by using them directly rather than
26 * using the generic single-entry routines.
27 */
28
29 struct list_head {
30 struct list_head *next, *prev;
31 };
32
33 #define LIST_HEAD_INIT(name) { &(name), &(name) }
34
35 #define LIST_HEAD(name) \
36 struct list_head name = LIST_HEAD_INIT(name)
37
38 static inline void INIT_LIST_HEAD(struct list_head *list)
39 {
40 list->next = list;
41 list->prev = list;
42 }
43
44 /*
45 * Insert a new entry between two known consecutive entries.
46 *
47 * This is only for internal list manipulation where we know
48 * the prev/next entries already!
49 */
50 static inline void __list_add(struct list_head *new,
51 struct list_head *prev,
52 struct list_head *next)
53 {
54 next->prev = new;
55 new->next = next;
56 new->prev = prev;
57 prev->next = new;
58 }
59
60 /**
61 * list_add - add a new entry
62 * @new: new entry to be added
63 * @head: list head to add it after
64 *
65 * Insert a new entry after the specified head.
66 * This is good for implementing stacks.
67 */
68 static inline void list_add(struct list_head *new, struct list_head *head)
69 {
70 __list_add(new, head, head->next);
71 }
72
73
74 /**
75 * list_add_tail - add a new entry
76 * @new: new entry to be added
77 * @head: list head to add it before
78 *
79 * Insert a new entry before the specified head.
80 * This is useful for implementing queues.
81 */
82 static inline void list_add_tail(struct list_head *new, struct list_head *head)
83 {
84 __list_add(new, head->prev, head);
85 }
86
87
88 /*
89 * Delete a list entry by making the prev/next entries
90 * point to each other.
91 *
92 * This is only for internal list manipulation where we know
93 * the prev/next entries already!
94 */
95 static inline void __list_del(struct list_head * prev, struct list_head * next)
96 {
97 next->prev = prev;
98 prev->next = next;
99 }
100
101 /**
102 * list_del - deletes entry from list.
103 * @entry: the element to delete from the list.
104 * Note: list_empty() on entry does not return true after this, the entry is
105 * in an undefined state.
106 */
107 static inline void list_del(struct list_head *entry)
108 {
109 __list_del(entry->prev, entry->next);
110 entry->next = NULL;
111 entry->prev = NULL;
112 }
113
114 /**
115 * list_replace - replace old entry by new one
116 * @old : the element to be replaced
117 * @new : the new element to insert
118 *
119 * If @old was empty, it will be overwritten.
120 */
121 static inline void list_replace(struct list_head *old,
122 struct list_head *new)
123 {
124 new->next = old->next;
125 new->next->prev = new;
126 new->prev = old->prev;
127 new->prev->next = new;
128 }
129
130 static inline void list_replace_init(struct list_head *old,
131 struct list_head *new)
132 {
133 list_replace(old, new);
134 INIT_LIST_HEAD(old);
135 }
136
137 /**
138 * list_del_init - deletes entry from list and reinitialize it.
139 * @entry: the element to delete from the list.
140 */
141 static inline void list_del_init(struct list_head *entry)
142 {
143 __list_del(entry->prev, entry->next);
144 INIT_LIST_HEAD(entry);
145 }
146
147 /**
148 * list_move - delete from one list and add as another's head
149 * @list: the entry to move
150 * @head: the head that will precede our entry
151 */
152 static inline void list_move(struct list_head *list, struct list_head *head)
153 {
154 __list_del(list->prev, list->next);
155 list_add(list, head);
156 }
157
158 /**
159 * list_move_tail - delete from one list and add as another's tail
160 * @list: the entry to move
161 * @head: the head that will follow our entry
162 */
163 static inline void list_move_tail(struct list_head *list,
164 struct list_head *head)
165 {
166 __list_del(list->prev, list->next);
167 list_add_tail(list, head);
168 }
169
170 /**
171 * list_is_last - tests whether @list is the last entry in list @head
172 * @list: the entry to test
173 * @head: the head of the list
174 */
175 static inline int list_is_last(const struct list_head *list,
176 const struct list_head *head)
177 {
178 return list->next == head;
179 }
180
181 /**
182 * list_empty - tests whether a list is empty
183 * @head: the list to test.
184 */
185 static inline int list_empty(const struct list_head *head)
186 {
187 return head->next == head;
188 }
189
190 /**
191 * list_empty_careful - tests whether a list is empty and not being modified
192 * @head: the list to test
193 *
194 * Description:
195 * tests whether a list is empty _and_ checks that no other CPU might be
196 * in the process of modifying either member (next or prev)
197 *
198 * NOTE: using list_empty_careful() without synchronization
199 * can only be safe if the only activity that can happen
200 * to the list entry is list_del_init(). Eg. it cannot be used
201 * if another CPU could re-list_add() it.
202 */
203 static inline int list_empty_careful(const struct list_head *head)
204 {
205 struct list_head *next = head->next;
206 return (next == head) && (next == head->prev);
207 }
208
209 static inline void __list_splice(struct list_head *list,
210 struct list_head *head)
211 {
212 struct list_head *first = list->next;
213 struct list_head *last = list->prev;
214 struct list_head *at = head->next;
215
216 first->prev = head;
217 head->next = first;
218
219 last->next = at;
220 at->prev = last;
221 }
222
223 /**
224 * list_splice - join two lists
225 * @list: the new list to add.
226 * @head: the place to add it in the first list.
227 */
228 static inline void list_splice(struct list_head *list, struct list_head *head)
229 {
230 if (!list_empty(list))
231 __list_splice(list, head);
232 }
233
234 /**
235 * list_splice_init - join two lists and reinitialise the emptied list.
236 * @list: the new list to add.
237 * @head: the place to add it in the first list.
238 *
239 * The list at @list is reinitialised
240 */
241 static inline void list_splice_init(struct list_head *list,
242 struct list_head *head)
243 {
244 if (!list_empty(list)) {
245 __list_splice(list, head);
246 INIT_LIST_HEAD(list);
247 }
248 }
249
250 /**
251 * list_entry - get the struct for this entry
252 * @ptr: the &struct list_head pointer.
253 * @type: the type of the struct this is embedded in.
254 * @member: the name of the list_struct within the struct.
255 */
256 #define list_entry(ptr, type, member) \
257 container_of(ptr, type, member)
258
259 /**
260 * list_first_entry - get the first element from a list
261 * @ptr: the list head to take the element from.
262 * @type: the type of the struct this is embedded in.
263 * @member: the name of the list_struct within the struct.
264 *
265 * Note, that list is expected to be not empty.
266 */
267 #define list_first_entry(ptr, type, member) \
268 list_entry((ptr)->next, type, member)
269
270 /**
271 * list_for_each - iterate over a list
272 * @pos: the &struct list_head to use as a loop cursor.
273 * @head: the head for your list.
274 */
275 #define list_for_each(pos, head) \
276 for (pos = (head)->next; pos != (head); \
277 pos = pos->next)
278
279 /**
280 * __list_for_each - iterate over a list
281 * @pos: the &struct list_head to use as a loop cursor.
282 * @head: the head for your list.
283 *
284 * This variant differs from list_for_each() in that it's the
285 * simplest possible list iteration code, no prefetching is done.
286 * Use this for code that knows the list to be very short (empty
287 * or 1 entry) most of the time.
288 */
289 #define __list_for_each(pos, head) \
290 for (pos = (head)->next; pos != (head); pos = pos->next)
291
292 /**
293 * list_for_each_prev - iterate over a list backwards
294 * @pos: the &struct list_head to use as a loop cursor.
295 * @head: the head for your list.
296 */
297 #define list_for_each_prev(pos, head) \
298 for (pos = (head)->prev; pos != (head); \
299 pos = pos->prev)
300
301 /**
302 * list_for_each_safe - iterate over a list safe against removal of list entry
303 * @pos: the &struct list_head to use as a loop cursor.
304 * @n: another &struct list_head to use as temporary storage
305 * @head: the head for your list.
306 */
307 #define list_for_each_safe(pos, n, head) \
308 for (pos = (head)->next, n = pos->next; pos != (head); \
309 pos = n, n = pos->next)
310
311 /**
312 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
313 * @pos: the &struct list_head to use as a loop cursor.
314 * @n: another &struct list_head to use as temporary storage
315 * @head: the head for your list.
316 */
317 #define list_for_each_prev_safe(pos, n, head) \
318 for (pos = (head)->prev, n = pos->prev; \
319 pos != (head); \
320 pos = n, n = pos->prev)
321
322 /**
323 * list_for_each_entry - iterate over list of given type
324 * @pos: the type * to use as a loop cursor.
325 * @head: the head for your list.
326 * @member: the name of the list_struct within the struct.
327 */
328 #define list_for_each_entry(pos, head, member) \
329 for (pos = list_entry((head)->next, typeof(*pos), member); \
330 &pos->member != (head); \
331 pos = list_entry(pos->member.next, typeof(*pos), member))
332
333 /**
334 * list_for_each_entry_reverse - iterate backwards over list of given type.
335 * @pos: the type * to use as a loop cursor.
336 * @head: the head for your list.
337 * @member: the name of the list_struct within the struct.
338 */
339 #define list_for_each_entry_reverse(pos, head, member) \
340 for (pos = list_entry((head)->prev, typeof(*pos), member); \
341 &pos->member != (head); \
342 pos = list_entry(pos->member.prev, typeof(*pos), member))
343
344 /**
345 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
346 * @pos: the type * to use as a start point
347 * @head: the head of the list
348 * @member: the name of the list_struct within the struct.
349 *
350 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
351 */
352 #define list_prepare_entry(pos, head, member) \
353 ((pos) ? : list_entry(head, typeof(*pos), member))
354
355 /**
356 * list_for_each_entry_continue - continue iteration over list of given type
357 * @pos: the type * to use as a loop cursor.
358 * @head: the head for your list.
359 * @member: the name of the list_struct within the struct.
360 *
361 * Continue to iterate over list of given type, continuing after
362 * the current position.
363 */
364 #define list_for_each_entry_continue(pos, head, member) \
365 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
366 &pos->member != (head); \
367 pos = list_entry(pos->member.next, typeof(*pos), member))
368
369 /**
370 * list_for_each_entry_continue_reverse - iterate backwards from the given point
371 * @pos: the type * to use as a loop cursor.
372 * @head: the head for your list.
373 * @member: the name of the list_struct within the struct.
374 *
375 * Start to iterate over list of given type backwards, continuing after
376 * the current position.
377 */
378 #define list_for_each_entry_continue_reverse(pos, head, member) \
379 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
380 &pos->member != (head); \
381 pos = list_entry(pos->member.prev, typeof(*pos), member))
382
383 /**
384 * list_for_each_entry_from - iterate over list of given type from the current point
385 * @pos: the type * to use as a loop cursor.
386 * @head: the head for your list.
387 * @member: the name of the list_struct within the struct.
388 *
389 * Iterate over list of given type, continuing from current position.
390 */
391 #define list_for_each_entry_from(pos, head, member) \
392 for (; &pos->member != (head); \
393 pos = list_entry(pos->member.next, typeof(*pos), member))
394
395 /**
396 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
397 * @pos: the type * to use as a loop cursor.
398 * @n: another type * to use as temporary storage
399 * @head: the head for your list.
400 * @member: the name of the list_struct within the struct.
401 */
402 #define list_for_each_entry_safe(pos, n, head, member) \
403 for (pos = list_entry((head)->next, typeof(*pos), member), \
404 n = list_entry(pos->member.next, typeof(*pos), member); \
405 &pos->member != (head); \
406 pos = n, n = list_entry(n->member.next, typeof(*n), member))
407
408 /**
409 * list_for_each_entry_safe_continue
410 * @pos: the type * to use as a loop cursor.
411 * @n: another type * to use as temporary storage
412 * @head: the head for your list.
413 * @member: the name of the list_struct within the struct.
414 *
415 * Iterate over list of given type, continuing after current point,
416 * safe against removal of list entry.
417 */
418 #define list_for_each_entry_safe_continue(pos, n, head, member) \
419 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
420 n = list_entry(pos->member.next, typeof(*pos), member); \
421 &pos->member != (head); \
422 pos = n, n = list_entry(n->member.next, typeof(*n), member))
423
424 /**
425 * list_for_each_entry_safe_from
426 * @pos: the type * to use as a loop cursor.
427 * @n: another type * to use as temporary storage
428 * @head: the head for your list.
429 * @member: the name of the list_struct within the struct.
430 *
431 * Iterate over list of given type from current point, safe against
432 * removal of list entry.
433 */
434 #define list_for_each_entry_safe_from(pos, n, head, member) \
435 for (n = list_entry(pos->member.next, typeof(*pos), member); \
436 &pos->member != (head); \
437 pos = n, n = list_entry(n->member.next, typeof(*n), member))
438
439 /**
440 * list_for_each_entry_safe_reverse
441 * @pos: the type * to use as a loop cursor.
442 * @n: another type * to use as temporary storage
443 * @head: the head for your list.
444 * @member: the name of the list_struct within the struct.
445 *
446 * Iterate backwards over list of given type, safe against removal
447 * of list entry.
448 */
449 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
450 for (pos = list_entry((head)->prev, typeof(*pos), member), \
451 n = list_entry(pos->member.prev, typeof(*pos), member); \
452 &pos->member != (head); \
453 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
454
455 /*
456 * Double linked lists with a single pointer list head.
457 * Mostly useful for hash tables where the two pointer list head is
458 * too wasteful.
459 * You lose the ability to access the tail in O(1).
460 */
461
462 struct hlist_head {
463 struct hlist_node *first;
464 };
465
466 struct hlist_node {
467 struct hlist_node *next, **pprev;
468 };
469
470 #define HLIST_HEAD_INIT { .first = NULL }
471 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
472 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
473 static inline void INIT_HLIST_NODE(struct hlist_node *h)
474 {
475 h->next = NULL;
476 h->pprev = NULL;
477 }
478
479 static inline int hlist_unhashed(const struct hlist_node *h)
480 {
481 return !h->pprev;
482 }
483
484 static inline int hlist_empty(const struct hlist_head *h)
485 {
486 return !h->first;
487 }
488
489 static inline void __hlist_del(struct hlist_node *n)
490 {
491 struct hlist_node *next = n->next;
492 struct hlist_node **pprev = n->pprev;
493 *pprev = next;
494 if (next)
495 next->pprev = pprev;
496 }
497
498 static inline void hlist_del(struct hlist_node *n)
499 {
500 __hlist_del(n);
501 n->next = NULL;
502 n->pprev = NULL;
503 }
504
505 static inline void hlist_del_init(struct hlist_node *n)
506 {
507 if (!hlist_unhashed(n)) {
508 __hlist_del(n);
509 INIT_HLIST_NODE(n);
510 }
511 }
512
513
514 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
515 {
516 struct hlist_node *first = h->first;
517 n->next = first;
518 if (first)
519 first->pprev = &n->next;
520 h->first = n;
521 n->pprev = &h->first;
522 }
523
524
525 /* next must be != NULL */
526 static inline void hlist_add_before(struct hlist_node *n,
527 struct hlist_node *next)
528 {
529 n->pprev = next->pprev;
530 n->next = next;
531 next->pprev = &n->next;
532 *(n->pprev) = n;
533 }
534
535 static inline void hlist_add_after(struct hlist_node *n,
536 struct hlist_node *next)
537 {
538 next->next = n->next;
539 n->next = next;
540 next->pprev = &n->next;
541
542 if(next->next)
543 next->next->pprev = &next->next;
544 }
545
546 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
547
548 #define hlist_for_each(pos, head) \
549 for (pos = (head)->first; pos; pos = pos->next)
550
551 #define hlist_for_each_safe(pos, n, head) \
552 for (pos = (head)->first; pos; pos = n)
553
554 /**
555 * hlist_for_each_entry - iterate over list of given type
556 * @tpos: the type * to use as a loop cursor.
557 * @pos: the &struct hlist_node to use as a loop cursor.
558 * @head: the head for your list.
559 * @member: the name of the hlist_node within the struct.
560 */
561 #define hlist_for_each_entry(tpos, pos, head, member) \
562 for (pos = (head)->first; pos && \
563 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
564 pos = pos->next)
565
566 /**
567 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
568 * @tpos: the type * to use as a loop cursor.
569 * @pos: the &struct hlist_node to use as a loop cursor.
570 * @member: the name of the hlist_node within the struct.
571 */
572 #define hlist_for_each_entry_continue(tpos, pos, member) \
573 for (pos = (pos)->next; pos && \
574 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
575 pos = pos->next)
576
577 /**
578 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
579 * @tpos: the type * to use as a loop cursor.
580 * @pos: the &struct hlist_node to use as a loop cursor.
581 * @member: the name of the hlist_node within the struct.
582 */
583 #define hlist_for_each_entry_from(tpos, pos, member) \
584 for (; pos && \
585 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
586 pos = pos->next)
587
588 /**
589 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
590 * @tpos: the type * to use as a loop cursor.
591 * @pos: the &struct hlist_node to use as a loop cursor.
592 * @n: another &struct hlist_node to use as temporary storage
593 * @head: the head for your list.
594 * @member: the name of the hlist_node within the struct.
595 */
596 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
597 for (pos = (head)->first; \
598 pos && ({ n = pos->next; 1; }) && \
599 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
600 pos = n)
601
602 #endif
This page took 0.083431 seconds and 5 git commands to generate.