sbi_list.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * Checks if the list is empty or not.
  39. * @param head List head
  40. *
  41. * Returns true if list is empty, false otherwise.
  42. */
  43. static inline bool sbi_list_empty(struct sbi_dlist *head)
  44. {
  45. return head->next == head;
  46. }
  47. /**
  48. * Adds the new node after the given head.
  49. * @param new New node that needs to be added to list.
  50. * @param head List head after which the "new" node should be added.
  51. * Note: the new node is added after the head.
  52. */
  53. static inline void sbi_list_add(struct sbi_dlist *new, struct sbi_dlist *head)
  54. {
  55. __sbi_list_add(new, head, head->next);
  56. }
  57. /**
  58. * Adds a node at the tail where tnode points to tail node.
  59. * @param new The new node to be added before tail.
  60. * @param tnode The current tail node.
  61. * Note: the new node is added before tail node.
  62. */
  63. static inline void sbi_list_add_tail(struct sbi_dlist *new,
  64. struct sbi_dlist *tnode)
  65. {
  66. __sbi_list_add(new, tnode->prev, tnode);
  67. }
  68. static inline void __sbi_list_del(struct sbi_dlist *prev,
  69. struct sbi_dlist *next)
  70. {
  71. prev->next = next;
  72. next->prev = prev;
  73. }
  74. static inline void __sbi_list_del_entry(struct sbi_dlist *entry)
  75. {
  76. __sbi_list_del(entry->prev, entry->next);
  77. }
  78. /**
  79. * Deletes a given entry from list.
  80. * @param node Node to be deleted.
  81. */
  82. static inline void sbi_list_del(struct sbi_dlist *entry)
  83. {
  84. __sbi_list_del(entry->prev, entry->next);
  85. entry->next = (void *)SBI_LIST_POISON_NEXT;
  86. entry->prev = (void *)SBI_LIST_POISON_PREV;
  87. }
  88. /**
  89. * Deletes entry from list and reinitialize it.
  90. * @param entry the element to delete from the list.
  91. */
  92. static inline void sbi_list_del_init(struct sbi_dlist *entry)
  93. {
  94. __sbi_list_del_entry(entry);
  95. SBI_INIT_LIST_HEAD(entry);
  96. }
  97. /**
  98. * Get the struct for this entry
  99. * @param ptr the &struct list_head pointer.
  100. * @param type the type of the struct this is embedded in.
  101. * @param member the name of the list_struct within the struct.
  102. */
  103. #define sbi_list_entry(ptr, type, member) \
  104. container_of(ptr, type, member)
  105. /**
  106. * Get the first 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_struct within the struct.
  110. *
  111. * Note: that list is expected to be not empty.
  112. */
  113. #define sbi_list_first_entry(ptr, type, member) \
  114. sbi_list_entry((ptr)->next, type, member)
  115. /**
  116. * Get the last element from a list
  117. * @param ptr the list head to take the element from.
  118. * @param type the type of the struct this is embedded in.
  119. * @param member the name of the list_head within the struct.
  120. *
  121. * Note: that list is expected to be not empty.
  122. */
  123. #define sbi_list_last_entry(ptr, type, member) \
  124. sbi_list_entry((ptr)->prev, type, member)
  125. /**
  126. * Iterate over a list
  127. * @param pos the &struct list_head to use as a loop cursor.
  128. * @param head the head for your list.
  129. */
  130. #define sbi_list_for_each(pos, head) \
  131. for (pos = (head)->next; pos != (head); pos = pos->next)
  132. /**
  133. * Iterate over list of given type
  134. * @param pos the type * to use as a loop cursor.
  135. * @param head the head for your list.
  136. * @param member the name of the list_struct within the struct.
  137. */
  138. #define sbi_list_for_each_entry(pos, head, member) \
  139. for (pos = sbi_list_entry((head)->next, typeof(*pos), member); \
  140. &pos->member != (head); \
  141. pos = sbi_list_entry(pos->member.next, typeof(*pos), member))
  142. #endif