dl_list.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2013-2014, 2017, 2019 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for
  6. * any purpose with or without fee is hereby granted, provided that the
  7. * above copyright notice and this permission notice appear in all
  8. * copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  13. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  15. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. /*=========================================================================== */
  20. /* Double-link list definitions (adapted from Atheros SDIO stack) */
  21. /* */
  22. /* Author(s): ="Atheros" */
  23. /*=========================================================================== */
  24. #ifndef __DL_LIST_H___
  25. #define __DL_LIST_H___
  26. #include "qdf_util.h"
  27. #define A_CONTAINING_STRUCT(address, struct_type, field_name) \
  28. (qdf_container_of(address, struct_type, field_name))
  29. /* list functions */
  30. /* pointers for the list */
  31. typedef struct _DL_LIST {
  32. struct _DL_LIST *pPrev;
  33. struct _DL_LIST *pNext;
  34. } DL_LIST, *PDL_LIST;
  35. /*
  36. * DL_LIST_INIT , initialize doubly linked list
  37. */
  38. #define DL_LIST_INIT(pList) \
  39. {(pList)->pPrev = pList; (pList)->pNext = pList; }
  40. /* faster macro to init list and add a single item */
  41. #define DL_LIST_INIT_AND_ADD(pList, pItem) \
  42. { (pList)->pPrev = (pItem); \
  43. (pList)->pNext = (pItem); \
  44. (pItem)->pNext = (pList); \
  45. (pItem)->pPrev = (pList); \
  46. }
  47. #define DL_LIST_IS_EMPTY(pList) (((pList)->pPrev == (pList)) && \
  48. ((pList)->pNext == (pList)))
  49. #define DL_LIST_GET_ITEM_AT_HEAD(pList) (pList)->pNext
  50. #define DL_LIST_GET_ITEM_AT_TAIL(pList) (pList)->pPrev
  51. /*
  52. * ITERATE_OVER_LIST pStart is the list, pTemp is a temp list member
  53. * NOT: do not use this function if the items in the list are deleted inside the
  54. * iteration loop
  55. */
  56. #define ITERATE_OVER_LIST(pStart, pTemp) \
  57. for ((pTemp) = (pStart)->pNext; pTemp != (pStart); \
  58. (pTemp) = (pTemp)->pNext)
  59. static inline bool dl_list_is_entry_in_list(const DL_LIST *pList,
  60. const DL_LIST *pEntry)
  61. {
  62. const DL_LIST *pTmp;
  63. if (pList == pEntry)
  64. return true;
  65. ITERATE_OVER_LIST(pList, pTmp) {
  66. if (pTmp == pEntry)
  67. return true;
  68. }
  69. return false;
  70. }
  71. /* safe iterate macro that allows the item to be removed from the list
  72. * the iteration continues to the next item in the list
  73. */
  74. #define ITERATE_OVER_LIST_ALLOW_REMOVE(pStart, pItem, st, offset) \
  75. { \
  76. PDL_LIST pTemp; \
  77. { pTemp = (pStart)->pNext; } \
  78. while (pTemp != (pStart)) { \
  79. { (pItem) = A_CONTAINING_STRUCT(pTemp, st, offset); } \
  80. { pTemp = pTemp->pNext; } \
  81. #define ITERATE_IS_VALID(pStart) dl_list_is_entry_in_list(pStart, pTemp)
  82. #define ITERATE_RESET(pStart) { pTemp = (pStart)->pNext; }
  83. #define ITERATE_END }}
  84. /*
  85. * dl_list_insert_tail - insert pAdd to the end of the list
  86. */
  87. static inline PDL_LIST dl_list_insert_tail(PDL_LIST pList, PDL_LIST pAdd)
  88. {
  89. /* insert at tail */
  90. pAdd->pPrev = pList->pPrev;
  91. pAdd->pNext = pList;
  92. if (pList->pPrev)
  93. pList->pPrev->pNext = pAdd;
  94. pList->pPrev = pAdd;
  95. return pAdd;
  96. }
  97. /*
  98. * dl_list_insert_head - insert pAdd into the head of the list
  99. */
  100. static inline PDL_LIST dl_list_insert_head(PDL_LIST pList, PDL_LIST pAdd)
  101. {
  102. /* insert at head */
  103. pAdd->pPrev = pList;
  104. pAdd->pNext = pList->pNext;
  105. pList->pNext->pPrev = pAdd;
  106. pList->pNext = pAdd;
  107. return pAdd;
  108. }
  109. #define DL_ListAdd(pList, pItem) dl_list_insert_head((pList), (pItem))
  110. /*
  111. * dl_list_remove - remove pDel from list
  112. */
  113. static inline PDL_LIST dl_list_remove(PDL_LIST pDel)
  114. {
  115. if (pDel->pNext)
  116. pDel->pNext->pPrev = pDel->pPrev;
  117. if (pDel->pPrev)
  118. pDel->pPrev->pNext = pDel->pNext;
  119. /* point back to itself just to be safe, if remove is called again */
  120. pDel->pNext = pDel;
  121. pDel->pPrev = pDel;
  122. return pDel;
  123. }
  124. /*
  125. * dl_list_remove_item_from_head - get a list item from the head
  126. */
  127. static inline PDL_LIST dl_list_remove_item_from_head(PDL_LIST pList)
  128. {
  129. PDL_LIST pItem = NULL;
  130. if (pList->pNext != pList) {
  131. pItem = pList->pNext;
  132. /* remove the first item from head */
  133. dl_list_remove(pItem);
  134. }
  135. return pItem;
  136. }
  137. static inline PDL_LIST dl_list_remove_item_from_tail(PDL_LIST pList)
  138. {
  139. PDL_LIST pItem = NULL;
  140. if (pList->pPrev != pList) {
  141. pItem = pList->pPrev;
  142. /* remove the item from tail */
  143. dl_list_remove(pItem);
  144. }
  145. return pItem;
  146. }
  147. /* transfer src list items to the tail of the destination list */
  148. static inline void dl_list_transfer_items_to_tail(PDL_LIST pDest, PDL_LIST pSrc)
  149. {
  150. /* only concatenate if src is not empty */
  151. if (!DL_LIST_IS_EMPTY(pSrc)) {
  152. /* cut out circular list in src and re-attach to end of dest */
  153. pSrc->pPrev->pNext = pDest;
  154. pSrc->pNext->pPrev = pDest->pPrev;
  155. pDest->pPrev->pNext = pSrc->pNext;
  156. pDest->pPrev = pSrc->pPrev;
  157. /* terminate src list, it is now empty */
  158. pSrc->pPrev = pSrc;
  159. pSrc->pNext = pSrc;
  160. }
  161. }
  162. /* transfer src list items to the head of the destination list */
  163. static inline void dl_list_transfer_items_to_head(PDL_LIST pDest, PDL_LIST pSrc)
  164. {
  165. /* only concatenate if src is not empty */
  166. if (!DL_LIST_IS_EMPTY(pSrc)) {
  167. /* cut out circular list in src and reattach to start of dest */
  168. pSrc->pNext->pPrev = pDest;
  169. pDest->pNext->pPrev = pSrc->pPrev;
  170. pSrc->pPrev->pNext = pDest->pNext;
  171. pDest->pNext = pSrc->pNext;
  172. /* terminate src list, it is now empty */
  173. pSrc->pPrev = pSrc;
  174. pSrc->pNext = pSrc;
  175. }
  176. }
  177. #endif /* __DL_LIST_H___ */