carl9170 firmware: import 1.7.0
[carl9170fw.git] / tools / include / list.h
1 /*
2  * list.h List Utilities
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #ifndef __LIST_H
13 #define __LIST_H
14
15 struct list_head {
16         struct list_head *next;
17         struct list_head *prev;
18 };
19
20 static inline void list_add(struct list_head *obj,
21                             struct list_head *prev,
22                             struct list_head *next)
23 {
24         prev->next = obj;
25         obj->prev = prev;
26         next->prev = obj;
27         obj->next = next;
28 }
29
30 static inline void list_add_tail(struct list_head *obj,
31                                  struct list_head *head)
32 {
33         list_add(obj, head->prev, head);
34 }
35
36 static inline void list_add_head(struct list_head *obj,
37                                  struct list_head *head)
38 {
39         list_add(obj, head, head->next);
40 }
41
42 static inline void list_del(struct list_head *obj)
43 {
44         obj->prev->next = obj->next;
45         obj->next->prev = obj->prev;
46         obj->next = obj->prev = obj;
47 }
48
49 static inline void list_replace(struct list_head *old,
50                                 struct list_head *obj)
51 {
52         obj->next = old->next;
53         obj->next->prev = obj;
54         obj->prev = old->prev;
55         obj->prev->next = obj;
56 }
57
58 static inline int list_empty(struct list_head *head)
59 {
60         return head->next == head;
61 }
62
63 #define list_entry(ptr, type, member) \
64         container_of(ptr, type, member)
65
66 #define list_first_entry(ptr, type, member) \
67         container_of((ptr)->next, type, member)
68
69 #define list_at_tail(pos, head, member) \
70         ((pos)->member.next == (head))
71
72 #define list_at_head(pos, head, member) \
73         ((pos)->member.prev == (head))
74
75 #define LIST_HEAD(name) \
76         struct list_head name = { &(name), &(name) }
77
78 #define list_for_each_entry(pos, head, member)                          \
79         for (pos = list_entry((head)->next, typeof(*pos), member);      \
80              &(pos)->member != (head);                                  \
81              (pos) = list_entry((pos)->member.next, typeof(*(pos)), member))
82
83 #define list_for_each_entry_safe(pos, n, head, member)                  \
84         for (pos = list_entry((head)->next, typeof(*pos), member),      \
85              n = list_entry(pos->member.next, typeof(*pos), member);    \
86              &(pos)->member != (head);                                  \
87              pos = n, n = list_entry(n->member.next, typeof(*n), member))
88
89 #define init_list_head(head) \
90         do { (head)->next = (head); (head)->prev = (head); } while (0)
91
92 #endif /* __LIST_H */