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.
15 #ifndef OPENOCD_HELPER_LIST_H
16 #define OPENOCD_HELPER_LIST_H
18 /* begin local changes */
19 #include <helper/types.h>
21 #define LIST_POISON1 NULL
22 #define LIST_POISON2 NULL
25 struct list_head *next, *prev;
29 struct hlist_node *first;
33 struct hlist_node *next, **pprev;
35 /* end local changes */
38 * Circular doubly linked list implementation.
40 * Some of the internal functions ("__xxx") are useful when
41 * manipulating whole lists rather than single entries, as
42 * sometimes we already know the next/prev entries and we can
43 * generate better code by using them directly rather than
44 * using the generic single-entry routines.
47 #define LIST_HEAD_INIT(name) { &(name), &(name) }
49 #define LIST_HEAD(name) \
50 struct list_head name = LIST_HEAD_INIT(name)
53 * INIT_LIST_HEAD - Initialize a list_head structure
54 * @param list list_head structure to be initialized.
56 * Initializes the list_head to point to itself. If it is a list header,
57 * the result is an empty list.
59 static inline void INIT_LIST_HEAD(struct list_head *list)
65 #ifdef CONFIG_DEBUG_LIST
66 extern bool __list_add_valid(struct list_head *new,
67 struct list_head *prev,
68 struct list_head *next);
69 extern bool __list_del_entry_valid(struct list_head *entry);
71 static inline bool __list_add_valid(struct list_head *new,
72 struct list_head *prev,
73 struct list_head *next)
77 static inline bool __list_del_entry_valid(struct list_head *entry)
84 * Insert a new entry between two known consecutive entries.
86 * This is only for internal list manipulation where we know
87 * the prev/next entries already!
89 static inline void __list_add(struct list_head *new,
90 struct list_head *prev,
91 struct list_head *next)
93 if (!__list_add_valid(new, prev, next))
103 * list_add - add a new entry
104 * @param new new entry to be added
105 * @param head list head to add it after
107 * Insert a new entry after the specified head.
108 * This is good for implementing stacks.
110 static inline void list_add(struct list_head *new, struct list_head *head)
112 __list_add(new, head, head->next);
117 * list_add_tail - add a new entry
118 * @param new new entry to be added
119 * @param head list head to add it before
121 * Insert a new entry before the specified head.
122 * This is useful for implementing queues.
124 static inline void list_add_tail(struct list_head *new, struct list_head *head)
126 __list_add(new, head->prev, head);
130 * Delete a list entry by making the prev/next entries
131 * point to each other.
133 * This is only for internal list manipulation where we know
134 * the prev/next entries already!
136 static inline void __list_del(struct list_head *prev, struct list_head *next)
142 /* Ignore kernel __list_del_clearprev() */
144 static inline void __list_del_entry(struct list_head *entry)
146 if (!__list_del_entry_valid(entry))
149 __list_del(entry->prev, entry->next);
153 * list_del - deletes entry from list.
154 * @param entry the element to delete from the list.
155 * Note: list_empty() on entry does not return true after this, the entry is
156 * in an undefined state.
158 static inline void list_del(struct list_head *entry)
160 __list_del_entry(entry);
161 entry->next = LIST_POISON1;
162 entry->prev = LIST_POISON2;
166 * list_replace - replace old entry by new one
167 * @param old the element to be replaced
168 * @param new the new element to insert
170 * If @a old was empty, it will be overwritten.
172 static inline void list_replace(struct list_head *old,
173 struct list_head *new)
175 new->next = old->next;
176 new->next->prev = new;
177 new->prev = old->prev;
178 new->prev->next = new;
182 * list_replace_init - replace old entry by new one and initialize the old one
183 * @param old the element to be replaced
184 * @param new the new element to insert
186 * If @a old was empty, it will be overwritten.
188 static inline void list_replace_init(struct list_head *old,
189 struct list_head *new)
191 list_replace(old, new);
196 * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
197 * @param entry1 the location to place entry2
198 * @param entry2 the location to place entry1
200 static inline void list_swap(struct list_head *entry1,
201 struct list_head *entry2)
203 struct list_head *pos = entry2->prev;
206 list_replace(entry1, entry2);
209 list_add(entry1, pos);
213 * list_del_init - deletes entry from list and reinitialize it.
214 * @param entry the element to delete from the list.
216 static inline void list_del_init(struct list_head *entry)
218 __list_del_entry(entry);
219 INIT_LIST_HEAD(entry);
223 * list_move - delete from one list and add as another's head
224 * @param list the entry to move
225 * @param head the head that will precede our entry
227 static inline void list_move(struct list_head *list, struct list_head *head)
229 __list_del_entry(list);
230 list_add(list, head);
234 * list_move_tail - delete from one list and add as another's tail
235 * @param list the entry to move
236 * @param head the head that will follow our entry
238 static inline void list_move_tail(struct list_head *list,
239 struct list_head *head)
241 __list_del_entry(list);
242 list_add_tail(list, head);
246 * list_bulk_move_tail - move a subsection of a list to its tail
247 * @param head the head that will follow our entry
248 * @param first the first entry to move
249 * @param last the last entry to move, can be the same as first
251 * Move all entries between @a first and including @a last before @a head.
252 * All three entries must belong to the same linked list.
254 static inline void list_bulk_move_tail(struct list_head *head,
255 struct list_head *first,
256 struct list_head *last)
258 first->prev->next = last->next;
259 last->next->prev = first->prev;
261 head->prev->next = first;
262 first->prev = head->prev;
269 * list_is_first -- tests whether @a list is the first entry in list @a head
270 * @param list the entry to test
271 * @param head the head of the list
273 static inline int list_is_first(const struct list_head *list,
274 const struct list_head *head)
276 return list->prev == head;
280 * list_is_last - tests whether @a list is the last entry in list @a head
281 * @param list the entry to test
282 * @param head the head of the list
284 static inline int list_is_last(const struct list_head *list,
285 const struct list_head *head)
287 return list->next == head;
291 * list_empty - tests whether a list is empty
292 * @param head the list to test.
294 static inline int list_empty(const struct list_head *head)
296 return head->next == head;
300 * list_del_init_careful - deletes entry from list and reinitialize it.
301 * @param entry the element to delete from the list.
303 * This is the same as list_del_init(), except designed to be used
304 * together with list_empty_careful() in a way to guarantee ordering
305 * of other memory operations.
307 * Any memory operations done before a list_del_init_careful() are
308 * guaranteed to be visible after a list_empty_careful() test.
310 static inline void list_del_init_careful(struct list_head *entry)
312 __list_del_entry(entry);
318 * list_empty_careful - tests whether a list is empty and not being modified
319 * @param head the list to test
322 * tests whether a list is empty _and_ checks that no other CPU might be
323 * in the process of modifying either member (next or prev)
325 * NOTE: using list_empty_careful() without synchronization
326 * can only be safe if the only activity that can happen
327 * to the list entry is list_del_init(). Eg. it cannot be used
328 * if another CPU could re-list_add() it.
330 static inline int list_empty_careful(const struct list_head *head)
332 struct list_head *next = head->next;
333 return (next == head) && (next == head->prev);
337 * list_rotate_left - rotate the list to the left
338 * @param head the head of the list
340 static inline void list_rotate_left(struct list_head *head)
342 struct list_head *first;
344 if (!list_empty(head)) {
346 list_move_tail(first, head);
351 * list_rotate_to_front() - Rotate list to specific item.
352 * @param list The desired new front of the list.
353 * @param head The head of the list.
355 * Rotates list so that @a list becomes the new front of the list.
357 static inline void list_rotate_to_front(struct list_head *list,
358 struct list_head *head)
361 * Deletes the list head from the list denoted by @a head and
362 * places it as the tail of @a list, this effectively rotates the
363 * list so that @a list is at the front.
365 list_move_tail(head, list);
369 * list_is_singular - tests whether a list has just one entry.
370 * @param head the list to test.
372 static inline int list_is_singular(const struct list_head *head)
374 return !list_empty(head) && (head->next == head->prev);
377 static inline void __list_cut_position(struct list_head *list,
378 struct list_head *head, struct list_head *entry)
380 struct list_head *new_first = entry->next;
381 list->next = head->next;
382 list->next->prev = list;
385 head->next = new_first;
386 new_first->prev = head;
390 * list_cut_position - cut a list into two
391 * @param list a new list to add all removed entries
392 * @param head a list with entries
393 * @param entry an entry within head, could be the head itself
394 * and if so we won't cut the list
396 * This helper moves the initial part of @a head, up to and
397 * including @a entry, from @a head to @a list. You should
398 * pass on @a entry an element you know is on @a head. @a list
399 * should be an empty list or a list you do not care about
403 static inline void list_cut_position(struct list_head *list,
404 struct list_head *head, struct list_head *entry)
406 if (list_empty(head))
408 if (list_is_singular(head) &&
409 (head->next != entry && head != entry))
412 INIT_LIST_HEAD(list);
414 __list_cut_position(list, head, entry);
418 * list_cut_before - cut a list into two, before given entry
419 * @param list a new list to add all removed entries
420 * @param head a list with entries
421 * @param entry an entry within head, could be the head itself
423 * This helper moves the initial part of @a head, up to but
424 * excluding @a entry, from @a head to @a list. You should pass
425 * in @a entry an element you know is on @a head. @a list should
426 * be an empty list or a list you do not care about losing
428 * If @a entry == @a head, all entries on @a head are moved to
431 static inline void list_cut_before(struct list_head *list,
432 struct list_head *head,
433 struct list_head *entry)
435 if (head->next == entry) {
436 INIT_LIST_HEAD(list);
439 list->next = head->next;
440 list->next->prev = list;
441 list->prev = entry->prev;
442 list->prev->next = list;
447 static inline void __list_splice(const struct list_head *list,
448 struct list_head *prev,
449 struct list_head *next)
451 struct list_head *first = list->next;
452 struct list_head *last = list->prev;
462 * list_splice - join two lists, this is designed for stacks
463 * @param list the new list to add.
464 * @param head the place to add it in the first list.
466 static inline void list_splice(const struct list_head *list,
467 struct list_head *head)
469 if (!list_empty(list))
470 __list_splice(list, head, head->next);
474 * list_splice_tail - join two lists, each list being a queue
475 * @param list the new list to add.
476 * @param head the place to add it in the first list.
478 static inline void list_splice_tail(struct list_head *list,
479 struct list_head *head)
481 if (!list_empty(list))
482 __list_splice(list, head->prev, head);
486 * list_splice_init - join two lists and reinitialise the emptied list.
487 * @param list the new list to add.
488 * @param head the place to add it in the first list.
490 * The list at @a list is reinitialised
492 static inline void list_splice_init(struct list_head *list,
493 struct list_head *head)
495 if (!list_empty(list)) {
496 __list_splice(list, head, head->next);
497 INIT_LIST_HEAD(list);
502 * list_splice_tail_init - join two lists and reinitialise the emptied list
503 * @param list the new list to add.
504 * @param head the place to add it in the first list.
506 * Each of the lists is a queue.
507 * The list at @a list is reinitialised
509 static inline void list_splice_tail_init(struct list_head *list,
510 struct list_head *head)
512 if (!list_empty(list)) {
513 __list_splice(list, head->prev, head);
514 INIT_LIST_HEAD(list);
519 * list_entry - get the struct for this entry
520 * @param ptr the &struct list_head pointer.
521 * @param type the type of the struct this is embedded in.
522 * @param member the name of the list_head within the struct.
524 #define list_entry(ptr, type, member) \
525 container_of(ptr, type, member)
528 * list_first_entry - get the first element from a list
529 * @param ptr the list head to take the element from.
530 * @param type the type of the struct this is embedded in.
531 * @param member the name of the list_head within the struct.
533 * Note, that list is expected to be not empty.
535 #define list_first_entry(ptr, type, member) \
536 list_entry((ptr)->next, type, member)
539 * list_last_entry - get the last element from a list
540 * @param ptr the list head to take the element from.
541 * @param type the type of the struct this is embedded in.
542 * @param member the name of the list_head within the struct.
544 * Note, that list is expected to be not empty.
546 #define list_last_entry(ptr, type, member) \
547 list_entry((ptr)->prev, type, member)
550 * list_first_entry_or_null - get the first element from a list
551 * @param ptr the list head to take the element from.
552 * @param type the type of the struct this is embedded in.
553 * @param member the name of the list_head within the struct.
555 * Note that if the list is empty, it returns NULL.
557 #define list_first_entry_or_null(ptr, type, member) ({ \
558 struct list_head *head__ = (ptr); \
559 struct list_head *pos__ = head__->next; \
560 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
564 * list_next_entry - get the next element in list
565 * @param pos the type * to cursor
566 * @param member the name of the list_head within the struct.
568 #define list_next_entry(pos, member) \
569 list_entry((pos)->member.next, typeof(*(pos)), member)
572 * list_prev_entry - get the prev element in list
573 * @param pos the type * to cursor
574 * @param member the name of the list_head within the struct.
576 #define list_prev_entry(pos, member) \
577 list_entry((pos)->member.prev, typeof(*(pos)), member)
580 * list_for_each - iterate over a list
581 * @param pos the &struct list_head to use as a loop cursor.
582 * @param head the head for your list.
584 #define list_for_each(pos, head) \
585 for (pos = (head)->next; pos != (head); pos = pos->next)
588 * list_for_each_continue - continue iteration over a list
589 * @param pos the &struct list_head to use as a loop cursor.
590 * @param head the head for your list.
592 * Continue to iterate over a list, continuing after the current position.
594 #define list_for_each_continue(pos, head) \
595 for (pos = pos->next; pos != (head); pos = pos->next)
598 * list_for_each_prev - iterate over a list backwards
599 * @param pos the &struct list_head to use as a loop cursor.
600 * @param head the head for your list.
602 #define list_for_each_prev(pos, head) \
603 for (pos = (head)->prev; pos != (head); pos = pos->prev)
606 * list_for_each_safe - iterate over a list safe against removal of list entry
607 * @param pos the &struct list_head to use as a loop cursor.
608 * @param n another &struct list_head to use as temporary storage
609 * @param head the head for your list.
611 #define list_for_each_safe(pos, n, head) \
612 for (pos = (head)->next, n = pos->next; pos != (head); \
613 pos = n, n = pos->next)
616 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
617 * @param pos the &struct list_head to use as a loop cursor.
618 * @param n another &struct list_head to use as temporary storage
619 * @param head the head for your list.
621 #define list_for_each_prev_safe(pos, n, head) \
622 for (pos = (head)->prev, n = pos->prev; \
624 pos = n, n = pos->prev)
627 * list_entry_is_head - test if the entry points to the head of the list
628 * @param pos the type * to cursor
629 * @param head the head for your list.
630 * @param member the name of the list_head within the struct.
632 #define list_entry_is_head(pos, head, member) \
633 (&pos->member == (head))
636 * list_for_each_entry - iterate over list of given type
637 * @param pos the type * to use as a loop cursor.
638 * @param head the head for your list.
639 * @param member the name of the list_head within the struct.
641 #define list_for_each_entry(pos, head, member) \
642 for (pos = list_first_entry(head, typeof(*pos), member); \
643 !list_entry_is_head(pos, head, member); \
644 pos = list_next_entry(pos, member))
647 * list_for_each_entry_reverse - iterate backwards over list of given type.
648 * @param pos the type * to use as a loop cursor.
649 * @param head the head for your list.
650 * @param member the name of the list_head within the struct.
652 #define list_for_each_entry_reverse(pos, head, member) \
653 for (pos = list_last_entry(head, typeof(*pos), member); \
654 !list_entry_is_head(pos, head, member); \
655 pos = list_prev_entry(pos, member))
658 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
659 * @param pos the type * to use as a start point
660 * @param head the head of the list
661 * @param member the name of the list_head within the struct.
663 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
665 #define list_prepare_entry(pos, head, member) \
666 ((pos) ? (pos) : list_entry(head, typeof(*pos), member))
669 * list_for_each_entry_continue - continue iteration over list of given type
670 * @param pos the type * to use as a loop cursor.
671 * @param head the head for your list.
672 * @param member the name of the list_head within the struct.
674 * Continue to iterate over list of given type, continuing after
675 * the current position.
677 #define list_for_each_entry_continue(pos, head, member) \
678 for (pos = list_next_entry(pos, member); \
679 !list_entry_is_head(pos, head, member); \
680 pos = list_next_entry(pos, member))
683 * list_for_each_entry_continue_reverse - iterate backwards from the given point
684 * @param pos the type * to use as a loop cursor.
685 * @param head the head for your list.
686 * @param member the name of the list_head within the struct.
688 * Start to iterate over list of given type backwards, continuing after
689 * the current position.
691 #define list_for_each_entry_continue_reverse(pos, head, member) \
692 for (pos = list_prev_entry(pos, member); \
693 !list_entry_is_head(pos, head, member); \
694 pos = list_prev_entry(pos, member))
697 * list_for_each_entry_from - iterate over list of given type from the current point
698 * @param pos the type * to use as a loop cursor.
699 * @param head the head for your list.
700 * @param member the name of the list_head within the struct.
702 * Iterate over list of given type, continuing from current position.
704 #define list_for_each_entry_from(pos, head, member) \
705 for (; !list_entry_is_head(pos, head, member); \
706 pos = list_next_entry(pos, member))
709 * list_for_each_entry_from_reverse - iterate backwards over list of given type
710 * from the current point
711 * @param pos the type * to use as a loop cursor.
712 * @param head the head for your list.
713 * @param member the name of the list_head within the struct.
715 * Iterate backwards over list of given type, continuing from current position.
717 #define list_for_each_entry_from_reverse(pos, head, member) \
718 for (; !list_entry_is_head(pos, head, member); \
719 pos = list_prev_entry(pos, member))
722 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
723 * @param pos the type * to use as a loop cursor.
724 * @param n another type * to use as temporary storage
725 * @param head the head for your list.
726 * @param member the name of the list_head within the struct.
728 #define list_for_each_entry_safe(pos, n, head, member) \
729 for (pos = list_first_entry(head, typeof(*pos), member), \
730 n = list_next_entry(pos, member); \
731 !list_entry_is_head(pos, head, member); \
732 pos = n, n = list_next_entry(n, member))
735 * list_for_each_entry_safe_continue - continue list iteration safe against removal
736 * @param pos the type * to use as a loop cursor.
737 * @param n another type * to use as temporary storage
738 * @param head the head for your list.
739 * @param member the name of the list_head within the struct.
741 * Iterate over list of given type, continuing after current point,
742 * safe against removal of list entry.
744 #define list_for_each_entry_safe_continue(pos, n, head, member) \
745 for (pos = list_next_entry(pos, member), \
746 n = list_next_entry(pos, member); \
747 !list_entry_is_head(pos, head, member); \
748 pos = n, n = list_next_entry(n, member))
751 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
752 * @param pos the type * to use as a loop cursor.
753 * @param n another type * to use as temporary storage
754 * @param head the head for your list.
755 * @param member the name of the list_head within the struct.
757 * Iterate over list of given type from current point, safe against
758 * removal of list entry.
760 #define list_for_each_entry_safe_from(pos, n, head, member) \
761 for (n = list_next_entry(pos, member); \
762 !list_entry_is_head(pos, head, member); \
763 pos = n, n = list_next_entry(n, member))
766 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
767 * @param pos the type * to use as a loop cursor.
768 * @param n another type * to use as temporary storage
769 * @param head the head for your list.
770 * @param member the name of the list_head within the struct.
772 * Iterate backwards over list of given type, safe against removal
775 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
776 for (pos = list_last_entry(head, typeof(*pos), member), \
777 n = list_prev_entry(pos, member); \
778 !list_entry_is_head(pos, head, member); \
779 pos = n, n = list_prev_entry(n, member))
782 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
783 * @param pos the loop cursor used in the list_for_each_entry_safe loop
784 * @param n temporary storage used in list_for_each_entry_safe
785 * @param member the name of the list_head within the struct.
787 * list_safe_reset_next is not safe to use in general if the list may be
788 * modified concurrently (eg. the lock is dropped in the loop body). An
789 * exception to this is if the cursor element (pos) is pinned in the list,
790 * and list_safe_reset_next is called after re-taking the lock and before
791 * completing the current iteration of the loop body.
793 #define list_safe_reset_next(pos, n, member) \
794 n = list_next_entry(pos, member)
797 * Double linked lists with a single pointer list head.
798 * Mostly useful for hash tables where the two pointer list head is
800 * You lose the ability to access the tail in O(1).
803 #define HLIST_HEAD_INIT { .first = NULL }
804 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
805 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
806 static inline void INIT_HLIST_NODE(struct hlist_node *h)
813 * hlist_unhashed - Has node been removed from list and reinitialized?
814 * @param h Node to be checked
816 * Not that not all removal functions will leave a node in unhashed
817 * state. For example, hlist_nulls_del_init_rcu() does leave the
818 * node in unhashed state, but hlist_nulls_del() does not.
820 static inline int hlist_unhashed(const struct hlist_node *h)
825 /* Ignore kernel hlist_unhashed_lockless() */
828 * hlist_empty - Is the specified hlist_head structure an empty hlist?
829 * @param h Structure to check.
831 static inline int hlist_empty(const struct hlist_head *h)
836 static inline void __hlist_del(struct hlist_node *n)
838 struct hlist_node *next = n->next;
839 struct hlist_node **pprev = n->pprev;
847 * hlist_del - Delete the specified hlist_node from its list
848 * @param n Node to delete.
850 * Note that this function leaves the node in hashed state. Use
851 * hlist_del_init() or similar instead to unhash @a n.
853 static inline void hlist_del(struct hlist_node *n)
856 n->next = LIST_POISON1;
857 n->pprev = LIST_POISON2;
861 * hlist_del_init - Delete the specified hlist_node from its list and initialize
862 * @param n Node to delete.
864 * Note that this function leaves the node in unhashed state.
866 static inline void hlist_del_init(struct hlist_node *n)
868 if (!hlist_unhashed(n)) {
875 * hlist_add_head - add a new entry at the beginning of the hlist
876 * @param n new entry to be added
877 * @param h hlist head to add it after
879 * Insert a new entry after the specified head.
880 * This is good for implementing stacks.
882 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
884 struct hlist_node *first = h->first;
887 first->pprev = &n->next;
889 n->pprev = &h->first;
893 * hlist_add_before - add a new entry before the one specified
894 * @param n new entry to be added
895 * @param next hlist node to add it before, which must be non-NULL
897 static inline void hlist_add_before(struct hlist_node *n,
898 struct hlist_node *next)
900 n->pprev = next->pprev;
902 next->pprev = &n->next;
907 * hlist_add_behind - add a new entry after the one specified
908 * @param n new entry to be added
909 * @param prev hlist node to add it after, which must be non-NULL
911 static inline void hlist_add_behind(struct hlist_node *n,
912 struct hlist_node *prev)
914 n->next = prev->next;
916 n->pprev = &prev->next;
919 n->next->pprev = &n->next;
923 * hlist_add_fake - create a fake hlist consisting of a single headless node
924 * @param n Node to make a fake list out of
926 * This makes @a n appear to be its own predecessor on a headless hlist.
927 * The point of this is to allow things like hlist_del() to work correctly
928 * in cases where there is no list.
930 static inline void hlist_add_fake(struct hlist_node *n)
936 * hlist_fake: Is this node a fake hlist?
937 * @param h Node to check for being a self-referential fake hlist.
939 static inline bool hlist_fake(struct hlist_node *h)
941 return h->pprev == &h->next;
945 * hlist_is_singular_node - is node the only element of the specified hlist?
946 * @param n Node to check for singularity.
947 * @param h Header for potentially singular list.
949 * Check whether the node is the only node of the head without
950 * accessing head, thus avoiding unnecessary cache misses.
953 hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
955 return !n->next && n->pprev == &h->first;
959 * hlist_move_list - Move an hlist
960 * @param old hlist_head for old list.
961 * @param new hlist_head for new list.
963 * Move a list from one list head to another. Fixup the pprev
964 * reference of the first entry if it exists.
966 static inline void hlist_move_list(struct hlist_head *old,
967 struct hlist_head *new)
969 new->first = old->first;
971 new->first->pprev = &new->first;
975 #define hlist_entry(ptr, type, member) container_of(ptr, type, member)
977 #define hlist_for_each(pos, head) \
978 for (pos = (head)->first; pos ; pos = pos->next)
980 #define hlist_for_each_safe(pos, n, head) \
981 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
984 #define hlist_entry_safe(ptr, type, member) \
985 ({ typeof(ptr) ____ptr = (ptr); \
986 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
990 * hlist_for_each_entry - iterate over list of given type
991 * @param pos the type * to use as a loop cursor.
992 * @param head the head for your list.
993 * @param member the name of the hlist_node within the struct.
995 #define hlist_for_each_entry(pos, head, member) \
996 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
998 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1001 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
1002 * @param pos the type * to use as a loop cursor.
1003 * @param member the name of the hlist_node within the struct.
1005 #define hlist_for_each_entry_continue(pos, member) \
1006 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
1008 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1011 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
1012 * @param pos the type * to use as a loop cursor.
1013 * @param member the name of the hlist_node within the struct.
1015 #define hlist_for_each_entry_from(pos, member) \
1017 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1020 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
1021 * @param pos the type * to use as a loop cursor.
1022 * @param n a &struct hlist_node to use as temporary storage
1023 * @param head the head for your list.
1024 * @param member the name of the hlist_node within the struct.
1026 #define hlist_for_each_entry_safe(pos, n, head, member) \
1027 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
1028 pos && ({ n = pos->member.next; 1; }); \
1029 pos = hlist_entry_safe(n, typeof(*pos), member))
1031 #endif /* OPENOCD_HELPER_LIST_H */