dl_list.h 5.9 KB

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