sbi_list.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Simple doubly-linked list library.
  5. *
  6. * Adapted from Xvisor source file libs/include/libs/list.h
  7. *
  8. * Copyright (c) 2020 Western Digital Corporation or its affiliates.
  9. *
  10. * Authors:
  11. * Anup Patel <anup.patel@wdc.com>
  12. */
  13. #ifndef __SBI_LIST_H__
  14. #define __SBI_LIST_H__
  15. #include <sbi/sbi_types.h>
  16. #define SBI_LIST_POISON_PREV 0xDEADBEEF
  17. #define SBI_LIST_POISON_NEXT 0xFADEBABE
  18. struct sbi_dlist {
  19. struct sbi_dlist *next, *prev;
  20. };
  21. #define SBI_LIST_HEAD_INIT(__lname) { &(__lname), &(__lname) }
  22. #define SBI_LIST_HEAD(_lname) \
  23. struct sbi_dlist _lname = SBI_LIST_HEAD_INIT(_lname)
  24. #define SBI_INIT_LIST_HEAD(ptr) \
  25. do { \
  26. (ptr)->next = ptr; (ptr)->prev = ptr; \
  27. } while (0);
  28. static inline void __sbi_list_add(struct sbi_dlist *new,
  29. struct sbi_dlist *prev,
  30. struct sbi_dlist *next)
  31. {
  32. new->prev = prev;
  33. new->next = next;
  34. prev->next = new;
  35. next->prev = new;
  36. }
  37. /**
  38. * Adds the new node after the given head.
  39. * @param new New node that needs to be added to list.
  40. * @param head List head after which the "new" node should be added.
  41. * Note: the new node is added after the head.
  42. */
  43. static inline void sbi_list_add(struct sbi_dlist *new, struct sbi_dlist *head)
  44. {
  45. __sbi_list_add(new, head, head->next);
  46. }
  47. /**
  48. * Adds a node at the tail where tnode points to tail node.
  49. * @param new The new node to be added before tail.
  50. * @param tnode The current tail node.
  51. * Note: the new node is added before tail node.
  52. */
  53. static inline void sbi_list_add_tail(struct sbi_dlist *new,
  54. struct sbi_dlist *tnode)
  55. {
  56. __sbi_list_add(new, tnode->prev, tnode);
  57. }
  58. static inline void __sbi_list_del(struct sbi_dlist *prev,
  59. struct sbi_dlist *next)
  60. {
  61. prev->next = next;
  62. next->prev = prev;
  63. }
  64. static inline void __sbi_list_del_entry(struct sbi_dlist *entry)
  65. {
  66. __sbi_list_del(entry->prev, entry->next);
  67. }
  68. /**
  69. * Deletes a given entry from list.
  70. * @param node Node to be deleted.
  71. */
  72. static inline void sbi_list_del(struct sbi_dlist *entry)
  73. {
  74. __sbi_list_del(entry->prev, entry->next);
  75. entry->next = (void *)SBI_LIST_POISON_NEXT;
  76. entry->prev = (void *)SBI_LIST_POISON_PREV;
  77. }
  78. /**
  79. * Deletes entry from list and reinitialize it.
  80. * @param entry the element to delete from the list.
  81. */
  82. static inline void sbi_list_del_init(struct sbi_dlist *entry)
  83. {
  84. __sbi_list_del_entry(entry);
  85. SBI_INIT_LIST_HEAD(entry);
  86. }
  87. /**
  88. * Get the struct for this entry
  89. * @param ptr the &struct list_head pointer.
  90. * @param type the type of the struct this is embedded in.
  91. * @param member the name of the list_struct within the struct.
  92. */
  93. #define sbi_list_entry(ptr, type, member) \
  94. container_of(ptr, type, member)
  95. /**
  96. * Get the first element from a list
  97. * @param ptr the list head to take the element from.
  98. * @param type the type of the struct this is embedded in.
  99. * @param member the name of the list_struct within the struct.
  100. *
  101. * Note: that list is expected to be not empty.
  102. */
  103. #define sbi_list_first_entry(ptr, type, member) \
  104. sbi_list_entry((ptr)->next, type, member)
  105. /**
  106. * Get the last element from a list
  107. * @param ptr the list head to take the element from.
  108. * @param type the type of the struct this is embedded in.
  109. * @param member the name of the list_head within the struct.
  110. *
  111. * Note: that list is expected to be not empty.
  112. */
  113. #define sbi_list_last_entry(ptr, type, member) \
  114. sbi_list_entry((ptr)->prev, type, member)
  115. /**
  116. * Iterate over a list
  117. * @param pos the &struct list_head to use as a loop cursor.
  118. * @param head the head for your list.
  119. */
  120. #define sbi_list_for_each(pos, head) \
  121. for (pos = (head)->next; pos != (head); pos = pos->next)
  122. /**
  123. * Iterate over list of given type
  124. * @param pos the type * to use as a loop cursor.
  125. * @param head the head for your list.
  126. * @param member the name of the list_struct within the struct.
  127. */
  128. #define sbi_list_for_each_entry(pos, head, member) \
  129. for (pos = sbi_list_entry((head)->next, typeof(*pos), member); \
  130. &pos->member != (head); \
  131. pos = sbi_list_entry(pos->member.next, typeof(*pos), member))
  132. #endif