ioutil: Remove unnecessary casts and fix const-discarding
[openocd.git] / src / helper / list.h
blob302b91097b5d655343d6fe5e6ced84d58413436c
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
4 /* begin local changes */
5 #include <helper/types.h>
7 #define prefetch(x) ((void)x)
8 #define LIST_POISON1 NULL
9 #define LIST_POISON2 NULL
11 struct list_head {
12 struct list_head *next, *prev;
14 struct hlist_head {
15 struct hlist_node *first;
17 struct hlist_node {
18 struct hlist_node *next, **pprev;
20 /* end local changes */
23 * Simple doubly linked list implementation.
25 * Some of the internal functions ("__xxx") are useful when
26 * manipulating whole lists rather than single entries, as
27 * sometimes we already know the next/prev entries and we can
28 * generate better code by using them directly rather than
29 * using the generic single-entry routines.
32 #define LIST_HEAD_INIT(name) { &(name), &(name) }
34 #define LIST_HEAD(name) \
35 struct list_head name = LIST_HEAD_INIT(name)
37 static inline void INIT_LIST_HEAD(struct list_head *list)
39 list->next = list;
40 list->prev = list;
44 * Insert a new entry between two known consecutive entries.
46 * This is only for internal list manipulation where we know
47 * the prev/next entries already!
49 #ifndef CONFIG_DEBUG_LIST
50 static inline void __list_add(struct list_head *new,
51 struct list_head *prev,
52 struct list_head *next)
54 next->prev = new;
55 new->next = next;
56 new->prev = prev;
57 prev->next = new;
59 #else
60 extern void __list_add(struct list_head *new,
61 struct list_head *prev,
62 struct list_head *next);
63 #endif
65 /**
66 * list_add - add a new entry
67 * @new: new entry to be added
68 * @head: list head to add it after
70 * Insert a new entry after the specified head.
71 * This is good for implementing stacks.
73 static inline void list_add(struct list_head *new, struct list_head *head)
75 __list_add(new, head, head->next);
79 /**
80 * list_add_tail - add a new entry
81 * @new: new entry to be added
82 * @head: list head to add it before
84 * Insert a new entry before the specified head.
85 * This is useful for implementing queues.
87 static inline void list_add_tail(struct list_head *new, struct list_head *head)
89 __list_add(new, head->prev, head);
93 * Delete a list entry by making the prev/next entries
94 * point to each other.
96 * This is only for internal list manipulation where we know
97 * the prev/next entries already!
99 static inline void __list_del(struct list_head *prev, struct list_head *next)
101 next->prev = prev;
102 prev->next = next;
106 * list_del - deletes entry from list.
107 * @entry: the element to delete from the list.
108 * Note: list_empty() on entry does not return true after this, the entry is
109 * in an undefined state.
111 #ifndef CONFIG_DEBUG_LIST
112 static inline void __list_del_entry(struct list_head *entry)
114 __list_del(entry->prev, entry->next);
117 static inline void list_del(struct list_head *entry)
119 __list_del(entry->prev, entry->next);
120 entry->next = LIST_POISON1;
121 entry->prev = LIST_POISON2;
123 #else
124 extern void __list_del_entry(struct list_head *entry);
125 extern void list_del(struct list_head *entry);
126 #endif
129 * list_replace - replace old entry by new one
130 * @old : the element to be replaced
131 * @new : the new element to insert
133 * If @old was empty, it will be overwritten.
135 static inline void list_replace(struct list_head *old,
136 struct list_head *new)
138 new->next = old->next;
139 new->next->prev = new;
140 new->prev = old->prev;
141 new->prev->next = new;
144 static inline void list_replace_init(struct list_head *old,
145 struct list_head *new)
147 list_replace(old, new);
148 INIT_LIST_HEAD(old);
152 * list_del_init - deletes entry from list and reinitialize it.
153 * @entry: the element to delete from the list.
155 static inline void list_del_init(struct list_head *entry)
157 __list_del_entry(entry);
158 INIT_LIST_HEAD(entry);
162 * list_move - delete from one list and add as another's head
163 * @list: the entry to move
164 * @head: the head that will precede our entry
166 static inline void list_move(struct list_head *list, struct list_head *head)
168 __list_del_entry(list);
169 list_add(list, head);
173 * list_move_tail - delete from one list and add as another's tail
174 * @list: the entry to move
175 * @head: the head that will follow our entry
177 static inline void list_move_tail(struct list_head *list,
178 struct list_head *head)
180 __list_del_entry(list);
181 list_add_tail(list, head);
185 * list_is_last - tests whether @list is the last entry in list @head
186 * @list: the entry to test
187 * @head: the head of the list
189 static inline int list_is_last(const struct list_head *list,
190 const struct list_head *head)
192 return list->next == head;
196 * list_empty - tests whether a list is empty
197 * @head: the list to test.
199 static inline int list_empty(const struct list_head *head)
201 return head->next == head;
205 * list_empty_careful - tests whether a list is empty and not being modified
206 * @head: the list to test
208 * Description:
209 * tests whether a list is empty _and_ checks that no other CPU might be
210 * in the process of modifying either member (next or prev)
212 * NOTE: using list_empty_careful() without synchronization
213 * can only be safe if the only activity that can happen
214 * to the list entry is list_del_init(). Eg. it cannot be used
215 * if another CPU could re-list_add() it.
217 static inline int list_empty_careful(const struct list_head *head)
219 struct list_head *next = head->next;
220 return (next == head) && (next == head->prev);
224 * list_rotate_left - rotate the list to the left
225 * @head: the head of the list
227 static inline void list_rotate_left(struct list_head *head)
229 struct list_head *first;
231 if (!list_empty(head)) {
232 first = head->next;
233 list_move_tail(first, head);
238 * list_is_singular - tests whether a list has just one entry.
239 * @head: the list to test.
241 static inline int list_is_singular(const struct list_head *head)
243 return !list_empty(head) && (head->next == head->prev);
246 static inline void __list_cut_position(struct list_head *list,
247 struct list_head *head, struct list_head *entry)
249 struct list_head *new_first = entry->next;
250 list->next = head->next;
251 list->next->prev = list;
252 list->prev = entry;
253 entry->next = list;
254 head->next = new_first;
255 new_first->prev = head;
259 * list_cut_position - cut a list into two
260 * @list: a new list to add all removed entries
261 * @head: a list with entries
262 * @entry: an entry within head, could be the head itself
263 * and if so we won't cut the list
265 * This helper moves the initial part of @head, up to and
266 * including @entry, from @head to @list. You should
267 * pass on @entry an element you know is on @head. @list
268 * should be an empty list or a list you do not care about
269 * losing its data.
272 static inline void list_cut_position(struct list_head *list,
273 struct list_head *head, struct list_head *entry)
275 if (list_empty(head))
276 return;
277 if (list_is_singular(head) &&
278 (head->next != entry && head != entry))
279 return;
280 if (entry == head)
281 INIT_LIST_HEAD(list);
282 else
283 __list_cut_position(list, head, entry);
286 static inline void __list_splice(const struct list_head *list,
287 struct list_head *prev,
288 struct list_head *next)
290 struct list_head *first = list->next;
291 struct list_head *last = list->prev;
293 first->prev = prev;
294 prev->next = first;
296 last->next = next;
297 next->prev = last;
301 * list_splice - join two lists, this is designed for stacks
302 * @list: the new list to add.
303 * @head: the place to add it in the first list.
305 static inline void list_splice(const struct list_head *list,
306 struct list_head *head)
308 if (!list_empty(list))
309 __list_splice(list, head, head->next);
313 * list_splice_tail - join two lists, each list being a queue
314 * @list: the new list to add.
315 * @head: the place to add it in the first list.
317 static inline void list_splice_tail(struct list_head *list,
318 struct list_head *head)
320 if (!list_empty(list))
321 __list_splice(list, head->prev, head);
325 * list_splice_init - join two lists and reinitialise the emptied list.
326 * @list: the new list to add.
327 * @head: the place to add it in the first list.
329 * The list at @list is reinitialised
331 static inline void list_splice_init(struct list_head *list,
332 struct list_head *head)
334 if (!list_empty(list)) {
335 __list_splice(list, head, head->next);
336 INIT_LIST_HEAD(list);
341 * list_splice_tail_init - join two lists and reinitialise the emptied list
342 * @list: the new list to add.
343 * @head: the place to add it in the first list.
345 * Each of the lists is a queue.
346 * The list at @list is reinitialised
348 static inline void list_splice_tail_init(struct list_head *list,
349 struct list_head *head)
351 if (!list_empty(list)) {
352 __list_splice(list, head->prev, head);
353 INIT_LIST_HEAD(list);
358 * list_entry - get the struct for this entry
359 * @ptr: the &struct list_head pointer.
360 * @type: the type of the struct this is embedded in.
361 * @member: the name of the list_struct within the struct.
363 #define list_entry(ptr, type, member) \
364 container_of(ptr, type, member)
367 * list_first_entry - get the first element from a list
368 * @ptr: the list head to take the element from.
369 * @type: the type of the struct this is embedded in.
370 * @member: the name of the list_struct within the struct.
372 * Note, that list is expected to be not empty.
374 #define list_first_entry(ptr, type, member) \
375 list_entry((ptr)->next, type, member)
378 * list_for_each - iterate over a list
379 * @pos: the &struct list_head to use as a loop cursor.
380 * @head: the head for your list.
382 #define list_for_each(pos, head) \
383 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
384 pos = pos->next)
387 * __list_for_each - iterate over a list
388 * @pos: the &struct list_head to use as a loop cursor.
389 * @head: the head for your list.
391 * This variant differs from list_for_each() in that it's the
392 * simplest possible list iteration code, no prefetching is done.
393 * Use this for code that knows the list to be very short (empty
394 * or 1 entry) most of the time.
396 #define __list_for_each(pos, head) \
397 for (pos = (head)->next; pos != (head); pos = pos->next)
400 * list_for_each_prev - iterate over a list backwards
401 * @pos: the &struct list_head to use as a loop cursor.
402 * @head: the head for your list.
404 #define list_for_each_prev(pos, head) \
405 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
406 pos = pos->prev)
409 * list_for_each_safe - iterate over a list safe against removal of list entry
410 * @pos: the &struct list_head to use as a loop cursor.
411 * @n: another &struct list_head to use as temporary storage
412 * @head: the head for your list.
414 #define list_for_each_safe(pos, n, head) \
415 for (pos = (head)->next, n = pos->next; pos != (head); \
416 pos = n, n = pos->next)
419 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
420 * @pos: the &struct list_head to use as a loop cursor.
421 * @n: another &struct list_head to use as temporary storage
422 * @head: the head for your list.
424 #define list_for_each_prev_safe(pos, n, head) \
425 for (pos = (head)->prev, n = pos->prev; \
426 prefetch(pos->prev), pos != (head); \
427 pos = n, n = pos->prev)
430 * list_for_each_entry - iterate over list of given type
431 * @pos: the type * to use as a loop cursor.
432 * @head: the head for your list.
433 * @member: the name of the list_struct within the struct.
435 #define list_for_each_entry(pos, head, member) \
436 for (pos = list_entry((head)->next, typeof(*pos), member); \
437 prefetch(pos->member.next), &pos->member != (head); \
438 pos = list_entry(pos->member.next, typeof(*pos), member))
441 * list_for_each_entry_reverse - iterate backwards over list of given type.
442 * @pos: the type * to use as a loop cursor.
443 * @head: the head for your list.
444 * @member: the name of the list_struct within the struct.
446 #define list_for_each_entry_reverse(pos, head, member) \
447 for (pos = list_entry((head)->prev, typeof(*pos), member); \
448 prefetch(pos->member.prev), &pos->member != (head); \
449 pos = list_entry(pos->member.prev, typeof(*pos), member))
452 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
453 * @pos: the type * to use as a start point
454 * @head: the head of the list
455 * @member: the name of the list_struct within the struct.
457 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
459 #define list_prepare_entry(pos, head, member) \
460 ((pos) ? : list_entry(head, typeof(*pos), member))
463 * list_for_each_entry_continue - continue iteration over list of given type
464 * @pos: the type * to use as a loop cursor.
465 * @head: the head for your list.
466 * @member: the name of the list_struct within the struct.
468 * Continue to iterate over list of given type, continuing after
469 * the current position.
471 #define list_for_each_entry_continue(pos, head, member) \
472 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
473 prefetch(pos->member.next), &pos->member != (head); \
474 pos = list_entry(pos->member.next, typeof(*pos), member))
477 * list_for_each_entry_continue_reverse - iterate backwards from the given point
478 * @pos: the type * to use as a loop cursor.
479 * @head: the head for your list.
480 * @member: the name of the list_struct within the struct.
482 * Start to iterate over list of given type backwards, continuing after
483 * the current position.
485 #define list_for_each_entry_continue_reverse(pos, head, member) \
486 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
487 prefetch(pos->member.prev), &pos->member != (head); \
488 pos = list_entry(pos->member.prev, typeof(*pos), member))
491 * list_for_each_entry_from - iterate over list of given type from the current point
492 * @pos: the type * to use as a loop cursor.
493 * @head: the head for your list.
494 * @member: the name of the list_struct within the struct.
496 * Iterate over list of given type, continuing from current position.
498 #define list_for_each_entry_from(pos, head, member) \
499 for (; prefetch(pos->member.next), &pos->member != (head); \
500 pos = list_entry(pos->member.next, typeof(*pos), member))
503 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
504 * @pos: the type * to use as a loop cursor.
505 * @n: another type * to use as temporary storage
506 * @head: the head for your list.
507 * @member: the name of the list_struct within the struct.
509 #define list_for_each_entry_safe(pos, n, head, member) \
510 for (pos = list_entry((head)->next, typeof(*pos), member), \
511 n = list_entry(pos->member.next, typeof(*pos), member); \
512 &pos->member != (head); \
513 pos = n, n = list_entry(n->member.next, typeof(*n), member))
516 * list_for_each_entry_safe_continue - continue list iteration safe against removal
517 * @pos: the type * to use as a loop cursor.
518 * @n: another type * to use as temporary storage
519 * @head: the head for your list.
520 * @member: the name of the list_struct within the struct.
522 * Iterate over list of given type, continuing after current point,
523 * safe against removal of list entry.
525 #define list_for_each_entry_safe_continue(pos, n, head, member) \
526 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
527 n = list_entry(pos->member.next, typeof(*pos), member); \
528 &pos->member != (head); \
529 pos = n, n = list_entry(n->member.next, typeof(*n), member))
532 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
533 * @pos: the type * to use as a loop cursor.
534 * @n: another type * to use as temporary storage
535 * @head: the head for your list.
536 * @member: the name of the list_struct within the struct.
538 * Iterate over list of given type from current point, safe against
539 * removal of list entry.
541 #define list_for_each_entry_safe_from(pos, n, head, member) \
542 for (n = list_entry(pos->member.next, typeof(*pos), member); \
543 &pos->member != (head); \
544 pos = n, n = list_entry(n->member.next, typeof(*n), member))
547 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
548 * @pos: the type * to use as a loop cursor.
549 * @n: another type * to use as temporary storage
550 * @head: the head for your list.
551 * @member: the name of the list_struct within the struct.
553 * Iterate backwards over list of given type, safe against removal
554 * of list entry.
556 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
557 for (pos = list_entry((head)->prev, typeof(*pos), member), \
558 n = list_entry(pos->member.prev, typeof(*pos), member); \
559 &pos->member != (head); \
560 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
563 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
564 * @pos: the loop cursor used in the list_for_each_entry_safe loop
565 * @n: temporary storage used in list_for_each_entry_safe
566 * @member: the name of the list_struct within the struct.
568 * list_safe_reset_next is not safe to use in general if the list may be
569 * modified concurrently (eg. the lock is dropped in the loop body). An
570 * exception to this is if the cursor element (pos) is pinned in the list,
571 * and list_safe_reset_next is called after re-taking the lock and before
572 * completing the current iteration of the loop body.
574 #define list_safe_reset_next(pos, n, member) \
575 n = list_entry(pos->member.next, typeof(*pos), member)
578 * Double linked lists with a single pointer list head.
579 * Mostly useful for hash tables where the two pointer list head is
580 * too wasteful.
581 * You lose the ability to access the tail in O(1).
584 #define HLIST_HEAD_INIT { .first = NULL }
585 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
586 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
587 static inline void INIT_HLIST_NODE(struct hlist_node *h)
589 h->next = NULL;
590 h->pprev = NULL;
593 static inline int hlist_unhashed(const struct hlist_node *h)
595 return !h->pprev;
598 static inline int hlist_empty(const struct hlist_head *h)
600 return !h->first;
603 static inline void __hlist_del(struct hlist_node *n)
605 struct hlist_node *next = n->next;
606 struct hlist_node **pprev = n->pprev;
607 *pprev = next;
608 if (next)
609 next->pprev = pprev;
612 static inline void hlist_del(struct hlist_node *n)
614 __hlist_del(n);
615 n->next = LIST_POISON1;
616 n->pprev = LIST_POISON2;
619 static inline void hlist_del_init(struct hlist_node *n)
621 if (!hlist_unhashed(n)) {
622 __hlist_del(n);
623 INIT_HLIST_NODE(n);
627 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
629 struct hlist_node *first = h->first;
630 n->next = first;
631 if (first)
632 first->pprev = &n->next;
633 h->first = n;
634 n->pprev = &h->first;
637 /* next must be != NULL */
638 static inline void hlist_add_before(struct hlist_node *n,
639 struct hlist_node *next)
641 n->pprev = next->pprev;
642 n->next = next;
643 next->pprev = &n->next;
644 *(n->pprev) = n;
647 static inline void hlist_add_after(struct hlist_node *n,
648 struct hlist_node *next)
650 next->next = n->next;
651 n->next = next;
652 next->pprev = &n->next;
654 if (next->next)
655 next->next->pprev = &next->next;
658 /* after that we'll appear to be on some hlist and hlist_del will work */
659 static inline void hlist_add_fake(struct hlist_node *n)
661 n->pprev = &n->next;
665 * Move a list from one list head to another. Fixup the pprev
666 * reference of the first entry if it exists.
668 static inline void hlist_move_list(struct hlist_head *old,
669 struct hlist_head *new)
671 new->first = old->first;
672 if (new->first)
673 new->first->pprev = &new->first;
674 old->first = NULL;
677 #define hlist_entry(ptr, type, member) container_of(ptr, type, member)
679 #define hlist_for_each(pos, head) \
680 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
681 pos = pos->next)
683 #define hlist_for_each_safe(pos, n, head) \
684 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
685 pos = n)
688 * hlist_for_each_entry - iterate over list of given type
689 * @tpos: the type * to use as a loop cursor.
690 * @pos: the &struct hlist_node to use as a loop cursor.
691 * @head: the head for your list.
692 * @member: the name of the hlist_node within the struct.
694 #define hlist_for_each_entry(tpos, pos, head, member) \
695 for (pos = (head)->first; \
696 pos && ({ prefetch(pos->next); 1; }) && \
697 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
698 pos = pos->next)
701 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
702 * @tpos: the type * to use as a loop cursor.
703 * @pos: the &struct hlist_node to use as a loop cursor.
704 * @member: the name of the hlist_node within the struct.
706 #define hlist_for_each_entry_continue(tpos, pos, member) \
707 for (pos = (pos)->next; \
708 pos && ({ prefetch(pos->next); 1; }) && \
709 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
710 pos = pos->next)
713 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
714 * @tpos: the type * to use as a loop cursor.
715 * @pos: the &struct hlist_node to use as a loop cursor.
716 * @member: the name of the hlist_node within the struct.
718 #define hlist_for_each_entry_from(tpos, pos, member) \
719 for (; pos && ({ prefetch(pos->next); 1; }) && \
720 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
721 pos = pos->next)
724 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
725 * @tpos: the type * to use as a loop cursor.
726 * @pos: the &struct hlist_node to use as a loop cursor.
727 * @n: another &struct hlist_node to use as temporary storage
728 * @head: the head for your list.
729 * @member: the name of the hlist_node within the struct.
731 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
732 for (pos = (head)->first; \
733 pos && ({ n = pos->next; 1; }) && \
734 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
735 pos = n)
737 #endif