]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/app/usbcan/can/ul_listbase.h
Merge branch 'master' into can-usb1
[lincan.git] / embedded / app / usbcan / can / ul_listbase.h
1 #ifndef _UL_LISTBASE_H
2 #define _UL_LISTBASE_H
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #ifndef __KERNEL__
9
10 #define LIST_POISON1  ((void *) 0)
11 #define LIST_POISON2  ((void *) 0)
12
13 /*
14  * Simple doubly linked list implementation.
15  *
16  * Some of the internal functions ("__xxx") are useful when
17  * manipulating whole lists rather than single entries, as
18  * sometimes we already know the next/prev entries and we can
19  * generate better code by using them directly rather than
20  * using the generic single-entry routines.
21  */
22
23 struct list_head {
24         struct list_head *next, *prev;
25 };
26
27 #define LIST_HEAD_INIT(name) { &(name), &(name) }
28
29 #define LIST_HEAD(name) \
30         struct list_head name = LIST_HEAD_INIT(name)
31
32 #define INIT_LIST_HEAD(ptr) do { \
33         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
34 } while (0)
35
36 /*
37  * Insert a new entry between two known consecutive entries. 
38  *
39  * This is only for internal list manipulation where we know
40  * the prev/next entries already!
41  */
42 static inline void __list_add(struct list_head *new,
43                               struct list_head *prev,
44                               struct list_head *next)
45 {
46         next->prev = new;
47         new->next = next;
48         new->prev = prev;
49         prev->next = new;
50 }
51
52 /**
53  * list_add - add a new entry
54  * @new: new entry to be added
55  * @head: list head to add it after
56  *
57  * Insert a new entry after the specified head.
58  * This is good for implementing stacks.
59  */
60 static inline void list_add(struct list_head *new, struct list_head *head)
61 {
62         __list_add(new, head, head->next);
63 }
64
65 /**
66  * list_add_tail - add a new entry
67  * @new: new entry to be added
68  * @head: list head to add it before
69  *
70  * Insert a new entry before the specified head.
71  * This is useful for implementing queues.
72  */
73 static inline void list_add_tail(struct list_head *new, struct list_head *head)
74 {
75         __list_add(new, head->prev, head);
76 }
77
78 /*
79  * Delete a list entry by making the prev/next entries
80  * point to each other.
81  *
82  * This is only for internal list manipulation where we know
83  * the prev/next entries already!
84  */
85 static inline void __list_del(struct list_head * prev, struct list_head * next)
86 {
87         next->prev = prev;
88         prev->next = next;
89 }
90
91 /**
92  * list_del - deletes entry from list.
93  * @entry: the element to delete from the list.
94  * Note: list_empty on entry does not return true after this, the entry is
95  * in an undefined state.
96  */
97 static inline void list_del(struct list_head *entry)
98 {
99         __list_del(entry->prev, entry->next);
100         entry->next = LIST_POISON1;
101         entry->prev = LIST_POISON2;
102 }
103
104 /**
105  * list_del_init - deletes entry from list and reinitialize it.
106  * @entry: the element to delete from the list.
107  */
108 static inline void list_del_init(struct list_head *entry)
109 {
110         __list_del(entry->prev, entry->next);
111         INIT_LIST_HEAD(entry); 
112 }
113
114 /**
115  * list_move - delete from one list and add as another's head
116  * @list: the entry to move
117  * @head: the head that will precede our entry
118  */
119 static inline void list_move(struct list_head *list, struct list_head *head)
120 {
121         __list_del(list->prev, list->next);
122         list_add(list, head);
123 }
124
125 /**
126  * list_move_tail - delete from one list and add as another's tail
127  * @list: the entry to move
128  * @head: the head that will follow our entry
129  */
130 static inline void list_move_tail(struct list_head *list,
131                                   struct list_head *head)
132 {
133         __list_del(list->prev, list->next);
134         list_add_tail(list, head);
135 }
136
137 /**
138  * list_empty - tests whether a list is empty
139  * @head: the list to test.
140  */
141 static inline int list_empty(struct list_head *head)
142 {
143         return head->next == head;
144 }
145
146 static inline void __list_splice(struct list_head *list,
147                                  struct list_head *head)
148 {
149         struct list_head *first = list->next;
150         struct list_head *last = list->prev;
151         struct list_head *where = head->next;
152
153         first->prev = head;
154         head->next = first;
155
156         last->next = where;
157         where->prev = last;
158 }
159
160 /**
161  * list_splice - join two lists
162  * @list: the new list to add.
163  * @head: the place to add it in the first list.
164  */
165 static inline void list_splice(struct list_head *list, struct list_head *head)
166 {
167         if (!list_empty(list))
168                 __list_splice(list, head);
169 }
170
171 /**
172  * list_splice_init - join two lists and reinitialise the emptied list.
173  * @list: the new list to add.
174  * @head: the place to add it in the first list.
175  *
176  * The list at @list is reinitialised
177  */
178 static inline void list_splice_init(struct list_head *list,
179                                     struct list_head *head)
180 {
181         if (!list_empty(list)) {
182                 __list_splice(list, head);
183                 INIT_LIST_HEAD(list);
184         }
185 }
186
187 /**
188  * list_entry - get the struct for this entry
189  * @ptr:        the &struct list_head pointer.
190  * @type:       the type of the struct this is embedded in.
191  * @member:     the name of the list_struct within the struct.
192  */
193 #define list_entry(ptr, type, member) \
194         container_of(ptr, type, member)
195
196 /**
197  * list_for_each        -       iterate over a list
198  * @pos:        the &struct list_head to use as a loop counter.
199  * @head:       the head for your list.
200  */
201 #define list_for_each(pos, head) \
202         for (pos = (head)->next, prefetch(pos->next); pos != (head); \
203                 pos = pos->next, prefetch(pos->next))
204
205 /**
206  * __list_for_each      -       iterate over a list
207  * @pos:        the &struct list_head to use as a loop counter.
208  * @head:       the head for your list.
209  *
210  * This variant differs from list_for_each() in that it's the
211  * simplest possible list iteration code, no prefetching is done.
212  * Use this for code that knows the list to be very short (empty
213  * or 1 entry) most of the time.
214  */
215 #define __list_for_each(pos, head) \
216         for (pos = (head)->next; pos != (head); pos = pos->next)
217
218 /**
219  * list_for_each_prev   -       iterate over a list backwards
220  * @pos:        the &struct list_head to use as a loop counter.
221  * @head:       the head for your list.
222  */
223 #define list_for_each_prev(pos, head) \
224         for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
225                 pos = pos->prev, prefetch(pos->prev))
226                 
227 /**
228  * list_for_each_safe   -       iterate over a list safe against removal of list entry
229  * @pos:        the &struct list_head to use as a loop counter.
230  * @n:          another &struct list_head to use as temporary storage
231  * @head:       the head for your list.
232  */
233 #define list_for_each_safe(pos, n, head) \
234         for (pos = (head)->next, n = pos->next; pos != (head); \
235                 pos = n, n = pos->next)
236
237 /**
238  * list_for_each_entry  -       iterate over list of given type
239  * @pos:        the type * to use as a loop counter.
240  * @head:       the head for your list.
241  * @member:     the name of the list_struct within the struct.
242  */
243 #define list_for_each_entry(pos, head, member)                          \
244         for (pos = list_entry((head)->next, typeof(*pos), member),      \
245                      prefetch(pos->member.next);                        \
246              &pos->member != (head);                                    \
247              pos = list_entry(pos->member.next, typeof(*pos), member),  \
248                      prefetch(pos->member.next))
249
250 /**
251  * list_for_each_entry_reverse - iterate backwards over list of given type.
252  * @pos:        the type * to use as a loop counter.
253  * @head:       the head for your list.
254  * @member:     the name of the list_struct within the struct.
255  */
256 #define list_for_each_entry_reverse(pos, head, member)                  \
257         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
258                      prefetch(pos->member.prev);                        \
259              &pos->member != (head);                                    \
260              pos = list_entry(pos->member.prev, typeof(*pos), member),  \
261                      prefetch(pos->member.prev))
262
263
264 /**
265  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
266  * @pos:        the type * to use as a loop counter.
267  * @n:          another type * to use as temporary storage
268  * @head:       the head for your list.
269  * @member:     the name of the list_struct within the struct.
270  */
271 #define list_for_each_entry_safe(pos, n, head, member)                  \
272         for (pos = list_entry((head)->next, typeof(*pos), member),      \
273                 n = list_entry(pos->member.next, typeof(*pos), member); \
274              &pos->member != (head);                                    \
275              pos = n, n = list_entry(n->member.next, typeof(*n), member))
276
277 #else /*__KERNEL__*/
278
279 #include <linux/list.h>
280
281 #endif /*__KERNEL__*/
282
283 #ifdef __cplusplus
284 } /* extern "C"*/
285 #endif
286
287 #endif /* _UL_LISTBASE_H */