1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * The content of this file is mainly copied/inspired from Linux kernel
5 * code in include/linux/list.h, include/linux/types.h
6 * Last aligned with kernel v5.12:
7 * - skip the functions hlist_unhashed_lockless() and __list_del_clearprev()
8 * that are relevant only in kernel;
9 * - Remove non-standard GCC extension "omitted conditional operand" from
11 * - expand READ_ONCE, WRITE_ONCE, smp_load_acquire, smp_store_release;
12 * - make comments compatible with doxygen.
14 * There is an example of using this file in contrib/list_example.c.
17 #ifndef OPENOCD_HELPER_LIST_H
18 #define OPENOCD_HELPER_LIST_H
20 /* begin local changes */
21 #include <helper/types.h>
23 #define LIST_POISON1 NULL
24 #define LIST_POISON2 NULL
27 struct list_head *next, *prev;
31 struct hlist_node *first;
35 struct hlist_node *next, **pprev;
37 /* end local changes */
40 * Circular doubly linked list implementation.
42 * Some of the internal functions ("__xxx") are useful when
43 * manipulating whole lists rather than single entries, as
44 * sometimes we already know the next/prev entries and we can
45 * generate better code by using them directly rather than
46 * using the generic single-entry routines.
49 #define LIST_HEAD_INIT(name) { &(name), &(name) }
51 #define LIST_HEAD(name) \
52 struct list_head name = LIST_HEAD_INIT(name)
55 * INIT_LIST_HEAD - Initialize a list_head structure
56 * @param list list_head structure to be initialized.
58 * Initializes the list_head to point to itself. If it is a list header,
59 * the result is an empty list.
61 static inline void INIT_LIST_HEAD(struct list_head *list)
67 #ifdef CONFIG_DEBUG_LIST
68 extern bool __list_add_valid(struct list_head *new,
69 struct list_head *prev,
70 struct list_head *next);
71 extern bool __list_del_entry_valid(struct list_head *entry);
73 static inline bool __list_add_valid(struct list_head *new,
74 struct list_head *prev,
75 struct list_head *next)
79 static inline bool __list_del_entry_valid(struct list_head *entry)
86 * Insert a new entry between two known consecutive entries.
88 * This is only for internal list manipulation where we know
89 * the prev/next entries already!
91 static inline void __list_add(struct list_head *new,
92 struct list_head *prev,
93 struct list_head *next)
95 if (!__list_add_valid(new, prev, next))
105 * list_add - add a new entry
106 * @param new new entry to be added
107 * @param head list head to add it after
109 * Insert a new entry after the specified head.
110 * This is good for implementing stacks.
112 static inline void list_add(struct list_head *new, struct list_head *head)
114 __list_add(new, head, head->next);
119 * list_add_tail - add a new entry
120 * @param new new entry to be added
121 * @param head list head to add it before
123 * Insert a new entry before the specified head.
124 * This is useful for implementing queues.
126 static inline void list_add_tail(struct list_head *new, struct list_head *head)
128 __list_add(new, head->prev, head);
132 * Delete a list entry by making the prev/next entries
133 * point to each other.
135 * This is only for internal list manipulation where we know
136 * the prev/next entries already!
138 static inline void __list_del(struct list_head *prev, struct list_head *next)
144 /* Ignore kernel __list_del_clearprev() */
146 static inline void __list_del_entry(struct list_head *entry)
148 if (!__list_del_entry_valid(entry))
151 __list_del(entry->prev, entry->next);
155 * list_del - deletes entry from list.
156 * @param entry the element to delete from the list.
157 * Note: list_empty() on entry does not return true after this, the entry is
158 * in an undefined state.
160 static inline void list_del(struct list_head *entry)
162 __list_del_entry(entry);
163 entry->next = LIST_POISON1;
164 entry->prev = LIST_POISON2;
168 * list_replace - replace old entry by new one
169 * @param old the element to be replaced
170 * @param new the new element to insert
172 * If @a old was empty, it will be overwritten.
174 static inline void list_replace(struct list_head *old,
175 struct list_head *new)
177 new->next = old->next;
178 new->next->prev = new;
179 new->prev = old->prev;
180 new->prev->next = new;
184 * list_replace_init - replace old entry by new one and initialize the old one
185 * @param old the element to be replaced
186 * @param new the new element to insert
188 * If @a old was empty, it will be overwritten.
190 static inline void list_replace_init(struct list_head *old,
191 struct list_head *new)
193 list_replace(old, new);
198 * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
199 * @param entry1 the location to place entry2
200 * @param entry2 the location to place entry1
202 static inline void list_swap(struct list_head *entry1,
203 struct list_head *entry2)
205 struct list_head *pos = entry2->prev;
208 list_replace(entry1, entry2);
211 list_add(entry1, pos);
215 * list_del_init - deletes entry from list and reinitialize it.
216 * @param entry the element to delete from the list.
218 static inline void list_del_init(struct list_head *entry)
220 __list_del_entry(entry);
221 INIT_LIST_HEAD(entry);
225 * list_move - delete from one list and add as another's head
226 * @param list the entry to move
227 * @param head the head that will precede our entry
229 static inline void list_move(struct list_head *list, struct list_head *head)
231 __list_del_entry(list);
232 list_add(list, head);
236 * list_move_tail - delete from one list and add as another's tail
237 * @param list the entry to move
238 * @param head the head that will follow our entry
240 static inline void list_move_tail(struct list_head *list,
241 struct list_head *head)
243 __list_del_entry(list);
244 list_add_tail(list, head);
248 * list_bulk_move_tail - move a subsection of a list to its tail
249 * @param head the head that will follow our entry
250 * @param first the first entry to move
251 * @param last the last entry to move, can be the same as first
253 * Move all entries between @a first and including @a last before @a head.
254 * All three entries must belong to the same linked list.
256 static inline void list_bulk_move_tail(struct list_head *head,
257 struct list_head *first,
258 struct list_head *last)
260 first->prev->next = last->next;
261 last->next->prev = first->prev;
263 head->prev->next = first;
264 first->prev = head->prev;
271 * list_is_first -- tests whether @a list is the first entry in list @a head
272 * @param list the entry to test
273 * @param head the head of the list
275 static inline int list_is_first(const struct list_head *list,
276 const struct list_head *head)
278 return list->prev == head;
282 * list_is_last - tests whether @a list is the last entry in list @a head
283 * @param list the entry to test
284 * @param head the head of the list
286 static inline int list_is_last(const struct list_head *list,
287 const struct list_head *head)
289 return list->next == head;
293 * list_empty - tests whether a list is empty
294 * @param head the list to test.
296 static inline int list_empty(const struct list_head *head)
298 return head->next == head;
302 * list_del_init_careful - deletes entry from list and reinitialize it.
303 * @param entry the element to delete from the list.
305 * This is the same as list_del_init(), except designed to be used
306 * together with list_empty_careful() in a way to guarantee ordering
307 * of other memory operations.
309 * Any memory operations done before a list_del_init_careful() are
310 * guaranteed to be visible after a list_empty_careful() test.
312 static inline void list_del_init_careful(struct list_head *entry)
314 __list_del_entry(entry);
320 * list_empty_careful - tests whether a list is empty and not being modified
321 * @param head the list to test
324 * tests whether a list is empty _and_ checks that no other CPU might be
325 * in the process of modifying either member (next or prev)
327 * NOTE: using list_empty_careful() without synchronization
328 * can only be safe if the only activity that can happen
329 * to the list entry is list_del_init(). Eg. it cannot be used
330 * if another CPU could re-list_add() it.
332 static inline int list_empty_careful(const struct list_head *head)
334 struct list_head *next = head->next;
335 return (next == head) && (next == head->prev);
339 * list_rotate_left - rotate the list to the left
340 * @param head the head of the list
342 static inline void list_rotate_left(struct list_head *head)
344 struct list_head *first;
346 if (!list_empty(head)) {
348 list_move_tail(first, head);
353 * list_rotate_to_front() - Rotate list to specific item.
354 * @param list The desired new front of the list.
355 * @param head The head of the list.
357 * Rotates list so that @a list becomes the new front of the list.
359 static inline void list_rotate_to_front(struct list_head *list,
360 struct list_head *head)
363 * Deletes the list head from the list denoted by @a head and
364 * places it as the tail of @a list, this effectively rotates the
365 * list so that @a list is at the front.
367 list_move_tail(head, list);
371 * list_is_singular - tests whether a list has just one entry.
372 * @param head the list to test.
374 static inline int list_is_singular(const struct list_head *head)
376 return !list_empty(head) && (head->next == head->prev);
379 static inline void __list_cut_position(struct list_head *list,
380 struct list_head *head, struct list_head *entry)
382 struct list_head *new_first = entry->next;
383 list->next = head->next;
384 list->next->prev = list;
387 head->next = new_first;
388 new_first->prev = head;
392 * list_cut_position - cut a list into two
393 * @param list a new list to add all removed entries
394 * @param head a list with entries
395 * @param entry an entry within head, could be the head itself
396 * and if so we won't cut the list
398 * This helper moves the initial part of @a head, up to and
399 * including @a entry, from @a head to @a list. You should
400 * pass on @a entry an element you know is on @a head. @a list
401 * should be an empty list or a list you do not care about
405 static inline void list_cut_position(struct list_head *list,
406 struct list_head *head, struct list_head *entry)
408 if (list_empty(head))
410 if (list_is_singular(head) &&
411 (head->next != entry && head != entry))
414 INIT_LIST_HEAD(list);
416 __list_cut_position(list, head, entry);
420 * list_cut_before - cut a list into two, before given entry
421 * @param list a new list to add all removed entries
422 * @param head a list with entries
423 * @param entry an entry within head, could be the head itself
425 * This helper moves the initial part of @a head, up to but
426 * excluding @a entry, from @a head to @a list. You should pass
427 * in @a entry an element you know is on @a head. @a list should
428 * be an empty list or a list you do not care about losing
430 * If @a entry == @a head, all entries on @a head are moved to
433 static inline void list_cut_before(struct list_head *list,
434 struct list_head *head,
435 struct list_head *entry)
437 if (head->next == entry) {
438 INIT_LIST_HEAD(list);
441 list->next = head->next;
442 list->next->prev = list;
443 list->prev = entry->prev;
444 list->prev->next = list;
449 static inline void __list_splice(const struct list_head *list,
450 struct list_head *prev,
451 struct list_head *next)
453 struct list_head *first = list->next;
454 struct list_head *last = list->prev;
464 * list_splice - join two lists, this is designed for stacks
465 * @param list the new list to add.
466 * @param head the place to add it in the first list.
468 static inline void list_splice(const struct list_head *list,
469 struct list_head *head)
471 if (!list_empty(list))
472 __list_splice(list, head, head->next);
476 * list_splice_tail - join two lists, each list being a queue
477 * @param list the new list to add.
478 * @param head the place to add it in the first list.
480 static inline void list_splice_tail(struct list_head *list,
481 struct list_head *head)
483 if (!list_empty(list))
484 __list_splice(list, head->prev, head);
488 * list_splice_init - join two lists and reinitialise the emptied list.
489 * @param list the new list to add.
490 * @param head the place to add it in the first list.
492 * The list at @a list is reinitialised
494 static inline void list_splice_init(struct list_head *list,
495 struct list_head *head)
497 if (!list_empty(list)) {
498 __list_splice(list, head, head->next);
499 INIT_LIST_HEAD(list);
504 * list_splice_tail_init - join two lists and reinitialise the emptied list
505 * @param list the new list to add.
506 * @param head the place to add it in the first list.
508 * Each of the lists is a queue.
509 * The list at @a list is reinitialised
511 static inline void list_splice_tail_init(struct list_head *list,
512 struct list_head *head)
514 if (!list_empty(list)) {
515 __list_splice(list, head->prev, head);
516 INIT_LIST_HEAD(list);
521 * list_entry - get the struct for this entry
522 * @param ptr the &struct list_head pointer.
523 * @param type the type of the struct this is embedded in.
524 * @param member the name of the list_head within the struct.
526 #define list_entry(ptr, type, member) \
527 container_of(ptr, type, member)
530 * list_first_entry - get the first element from a list
531 * @param ptr the list head to take the element from.
532 * @param type the type of the struct this is embedded in.
533 * @param member the name of the list_head within the struct.
535 * Note, that list is expected to be not empty.
537 #define list_first_entry(ptr, type, member) \
538 list_entry((ptr)->next, type, member)
541 * list_last_entry - get the last element from a list
542 * @param ptr the list head to take the element from.
543 * @param type the type of the struct this is embedded in.
544 * @param member the name of the list_head within the struct.
546 * Note, that list is expected to be not empty.
548 #define list_last_entry(ptr, type, member) \
549 list_entry((ptr)->prev, type, member)
552 * list_first_entry_or_null - get the first element from a list
553 * @param ptr the list head to take the element from.
554 * @param type the type of the struct this is embedded in.
555 * @param member the name of the list_head within the struct.
557 * Note that if the list is empty, it returns NULL.
559 #define list_first_entry_or_null(ptr, type, member) ({ \
560 struct list_head *head__ = (ptr); \
561 struct list_head *pos__ = head__->next; \
562 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
566 * list_next_entry - get the next element in list
567 * @param pos the type * to cursor
568 * @param member the name of the list_head within the struct.
570 #define list_next_entry(pos, member) \
571 list_entry((pos)->member.next, typeof(*(pos)), member)
574 * list_prev_entry - get the prev element in list
575 * @param pos the type * to cursor
576 * @param member the name of the list_head within the struct.
578 #define list_prev_entry(pos, member) \
579 list_entry((pos)->member.prev, typeof(*(pos)), member)
582 * list_for_each - iterate over a list
583 * @param pos the &struct list_head to use as a loop cursor.
584 * @param head the head for your list.
586 #define list_for_each(pos, head) \
587 for (pos = (head)->next; pos != (head); pos = pos->next)
590 * list_for_each_continue - continue iteration over a list
591 * @param pos the &struct list_head to use as a loop cursor.
592 * @param head the head for your list.
594 * Continue to iterate over a list, continuing after the current position.
596 #define list_for_each_continue(pos, head) \
597 for (pos = pos->next; pos != (head); pos = pos->next)
600 * list_for_each_prev - iterate over a list backwards
601 * @param pos the &struct list_head to use as a loop cursor.
602 * @param head the head for your list.
604 #define list_for_each_prev(pos, head) \
605 for (pos = (head)->prev; pos != (head); pos = pos->prev)
608 * list_for_each_safe - iterate over a list safe against removal of list entry
609 * @param pos the &struct list_head to use as a loop cursor.
610 * @param n another &struct list_head to use as temporary storage
611 * @param head the head for your list.
613 #define list_for_each_safe(pos, n, head) \
614 for (pos = (head)->next, n = pos->next; pos != (head); \
615 pos = n, n = pos->next)
618 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
619 * @param pos the &struct list_head to use as a loop cursor.
620 * @param n another &struct list_head to use as temporary storage
621 * @param head the head for your list.
623 #define list_for_each_prev_safe(pos, n, head) \
624 for (pos = (head)->prev, n = pos->prev; \
626 pos = n, n = pos->prev)
629 * list_entry_is_head - test if the entry points to the head of the list
630 * @param pos the type * to cursor
631 * @param head the head for your list.
632 * @param member the name of the list_head within the struct.
634 #define list_entry_is_head(pos, head, member) \
635 (&pos->member == (head))
638 * list_for_each_entry - iterate over list of given type
639 * @param pos the type * to use as a loop cursor.
640 * @param head the head for your list.
641 * @param member the name of the list_head within the struct.
643 #define list_for_each_entry(pos, head, member) \
644 for (pos = list_first_entry(head, typeof(*pos), member); \
645 !list_entry_is_head(pos, head, member); \
646 pos = list_next_entry(pos, member))
649 * list_for_each_entry_reverse - iterate backwards over list of given type.
650 * @param pos the type * to use as a loop cursor.
651 * @param head the head for your list.
652 * @param member the name of the list_head within the struct.
654 #define list_for_each_entry_reverse(pos, head, member) \
655 for (pos = list_last_entry(head, typeof(*pos), member); \
656 !list_entry_is_head(pos, head, member); \
657 pos = list_prev_entry(pos, member))
660 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
661 * @param pos the type * to use as a start point
662 * @param head the head of the list
663 * @param member the name of the list_head within the struct.
665 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
667 #define list_prepare_entry(pos, head, member) \
668 ((pos) ? (pos) : list_entry(head, typeof(*pos), member))
671 * list_for_each_entry_continue - continue iteration over list of given type
672 * @param pos the type * to use as a loop cursor.
673 * @param head the head for your list.
674 * @param member the name of the list_head within the struct.
676 * Continue to iterate over list of given type, continuing after
677 * the current position.
679 #define list_for_each_entry_continue(pos, head, member) \
680 for (pos = list_next_entry(pos, member); \
681 !list_entry_is_head(pos, head, member); \
682 pos = list_next_entry(pos, member))
685 * list_for_each_entry_continue_reverse - iterate backwards from the given point
686 * @param pos the type * to use as a loop cursor.
687 * @param head the head for your list.
688 * @param member the name of the list_head within the struct.
690 * Start to iterate over list of given type backwards, continuing after
691 * the current position.
693 #define list_for_each_entry_continue_reverse(pos, head, member) \
694 for (pos = list_prev_entry(pos, member); \
695 !list_entry_is_head(pos, head, member); \
696 pos = list_prev_entry(pos, member))
699 * list_for_each_entry_from - iterate over list of given type from the current point
700 * @param pos the type * to use as a loop cursor.
701 * @param head the head for your list.
702 * @param member the name of the list_head within the struct.
704 * Iterate over list of given type, continuing from current position.
706 #define list_for_each_entry_from(pos, head, member) \
707 for (; !list_entry_is_head(pos, head, member); \
708 pos = list_next_entry(pos, member))
711 * list_for_each_entry_from_reverse - iterate backwards over list of given type
712 * from the current point
713 * @param pos the type * to use as a loop cursor.
714 * @param head the head for your list.
715 * @param member the name of the list_head within the struct.
717 * Iterate backwards over list of given type, continuing from current position.
719 #define list_for_each_entry_from_reverse(pos, head, member) \
720 for (; !list_entry_is_head(pos, head, member); \
721 pos = list_prev_entry(pos, member))
724 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
725 * @param pos the type * to use as a loop cursor.
726 * @param n another type * to use as temporary storage
727 * @param head the head for your list.
728 * @param member the name of the list_head within the struct.
730 #define list_for_each_entry_safe(pos, n, head, member) \
731 for (pos = list_first_entry(head, typeof(*pos), member), \
732 n = list_next_entry(pos, member); \
733 !list_entry_is_head(pos, head, member); \
734 pos = n, n = list_next_entry(n, member))
737 * list_for_each_entry_safe_continue - continue list iteration safe against removal
738 * @param pos the type * to use as a loop cursor.
739 * @param n another type * to use as temporary storage
740 * @param head the head for your list.
741 * @param member the name of the list_head within the struct.
743 * Iterate over list of given type, continuing after current point,
744 * safe against removal of list entry.
746 #define list_for_each_entry_safe_continue(pos, n, head, member) \
747 for (pos = list_next_entry(pos, member), \
748 n = list_next_entry(pos, member); \
749 !list_entry_is_head(pos, head, member); \
750 pos = n, n = list_next_entry(n, member))
753 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
754 * @param pos the type * to use as a loop cursor.
755 * @param n another type * to use as temporary storage
756 * @param head the head for your list.
757 * @param member the name of the list_head within the struct.
759 * Iterate over list of given type from current point, safe against
760 * removal of list entry.
762 #define list_for_each_entry_safe_from(pos, n, head, member) \
763 for (n = list_next_entry(pos, member); \
764 !list_entry_is_head(pos, head, member); \
765 pos = n, n = list_next_entry(n, member))
768 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
769 * @param pos the type * to use as a loop cursor.
770 * @param n another type * to use as temporary storage
771 * @param head the head for your list.
772 * @param member the name of the list_head within the struct.
774 * Iterate backwards over list of given type, safe against removal
777 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
778 for (pos = list_last_entry(head, typeof(*pos), member), \
779 n = list_prev_entry(pos, member); \
780 !list_entry_is_head(pos, head, member); \
781 pos = n, n = list_prev_entry(n, member))
784 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
785 * @param pos the loop cursor used in the list_for_each_entry_safe loop
786 * @param n temporary storage used in list_for_each_entry_safe
787 * @param member the name of the list_head within the struct.
789 * list_safe_reset_next is not safe to use in general if the list may be
790 * modified concurrently (eg. the lock is dropped in the loop body). An
791 * exception to this is if the cursor element (pos) is pinned in the list,
792 * and list_safe_reset_next is called after re-taking the lock and before
793 * completing the current iteration of the loop body.
795 #define list_safe_reset_next(pos, n, member) \
796 n = list_next_entry(pos, member)
799 * Double linked lists with a single pointer list head.
800 * Mostly useful for hash tables where the two pointer list head is
802 * You lose the ability to access the tail in O(1).
805 #define HLIST_HEAD_INIT { .first = NULL }
806 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
807 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
808 static inline void INIT_HLIST_NODE(struct hlist_node *h)
815 * hlist_unhashed - Has node been removed from list and reinitialized?
816 * @param h Node to be checked
818 * Not that not all removal functions will leave a node in unhashed
819 * state. For example, hlist_nulls_del_init_rcu() does leave the
820 * node in unhashed state, but hlist_nulls_del() does not.
822 static inline int hlist_unhashed(const struct hlist_node *h)
827 /* Ignore kernel hlist_unhashed_lockless() */
830 * hlist_empty - Is the specified hlist_head structure an empty hlist?
831 * @param h Structure to check.
833 static inline int hlist_empty(const struct hlist_head *h)
838 static inline void __hlist_del(struct hlist_node *n)
840 struct hlist_node *next = n->next;
841 struct hlist_node **pprev = n->pprev;
849 * hlist_del - Delete the specified hlist_node from its list
850 * @param n Node to delete.
852 * Note that this function leaves the node in hashed state. Use
853 * hlist_del_init() or similar instead to unhash @a n.
855 static inline void hlist_del(struct hlist_node *n)
858 n->next = LIST_POISON1;
859 n->pprev = LIST_POISON2;
863 * hlist_del_init - Delete the specified hlist_node from its list and initialize
864 * @param n Node to delete.
866 * Note that this function leaves the node in unhashed state.
868 static inline void hlist_del_init(struct hlist_node *n)
870 if (!hlist_unhashed(n)) {
877 * hlist_add_head - add a new entry at the beginning of the hlist
878 * @param n new entry to be added
879 * @param h hlist head to add it after
881 * Insert a new entry after the specified head.
882 * This is good for implementing stacks.
884 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
886 struct hlist_node *first = h->first;
889 first->pprev = &n->next;
891 n->pprev = &h->first;
895 * hlist_add_before - add a new entry before the one specified
896 * @param n new entry to be added
897 * @param next hlist node to add it before, which must be non-NULL
899 static inline void hlist_add_before(struct hlist_node *n,
900 struct hlist_node *next)
902 n->pprev = next->pprev;
904 next->pprev = &n->next;
909 * hlist_add_behind - add a new entry after the one specified
910 * @param n new entry to be added
911 * @param prev hlist node to add it after, which must be non-NULL
913 static inline void hlist_add_behind(struct hlist_node *n,
914 struct hlist_node *prev)
916 n->next = prev->next;
918 n->pprev = &prev->next;
921 n->next->pprev = &n->next;
925 * hlist_add_fake - create a fake hlist consisting of a single headless node
926 * @param n Node to make a fake list out of
928 * This makes @a n appear to be its own predecessor on a headless hlist.
929 * The point of this is to allow things like hlist_del() to work correctly
930 * in cases where there is no list.
932 static inline void hlist_add_fake(struct hlist_node *n)
938 * hlist_fake: Is this node a fake hlist?
939 * @param h Node to check for being a self-referential fake hlist.
941 static inline bool hlist_fake(struct hlist_node *h)
943 return h->pprev == &h->next;
947 * hlist_is_singular_node - is node the only element of the specified hlist?
948 * @param n Node to check for singularity.
949 * @param h Header for potentially singular list.
951 * Check whether the node is the only node of the head without
952 * accessing head, thus avoiding unnecessary cache misses.
955 hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
957 return !n->next && n->pprev == &h->first;
961 * hlist_move_list - Move an hlist
962 * @param old hlist_head for old list.
963 * @param new hlist_head for new list.
965 * Move a list from one list head to another. Fixup the pprev
966 * reference of the first entry if it exists.
968 static inline void hlist_move_list(struct hlist_head *old,
969 struct hlist_head *new)
971 new->first = old->first;
973 new->first->pprev = &new->first;
977 #define hlist_entry(ptr, type, member) container_of(ptr, type, member)
979 #define hlist_for_each(pos, head) \
980 for (pos = (head)->first; pos ; pos = pos->next)
982 #define hlist_for_each_safe(pos, n, head) \
983 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
986 #define hlist_entry_safe(ptr, type, member) \
987 ({ typeof(ptr) ____ptr = (ptr); \
988 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
992 * hlist_for_each_entry - iterate over list of given type
993 * @param pos the type * to use as a loop cursor.
994 * @param head the head for your list.
995 * @param member the name of the hlist_node within the struct.
997 #define hlist_for_each_entry(pos, head, member) \
998 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
1000 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1003 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1004 * @param pos the type * to use as a loop cursor.
1005 * @param member the name of the hlist_node within the struct.
1007 #define hlist_for_each_entry_continue(pos, member) \
1008 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
1010 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1013 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1014 * @param pos the type * to use as a loop cursor.
1015 * @param member the name of the hlist_node within the struct.
1017 #define hlist_for_each_entry_from(pos, member) \
1019 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1022 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1023 * @param pos the type * to use as a loop cursor.
1024 * @param n a &struct hlist_node to use as temporary storage
1025 * @param head the head for your list.
1026 * @param member the name of the hlist_node within the struct.
1028 #define hlist_for_each_entry_safe(pos, n, head, member) \
1029 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
1030 pos && ({ n = pos->member.next; 1; }); \
1031 pos = hlist_entry_safe(n, typeof(*pos), member))
1033 #endif /* OPENOCD_HELPER_LIST_H */