xref: /freebsd/sys/dev/pms/RefTisa/discovery/dm/dmlist.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*******************************************************************************
2 **
3 *Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
4 *
5 *Redistribution and use in source and binary forms, with or without modification, are permitted provided
6 *that the following conditions are met:
7 *1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8 *following disclaimer.
9 *2. Redistributions in binary form must reproduce the above copyright notice,
10 *this list of conditions and the following disclaimer in the documentation and/or other materials provided
11 *with the distribution.
12 *
13 *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 *WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 *FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17 *NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
18 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19 *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20 *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
21 *
22 *
23 ********************************************************************************/
24 
25 #ifndef __DMLIST_H__
26 #define __DMLIST_H__
27 
28 typedef struct dmList_s dmList_t;
29 
30 struct dmList_s {
31   dmList_t  *flink;
32   dmList_t  *blink;
33 };
34 
35 #define DMLIST_INIT_HDR(hdr)                        \
36   do {                                              \
37     ((dmList_t *)(hdr))->flink = (dmList_t *)(hdr); \
38     ((dmList_t *)(hdr))->blink = (dmList_t *)(hdr); \
39   } while (0)
40 
41 #define DMLIST_INIT_ELEMENT(hdr)                     \
42   do {                                               \
43     ((dmList_t *)(hdr))->flink = (dmList_t *)agNULL; \
44     ((dmList_t *)(hdr))->blink = (dmList_t *)agNULL; \
45   } while (0)
46 
47 #define DMLIST_ENQUEUE_AT_HEAD(toAddHdr,listHdr)                                \
48   do {                                                                          \
49     ((dmList_t *)(toAddHdr))->flink           = ((dmList_t *)(listHdr))->flink; \
50     ((dmList_t *)(toAddHdr))->blink           = (dmList_t *)(listHdr) ;         \
51     ((dmList_t *)(listHdr))->flink->blink     = (dmList_t *)(toAddHdr);         \
52     ((dmList_t *)(listHdr))->flink            = (dmList_t *)(toAddHdr);         \
53   } while (0)
54 
55 #define DMLIST_ENQUEUE_AT_TAIL(toAddHdr,listHdr)                                \
56   do {                                                                          \
57     ((dmList_t *)(toAddHdr))->flink           = (dmList_t *)(listHdr);          \
58     ((dmList_t *)(toAddHdr))->blink           = ((dmList_t *)(listHdr))->blink; \
59     ((dmList_t *)(listHdr))->blink->flink     = (dmList_t *)(toAddHdr);         \
60     ((dmList_t *)(listHdr))->blink            = (dmList_t *)(toAddHdr);         \
61   } while (0)
62 
63 #define DMLIST_EMPTY(listHdr) \
64   (((dmList_t *)(listHdr))->flink == ((dmList_t *)(listHdr)))
65 
66 #define DMLIST_NOT_EMPTY(listHdr) \
67   (!DMLIST_EMPTY(listHdr))
68 
69 #define DMLIST_DEQUEUE_THIS(hdr)                                      \
70   do {                                                                \
71     ((dmList_t *)(hdr))->blink->flink = ((dmList_t *)(hdr))->flink;   \
72     ((dmList_t *)(hdr))->flink->blink = ((dmList_t *)(hdr))->blink;   \
73     ((dmList_t *)(hdr))->flink = ((dmList_t *)(hdr))->blink = agNULL; \
74   } while (0)
75 
76 #define DMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr)                              \
77   do {                                                                                \
78     *((dmList_t **)(atHeadHdr))                 = ((dmList_t *)(listHdr))->flink;     \
79     (*((dmList_t **)(atHeadHdr)))->flink->blink = (dmList_t *)(listHdr);              \
80     ((dmList_t *)(listHdr))->flink              = (*(dmList_t **)(atHeadHdr))->flink; \
81   } while (0)
82 
83 #define DMLIST_DEQUEUE_FROM_HEAD(atHeadHdr,listHdr)             \
84 do {                                                            \
85   if (DMLIST_NOT_EMPTY((listHdr)))                              \
86   {                                                             \
87     DMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr);           \
88   }                                                             \
89   else                                                          \
90   {                                                             \
91     (*((dmList_t **)(atHeadHdr))) = (dmList_t *)agNULL;         \
92   }                                                             \
93 } while (0)
94 
95 #define DMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr)                                \
96   do {                                                                                  \
97     (*((dmList_t **)(atTailHdr)))               = ((dmList_t *)(listHdr))->blink;       \
98     (*((dmList_t **)(atTailHdr)))->blink->flink = (dmList_t *)(listHdr);                \
99     ((dmList_t *)(listHdr))->blink              = (*((dmList_t **)(atTailHdr)))->blink; \
100   } while (0)
101 
102 #define DMLIST_DEQUEUE_FROM_TAIL(atTailHdr,listHdr)               \
103   do {                                                            \
104     if (DMLIST_NOT_EMPTY((listHdr)))                              \
105     {                                                             \
106       DMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr);           \
107     }                                                             \
108     else                                                          \
109     {                                                             \
110       (*((dmList_t **)(atTailHdr))) = (dmList_t *)agNULL;         \
111     }                                                             \
112   } while (0)
113 
114 #define DMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr)               \
115   do {                                                                        \
116     ((dmList_t *)toAddListHdr)->blink->flink = ((dmList_t *)listHdr);         \
117     ((dmList_t *)toAddListHdr)->flink->blink = ((dmList_t *)listHdr)->blink;  \
118     ((dmList_t *)listHdr)->blink->flink = ((dmList_t *)toAddListHdr)->flink;  \
119     ((dmList_t *)listHdr)->blink = ((dmList_t *)toAddListHdr)->blink;         \
120     DMLIST_INIT_HDR(toAddListHdr);                                            \
121   } while (0)
122 
123 #define DMLIST_ENQUEUE_LIST_AT_TAIL(toAddListHdr, listHdr)                    \
124   do {                                                                        \
125     if (DMLIST_NOT_EMPTY(toAddListHdr))                                       \
126     {                                                                         \
127       DMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr);                \
128     }                                                                         \
129   } while (0)
130 
131 #define DMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr)               \
132   do {                                                                        \
133     ((dmList_t *)toAddListHdr)->blink->flink = ((dmList_t *)listHdr)->flink;  \
134     ((dmList_t *)toAddListHdr)->flink->blink = ((dmList_t *)listHdr);         \
135     ((dmList_t *)listHdr)->flink->blink = ((dmList_t *)toAddListHdr)->blink;  \
136     ((dmList_t *)listHdr)->flink = ((dmList_t *)toAddListHdr)->flink;         \
137     DMLIST_INIT_HDR(toAddListHdr);                                            \
138   } while (0)
139 
140 #define DMLIST_ENQUEUE_LIST_AT_HEAD(toAddListHdr, listHdr)                    \
141   do {                                                                        \
142     if (DMLIST_NOT_EMPTY(toAddListHdr))                                       \
143     {                                                                         \
144       DMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr);                \
145     }                                                                         \
146   } while (0)
147 
148 #define TD_FIELD_OFFSET(baseType,fieldName) \
149                     ((bit32)((bitptr)(&(((baseType *)0)->fieldName))))
150 
151 #define DMLIST_OBJECT_BASE(baseType,fieldName,fieldPtr)         \
152                     (void *)fieldPtr == (void *)0 ? (baseType *)0 :             \
153                     ((baseType *)((bit8 *)(fieldPtr) - ((bitptr)(&(((baseType *)0)->fieldName)))))
154 
155 
156 
157 
158 #endif /* __DMLIST_H__ */
159 
160 
161