6 #include <lwk/stddef.h>
7 #include <lwk/prefetch.h>
10 * These are non-NULL pointers that will result in page faults
11 * under normal circumstances, used to verify that nobody uses
12 * non-initialized list entries.
14 #define LIST_POISON1 ((void *) 0x00100100)
15 #define LIST_POISON2 ((void *) 0x00200200)
18 * Simple doubly linked list implementation.
20 * Some of the internal functions ("__xxx") are useful when
21 * manipulating whole lists rather than single entries, as
22 * sometimes we already know the next/prev entries and we can
23 * generate better code by using them directly rather than
24 * using the generic single-entry routines.
28 struct list_head *next, *prev;
31 #define LIST_HEAD_INIT(name) { &(name), &(name) }
33 #define LIST_HEAD(name) \
34 struct list_head name = LIST_HEAD_INIT(name)
36 static inline void INIT_LIST_HEAD(struct list_head *list)
42 static inline void list_head_init(struct list_head *list)
48 * Insert a new entry between two known consecutive entries.
50 * This is only for internal list manipulation where we know
51 * the prev/next entries already!
53 static inline void __list_add(struct list_head *new,
54 struct list_head *prev,
55 struct list_head *next)
64 * list_add - add a new entry
65 * @new: new entry to be added
66 * @head: list head to add it after
68 * Insert a new entry after the specified head.
69 * This is good for implementing stacks.
71 static inline void list_add(struct list_head *new, struct list_head *head)
73 __list_add(new, head, head->next);
77 * list_add_tail - add a new entry
78 * @new: new entry to be added
79 * @head: list head to add it before
81 * Insert a new entry before the specified head.
82 * This is useful for implementing queues.
84 static inline void list_add_tail(struct list_head *new, struct list_head *head)
86 __list_add(new, head->prev, head);
90 * Delete a list entry by making the prev/next entries
91 * point to each other.
93 * This is only for internal list manipulation where we know
94 * the prev/next entries already!
96 static inline void __list_del(struct list_head * prev, struct list_head * next)
103 * list_del - deletes entry from list.
104 * @entry: the element to delete from the list.
105 * Note: list_empty on entry does not return true after this, the entry is
106 * in an undefined state.
108 static inline void list_del(struct list_head *entry)
110 __list_del(entry->prev, entry->next);
111 entry->next = LIST_POISON1;
112 entry->prev = LIST_POISON2;
116 * list_del_init - deletes entry from list and reinitialize it.
117 * @entry: the element to delete from the list.
119 static inline void list_del_init(struct list_head *entry)
121 __list_del(entry->prev, entry->next);
122 INIT_LIST_HEAD(entry);
126 * list_move - delete from one list and add as another's head
127 * @list: the entry to move
128 * @head: the head that will precede our entry
130 static inline void list_move(struct list_head *list, struct list_head *head)
132 __list_del(list->prev, list->next);
133 list_add(list, head);
137 * list_move_tail - delete from one list and add as another's tail
138 * @list: the entry to move
139 * @head: the head that will follow our entry
141 static inline void list_move_tail(struct list_head *list,
142 struct list_head *head)
144 __list_del(list->prev, list->next);
145 list_add_tail(list, head);
149 * list_empty - tests whether a list is empty
150 * @head: the list to test.
152 static inline int list_empty(const struct list_head *head)
154 return head->next == head;
158 * list_empty_careful - tests whether a list is
159 * empty _and_ checks that no other CPU might be
160 * in the process of still modifying either member
162 * NOTE: using list_empty_careful() without synchronization
163 * can only be safe if the only activity that can happen
164 * to the list entry is list_del_init(). Eg. it cannot be used
165 * if another CPU could re-list_add() it.
167 * @head: the list to test.
169 static inline int list_empty_careful(const struct list_head *head)
171 struct list_head *next = head->next;
172 return (next == head) && (next == head->prev);
175 static inline void __list_splice(struct list_head *list,
176 struct list_head *head)
178 struct list_head *first = list->next;
179 struct list_head *last = list->prev;
180 struct list_head *at = head->next;
190 * list_splice - join two lists
191 * @list: the new list to add.
192 * @head: the place to add it in the first list.
194 static inline void list_splice(struct list_head *list, struct list_head *head)
196 if (!list_empty(list))
197 __list_splice(list, head);
201 * list_splice_init - join two lists and reinitialise the emptied list.
202 * @list: the new list to add.
203 * @head: the place to add it in the first list.
205 * The list at @list is reinitialised
207 static inline void list_splice_init(struct list_head *list,
208 struct list_head *head)
210 if (!list_empty(list)) {
211 __list_splice(list, head);
212 INIT_LIST_HEAD(list);
217 * list_entry - get the struct for this entry
218 * @ptr: the &struct list_head pointer.
219 * @type: the type of the struct this is embedded in.
220 * @member: the name of the list_struct within the struct.
222 #define list_entry(ptr, type, member) \
223 container_of(ptr, type, member)
226 * list_for_each - iterate over a list
227 * @pos: the &struct list_head to use as a loop counter.
228 * @head: the head for your list.
230 #define list_for_each(pos, head) \
231 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
235 * __list_for_each - iterate over a list
236 * @pos: the &struct list_head to use as a loop counter.
237 * @head: the head for your list.
239 * This variant differs from list_for_each() in that it's the
240 * simplest possible list iteration code, no prefetching is done.
241 * Use this for code that knows the list to be very short (empty
242 * or 1 entry) most of the time.
244 #define __list_for_each(pos, head) \
245 for (pos = (head)->next; pos != (head); pos = pos->next)
248 * list_for_each_prev - iterate over a list backwards
249 * @pos: the &struct list_head to use as a loop counter.
250 * @head: the head for your list.
252 #define list_for_each_prev(pos, head) \
253 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
257 * list_for_each_safe - iterate over a list safe against removal of list entry
258 * @pos: the &struct list_head to use as a loop counter.
259 * @n: another &struct list_head to use as temporary storage
260 * @head: the head for your list.
262 #define list_for_each_safe(pos, n, head) \
263 for (pos = (head)->next, n = pos->next; pos != (head); \
264 pos = n, n = pos->next)
267 * list_for_each_entry - iterate over list of given type
268 * @pos: the type * to use as a loop counter.
269 * @head: the head for your list.
270 * @member: the name of the list_struct within the struct.
272 #define list_for_each_entry(pos, head, member) \
273 for (pos = list_entry((head)->next, typeof(*pos), member); \
274 prefetch(pos->member.next), &pos->member != (head); \
275 pos = list_entry(pos->member.next, typeof(*pos), member))
278 * list_for_each_entry_reverse - iterate backwards over list of given type.
279 * @pos: the type * to use as a loop counter.
280 * @head: the head for your list.
281 * @member: the name of the list_struct within the struct.
283 #define list_for_each_entry_reverse(pos, head, member) \
284 for (pos = list_entry((head)->prev, typeof(*pos), member); \
285 prefetch(pos->member.prev), &pos->member != (head); \
286 pos = list_entry(pos->member.prev, typeof(*pos), member))
289 * list_prepare_entry - prepare a pos entry for use as a start point in
290 * list_for_each_entry_continue
291 * @pos: the type * to use as a start point
292 * @head: the head of the list
293 * @member: the name of the list_struct within the struct.
295 #define list_prepare_entry(pos, head, member) \
296 ((pos) ? : list_entry(head, typeof(*pos), member))
299 * list_for_each_entry_continue - iterate over list of given type
300 * continuing after existing point
301 * @pos: the type * to use as a loop counter.
302 * @head: the head for your list.
303 * @member: the name of the list_struct within the struct.
305 #define list_for_each_entry_continue(pos, head, member) \
306 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
307 prefetch(pos->member.next), &pos->member != (head); \
308 pos = list_entry(pos->member.next, typeof(*pos), member))
311 * list_for_each_entry_from - iterate over list of given type
312 * continuing from existing point
313 * @pos: the type * to use as a loop counter.
314 * @head: the head for your list.
315 * @member: the name of the list_struct within the struct.
317 #define list_for_each_entry_from(pos, head, member) \
318 for (; prefetch(pos->member.next), &pos->member != (head); \
319 pos = list_entry(pos->member.next, typeof(*pos), member))
322 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
323 * @pos: the type * to use as a loop counter.
324 * @n: another type * to use as temporary storage
325 * @head: the head for your list.
326 * @member: the name of the list_struct within the struct.
328 #define list_for_each_entry_safe(pos, n, head, member) \
329 for (pos = list_entry((head)->next, typeof(*pos), member), \
330 n = list_entry(pos->member.next, typeof(*pos), member); \
331 &pos->member != (head); \
332 pos = n, n = list_entry(n->member.next, typeof(*n), member))
335 * list_for_each_entry_safe_continue - iterate over list of given type
336 * continuing after existing point safe against removal of list entry
337 * @pos: the type * to use as a loop counter.
338 * @n: another type * to use as temporary storage
339 * @head: the head for your list.
340 * @member: the name of the list_struct within the struct.
342 #define list_for_each_entry_safe_continue(pos, n, head, member) \
343 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
344 n = list_entry(pos->member.next, typeof(*pos), member); \
345 &pos->member != (head); \
346 pos = n, n = list_entry(n->member.next, typeof(*n), member))
349 * list_for_each_entry_safe_from - iterate over list of given type
350 * from existing point safe against removal of list entry
351 * @pos: the type * to use as a loop counter.
352 * @n: another type * to use as temporary storage
353 * @head: the head for your list.
354 * @member: the name of the list_struct within the struct.
356 #define list_for_each_entry_safe_from(pos, n, head, member) \
357 for (n = list_entry(pos->member.next, typeof(*pos), member); \
358 &pos->member != (head); \
359 pos = n, n = list_entry(n->member.next, typeof(*n), member))
362 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
363 * removal of list entry
364 * @pos: the type * to use as a loop counter.
365 * @n: another type * to use as temporary storage
366 * @head: the head for your list.
367 * @member: the name of the list_struct within the struct.
369 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
370 for (pos = list_entry((head)->prev, typeof(*pos), member), \
371 n = list_entry(pos->member.prev, typeof(*pos), member); \
372 &pos->member != (head); \
373 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
376 * Double linked lists with a single pointer list head.
377 * Mostly useful for hash tables where the two pointer list head is
379 * You lose the ability to access the tail in O(1).
383 struct hlist_node *first;
387 struct hlist_node *next, **pprev;
390 #define HLIST_HEAD_INIT { .first = NULL }
391 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
392 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
393 static inline void INIT_HLIST_NODE(struct hlist_node *h)
399 static inline void hlist_node_init(struct hlist_node *h)
404 static inline int hlist_unhashed(const struct hlist_node *h)
409 static inline int hlist_empty(const struct hlist_head *h)
414 static inline void __hlist_del(struct hlist_node *n)
416 struct hlist_node *next = n->next;
417 struct hlist_node **pprev = n->pprev;
423 static inline void hlist_del(struct hlist_node *n)
426 n->next = LIST_POISON1;
427 n->pprev = LIST_POISON2;
430 static inline void hlist_del_init(struct hlist_node *n)
432 if (!hlist_unhashed(n)) {
438 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
440 struct hlist_node *first = h->first;
443 first->pprev = &n->next;
445 n->pprev = &h->first;
448 /* next must be != NULL */
449 static inline void hlist_add_before(struct hlist_node *n,
450 struct hlist_node *next)
452 n->pprev = next->pprev;
454 next->pprev = &n->next;
458 static inline void hlist_add_after(struct hlist_node *n,
459 struct hlist_node *next)
461 next->next = n->next;
463 next->pprev = &n->next;
466 next->next->pprev = &next->next;
469 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
471 #define hlist_for_each(pos, head) \
472 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
475 #define hlist_for_each_safe(pos, n, head) \
476 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
480 * hlist_for_each_entry - iterate over list of given type
481 * @tpos: the type * to use as a loop counter.
482 * @pos: the &struct hlist_node to use as a loop counter.
483 * @head: the head for your list.
484 * @member: the name of the hlist_node within the struct.
486 #define hlist_for_each_entry(tpos, pos, head, member) \
487 for (pos = (head)->first; \
488 pos && ({ prefetch(pos->next); 1;}) && \
489 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
493 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
494 * @tpos: the type * to use as a loop counter.
495 * @pos: the &struct hlist_node to use as a loop counter.
496 * @member: the name of the hlist_node within the struct.
498 #define hlist_for_each_entry_continue(tpos, pos, member) \
499 for (pos = (pos)->next; \
500 pos && ({ prefetch(pos->next); 1;}) && \
501 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
505 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
506 * @tpos: the type * to use as a loop counter.
507 * @pos: the &struct hlist_node to use as a loop counter.
508 * @member: the name of the hlist_node within the struct.
510 #define hlist_for_each_entry_from(tpos, pos, member) \
511 for (; pos && ({ prefetch(pos->next); 1;}) && \
512 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
516 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
517 * @tpos: the type * to use as a loop counter.
518 * @pos: the &struct hlist_node to use as a loop counter.
519 * @n: another &struct hlist_node to use as temporary storage
520 * @head: the head for your list.
521 * @member: the name of the hlist_node within the struct.
523 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
524 for (pos = (head)->first; \
525 pos && ({ n = pos->next; 1; }) && \
526 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
530 #warning "don't include kernel headers in userspace"
531 #endif /* __KERNEL__ */