3 /* In RT-Linux this functions collide with Linux lists functions, so
4 * we use Linux functions instead. */
13 #define LIST_POISON1 ((void *) 0)
14 #define LIST_POISON2 ((void *) 0)
17 * Simple doubly linked list implementation.
19 * Some of the internal functions ("__xxx") are useful when
20 * manipulating whole lists rather than single entries, as
21 * sometimes we already know the next/prev entries and we can
22 * generate better code by using them directly rather than
23 * using the generic single-entry routines.
27 struct list_head *next, *prev;
30 #define LIST_HEAD_INIT(name) { &(name), &(name) }
32 #define LIST_HEAD(name) \
33 struct list_head name = LIST_HEAD_INIT(name)
35 #define INIT_LIST_HEAD(ptr) do { \
36 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
40 * Insert a new entry between two known consecutive entries.
42 * This is only for internal list manipulation where we know
43 * the prev/next entries already!
45 static inline void __list_add(struct list_head *new,
46 struct list_head *prev,
47 struct list_head *next)
56 * list_add - add a new entry
57 * @new: new entry to be added
58 * @head: list head to add it after
60 * Insert a new entry after the specified head.
61 * This is good for implementing stacks.
63 static inline void list_add(struct list_head *new, struct list_head *head)
65 __list_add(new, head, head->next);
69 * list_add_tail - add a new entry
70 * @new: new entry to be added
71 * @head: list head to add it before
73 * Insert a new entry before the specified head.
74 * This is useful for implementing queues.
76 static inline void list_add_tail(struct list_head *new, struct list_head *head)
78 __list_add(new, head->prev, head);
82 * Delete a list entry by making the prev/next entries
83 * point to each other.
85 * This is only for internal list manipulation where we know
86 * the prev/next entries already!
88 static inline void __list_del(struct list_head * prev, struct list_head * next)
95 * list_del - deletes entry from list.
96 * @entry: the element to delete from the list.
97 * Note: list_empty on entry does not return true after this, the entry is
98 * in an undefined state.
100 static inline void list_del(struct list_head *entry)
102 __list_del(entry->prev, entry->next);
103 entry->next = LIST_POISON1;
104 entry->prev = LIST_POISON2;
108 * list_del_init - deletes entry from list and reinitialize it.
109 * @entry: the element to delete from the list.
111 static inline void list_del_init(struct list_head *entry)
113 __list_del(entry->prev, entry->next);
114 INIT_LIST_HEAD(entry);
118 * list_move - delete from one list and add as another's head
119 * @list: the entry to move
120 * @head: the head that will precede our entry
122 static inline void list_move(struct list_head *list, struct list_head *head)
124 __list_del(list->prev, list->next);
125 list_add(list, head);
129 * list_move_tail - delete from one list and add as another's tail
130 * @list: the entry to move
131 * @head: the head that will follow our entry
133 static inline void list_move_tail(struct list_head *list,
134 struct list_head *head)
136 __list_del(list->prev, list->next);
137 list_add_tail(list, head);
141 * list_empty - tests whether a list is empty
142 * @head: the list to test.
144 static inline int list_empty(struct list_head *head)
146 return head->next == head;
149 static inline void __list_splice(struct list_head *list,
150 struct list_head *head)
152 struct list_head *first = list->next;
153 struct list_head *last = list->prev;
154 struct list_head *at = head->next;
164 * list_splice - join two lists
165 * @list: the new list to add.
166 * @head: the place to add it in the first list.
168 static inline void list_splice(struct list_head *list, struct list_head *head)
170 if (!list_empty(list))
171 __list_splice(list, head);
175 * list_splice_init - join two lists and reinitialise the emptied list.
176 * @list: the new list to add.
177 * @head: the place to add it in the first list.
179 * The list at @list is reinitialised
181 static inline void list_splice_init(struct list_head *list,
182 struct list_head *head)
184 if (!list_empty(list)) {
185 __list_splice(list, head);
186 INIT_LIST_HEAD(list);
191 * list_entry - get the struct for this entry
192 * @ptr: the &struct list_head pointer.
193 * @type: the type of the struct this is embedded in.
194 * @member: the name of the list_struct within the struct.
196 #define list_entry(ptr, type, member) \
197 container_of(ptr, type, member)
200 * list_for_each - iterate over a list
201 * @pos: the &struct list_head to use as a loop counter.
202 * @head: the head for your list.
204 #define list_for_each(pos, head) \
205 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
206 pos = pos->next, prefetch(pos->next))
209 * __list_for_each - iterate over a list
210 * @pos: the &struct list_head to use as a loop counter.
211 * @head: the head for your list.
213 * This variant differs from list_for_each() in that it's the
214 * simplest possible list iteration code, no prefetching is done.
215 * Use this for code that knows the list to be very short (empty
216 * or 1 entry) most of the time.
218 #define __list_for_each(pos, head) \
219 for (pos = (head)->next; pos != (head); pos = pos->next)
222 * list_for_each_prev - iterate over a list backwards
223 * @pos: the &struct list_head to use as a loop counter.
224 * @head: the head for your list.
226 #define list_for_each_prev(pos, head) \
227 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
228 pos = pos->prev, prefetch(pos->prev))
231 * list_for_each_safe - iterate over a list safe against removal of list entry
232 * @pos: the &struct list_head to use as a loop counter.
233 * @n: another &struct list_head to use as temporary storage
234 * @head: the head for your list.
236 #define list_for_each_safe(pos, n, head) \
237 for (pos = (head)->next, n = pos->next; pos != (head); \
238 pos = n, n = pos->next)
241 * list_for_each_entry - iterate over list of given type
242 * @pos: the type * to use as a loop counter.
243 * @head: the head for your list.
244 * @member: the name of the list_struct within the struct.
246 #define list_for_each_entry(pos, head, member) \
247 for (pos = list_entry((head)->next, typeof(*pos), member), \
248 prefetch(pos->member.next); \
249 &pos->member != (head); \
250 pos = list_entry(pos->member.next, typeof(*pos), member), \
251 prefetch(pos->member.next))
254 * list_for_each_entry_reverse - iterate backwards over list of given type.
255 * @pos: the type * to use as a loop counter.
256 * @head: the head for your list.
257 * @member: the name of the list_struct within the struct.
259 #define list_for_each_entry_reverse(pos, head, member) \
260 for (pos = list_entry((head)->prev, typeof(*pos), member), \
261 prefetch(pos->member.prev); \
262 &pos->member != (head); \
263 pos = list_entry(pos->member.prev, typeof(*pos), member), \
264 prefetch(pos->member.prev))
268 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
269 * @pos: the type * to use as a loop counter.
270 * @n: another type * to use as temporary storage
271 * @head: the head for your list.
272 * @member: the name of the list_struct within the struct.
274 #define list_for_each_entry_safe(pos, n, head, member) \
275 for (pos = list_entry((head)->next, typeof(*pos), member), \
276 n = list_entry(pos->member.next, typeof(*pos), member); \
277 &pos->member != (head); \
278 pos = n, n = list_entry(n->member.next, typeof(*n), member))
284 #endif /* _UL_LISTBASE_H */