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