123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #ifndef __DL_LIST_H___
- #define __DL_LIST_H___
- #define A_CONTAINING_STRUCT(address, struct_type, field_name) \
- ((struct_type *)((char *)(address) - (char *)(&((struct_type *)0)->field_name)))
- typedef struct _DL_LIST {
- struct _DL_LIST *pPrev;
- struct _DL_LIST *pNext;
- } DL_LIST, *PDL_LIST;
- #define DL_LIST_INIT(pList) \
- {(pList)->pPrev = pList; (pList)->pNext = pList; }
- #define DL_LIST_INIT_AND_ADD(pList,pItem) \
- { (pList)->pPrev = (pItem); \
- (pList)->pNext = (pItem); \
- (pItem)->pNext = (pList); \
- (pItem)->pPrev = (pList); \
- }
- #define DL_LIST_IS_EMPTY(pList) (((pList)->pPrev == (pList)) && ((pList)->pNext == (pList)))
- #define DL_LIST_GET_ITEM_AT_HEAD(pList) (pList)->pNext
- #define DL_LIST_GET_ITEM_AT_TAIL(pList) (pList)->pPrev
- #define ITERATE_OVER_LIST(pStart, pTemp) \
- for((pTemp) =(pStart)->pNext; pTemp != (pStart); (pTemp) = (pTemp)->pNext)
- static __inline bool dl_list_is_entry_in_list(const DL_LIST *pList,
- const DL_LIST *pEntry)
- {
- const DL_LIST *pTmp;
- if (pList == pEntry)
- return true;
- ITERATE_OVER_LIST(pList, pTmp) {
- if (pTmp == pEntry) {
- return true;
- }
- }
- return false;
- }
- #define ITERATE_OVER_LIST_ALLOW_REMOVE(pStart,pItem,st,offset) \
- { \
- PDL_LIST pTemp; \
- pTemp = (pStart)->pNext; \
- while (pTemp != (pStart)) { \
- (pItem) = A_CONTAINING_STRUCT(pTemp,st,offset); \
- pTemp = pTemp->pNext; \
- #define ITERATE_IS_VALID(pStart) dl_list_is_entry_in_list(pStart, pTemp)
- #define ITERATE_RESET(pStart) pTemp=(pStart)->pNext
- #define ITERATE_END }}
- static __inline PDL_LIST dl_list_insert_tail(PDL_LIST pList, PDL_LIST pAdd)
- {
-
- pAdd->pPrev = pList->pPrev;
- pAdd->pNext = pList;
- if (pList->pPrev) {
- pList->pPrev->pNext = pAdd;
- }
- pList->pPrev = pAdd;
- return pAdd;
- }
- static __inline PDL_LIST dl_list_insert_head(PDL_LIST pList, PDL_LIST pAdd)
- {
-
- pAdd->pPrev = pList;
- pAdd->pNext = pList->pNext;
- pList->pNext->pPrev = pAdd;
- pList->pNext = pAdd;
- return pAdd;
- }
- #define DL_ListAdd(pList,pItem) dl_list_insert_head((pList),(pItem))
- static __inline PDL_LIST dl_list_remove(PDL_LIST pDel)
- {
- if (pDel->pNext != NULL) {
- pDel->pNext->pPrev = pDel->pPrev;
- }
- if (pDel->pPrev != NULL) {
- pDel->pPrev->pNext = pDel->pNext;
- }
-
- pDel->pNext = pDel;
- pDel->pPrev = pDel;
- return pDel;
- }
- static __inline PDL_LIST dl_list_remove_item_from_head(PDL_LIST pList)
- {
- PDL_LIST pItem = NULL;
- if (pList->pNext != pList) {
- pItem = pList->pNext;
-
- dl_list_remove(pItem);
- }
- return pItem;
- }
- static __inline PDL_LIST dl_list_remove_item_from_tail(PDL_LIST pList)
- {
- PDL_LIST pItem = NULL;
- if (pList->pPrev != pList) {
- pItem = pList->pPrev;
-
- dl_list_remove(pItem);
- }
- return pItem;
- }
- static __inline void dl_list_transfer_items_to_tail(PDL_LIST pDest, PDL_LIST pSrc)
- {
-
- if (!DL_LIST_IS_EMPTY(pSrc)) {
-
- pSrc->pPrev->pNext = pDest;
- pSrc->pNext->pPrev = pDest->pPrev;
- pDest->pPrev->pNext = pSrc->pNext;
- pDest->pPrev = pSrc->pPrev;
-
- pSrc->pPrev = pSrc;
- pSrc->pNext = pSrc;
- }
- }
- static __inline void dl_list_transfer_items_to_head(PDL_LIST pDest, PDL_LIST pSrc)
- {
-
- if (!DL_LIST_IS_EMPTY(pSrc)) {
-
- pSrc->pNext->pPrev = pDest;
- pDest->pNext->pPrev = pSrc->pPrev;
- pSrc->pPrev->pNext = pDest->pNext;
- pDest->pNext = pSrc->pNext;
-
- pSrc->pPrev = pSrc;
- pSrc->pNext = pSrc;
- }
- }
- #endif
|