1 //------------------------------------------------------------------------------
2 // <copyright file="dl_list.h" company="Atheros">
3 // Copyright (c) 2004-2010 Atheros Corporation. All rights reserved.
6 // Permission to use, copy, modify, and/or distribute this software for any
7 // purpose with or without fee is hereby granted, provided that the above
8 // copyright notice and this permission notice appear in all copies.
10 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 //------------------------------------------------------------------------------
20 //==============================================================================
21 // Double-link list definitions (adapted from Atheros SDIO stack)
23 // Author(s): ="Atheros"
24 //==============================================================================
25 #ifndef __DL_LIST_H___
26 #define __DL_LIST_H___
30 #define A_CONTAINING_STRUCT(address, struct_type, field_name)\
31 ((struct_type *)((unsigned long)(address) - (unsigned long)(&((struct_type *)0)->field_name)))
34 /* pointers for the list */
35 typedef struct _DL_LIST {
36 struct _DL_LIST *pPrev;
37 struct _DL_LIST *pNext;
40 * DL_LIST_INIT , initialize doubly linked list
42 #define DL_LIST_INIT(pList)\
43 {(pList)->pPrev = pList; (pList)->pNext = pList;}
45 /* faster macro to init list and add a single item */
46 #define DL_LIST_INIT_AND_ADD(pList,pItem) \
47 { (pList)->pPrev = (pItem); \
48 (pList)->pNext = (pItem); \
49 (pItem)->pNext = (pList); \
50 (pItem)->pPrev = (pList); \
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
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
61 #define ITERATE_OVER_LIST(pStart, pTemp) \
62 for((pTemp) =(pStart)->pNext; pTemp != (pStart); (pTemp) = (pTemp)->pNext)
65 /* safe iterate macro that allows the item to be removed from the list
66 * the iteration continues to the next item in the list
68 #define ITERATE_OVER_LIST_ALLOW_REMOVE(pStart,pItem,st,offset) \
71 pTemp = (pStart)->pNext; \
72 while (pTemp != (pStart)) { \
73 (pItem) = A_CONTAINING_STRUCT(pTemp,st,offset); \
74 pTemp = pTemp->pNext; \
76 #define ITERATE_END }}
79 * DL_ListInsertTail - insert pAdd to the end of the list
81 static INLINE PDL_LIST DL_ListInsertTail(PDL_LIST pList, PDL_LIST pAdd) {
83 pAdd->pPrev = pList->pPrev;
85 pList->pPrev->pNext = pAdd;
91 * DL_ListInsertHead - insert pAdd into the head of the list
93 static INLINE PDL_LIST DL_ListInsertHead(PDL_LIST pList, PDL_LIST pAdd) {
96 pAdd->pNext = pList->pNext;
97 pList->pNext->pPrev = pAdd;
102 #define DL_ListAdd(pList,pItem) DL_ListInsertHead((pList),(pItem))
104 * DL_ListRemove - remove pDel from list
106 static INLINE PDL_LIST DL_ListRemove(PDL_LIST pDel) {
107 pDel->pNext->pPrev = pDel->pPrev;
108 pDel->pPrev->pNext = pDel->pNext;
109 /* point back to itself just to be safe, incase remove is called again */
116 * DL_ListRemoveItemFromHead - get a list item from the head
118 static INLINE PDL_LIST DL_ListRemoveItemFromHead(PDL_LIST pList) {
119 PDL_LIST pItem = NULL;
120 if (pList->pNext != pList) {
121 pItem = pList->pNext;
122 /* remove the first item from head */
123 DL_ListRemove(pItem);
128 static INLINE PDL_LIST DL_ListRemoveItemFromTail(PDL_LIST pList) {
129 PDL_LIST pItem = NULL;
130 if (pList->pPrev != pList) {
131 pItem = pList->pPrev;
132 /* remove the item from tail */
133 DL_ListRemove(pItem);
138 /* transfer src list items to the tail of the destination list */
139 static INLINE void DL_ListTransferItemsToTail(PDL_LIST pDest, PDL_LIST pSrc) {
140 /* only concatenate if src is not empty */
141 if (!DL_LIST_IS_EMPTY(pSrc)) {
142 /* cut out circular list in src and re-attach to end of dest */
143 pSrc->pPrev->pNext = pDest;
144 pSrc->pNext->pPrev = pDest->pPrev;
145 pDest->pPrev->pNext = pSrc->pNext;
146 pDest->pPrev = pSrc->pPrev;
147 /* terminate src list, it is now empty */
153 #endif /* __DL_LIST_H___ */