xref: /freebsd/sys/dev/pms/RefTisa/discovery/dm/dmlist.h (revision 63a938566d524836885917d95bd491aa4400b181)
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 * $FreeBSD$
23 *
24 ********************************************************************************/
25 
26 #ifndef __DMLIST_H__
27 #define __DMLIST_H__
28 
29 typedef struct dmList_s dmList_t;
30 
31 struct dmList_s {
32   dmList_t  *flink;
33   dmList_t  *blink;
34 };
35 
36 #define DMLIST_INIT_HDR(hdr)                        \
37   do {                                              \
38     ((dmList_t *)(hdr))->flink = (dmList_t *)(hdr); \
39     ((dmList_t *)(hdr))->blink = (dmList_t *)(hdr); \
40   } while (0)
41 
42 #define DMLIST_INIT_ELEMENT(hdr)                     \
43   do {                                               \
44     ((dmList_t *)(hdr))->flink = (dmList_t *)agNULL; \
45     ((dmList_t *)(hdr))->blink = (dmList_t *)agNULL; \
46   } while (0)
47 
48 #define DMLIST_ENQUEUE_AT_HEAD(toAddHdr,listHdr)                                \
49   do {                                                                          \
50     ((dmList_t *)(toAddHdr))->flink           = ((dmList_t *)(listHdr))->flink; \
51     ((dmList_t *)(toAddHdr))->blink           = (dmList_t *)(listHdr) ;         \
52     ((dmList_t *)(listHdr))->flink->blink     = (dmList_t *)(toAddHdr);         \
53     ((dmList_t *)(listHdr))->flink            = (dmList_t *)(toAddHdr);         \
54   } while (0)
55 
56 #define DMLIST_ENQUEUE_AT_TAIL(toAddHdr,listHdr)                                \
57   do {                                                                          \
58     ((dmList_t *)(toAddHdr))->flink           = (dmList_t *)(listHdr);          \
59     ((dmList_t *)(toAddHdr))->blink           = ((dmList_t *)(listHdr))->blink; \
60     ((dmList_t *)(listHdr))->blink->flink     = (dmList_t *)(toAddHdr);         \
61     ((dmList_t *)(listHdr))->blink            = (dmList_t *)(toAddHdr);         \
62   } while (0)
63 
64 #define DMLIST_EMPTY(listHdr) \
65   (((dmList_t *)(listHdr))->flink == ((dmList_t *)(listHdr)))
66 
67 #define DMLIST_NOT_EMPTY(listHdr) \
68   (!DMLIST_EMPTY(listHdr))
69 
70 #define DMLIST_DEQUEUE_THIS(hdr)                                      \
71   do {                                                                \
72     ((dmList_t *)(hdr))->blink->flink = ((dmList_t *)(hdr))->flink;   \
73     ((dmList_t *)(hdr))->flink->blink = ((dmList_t *)(hdr))->blink;   \
74     ((dmList_t *)(hdr))->flink = ((dmList_t *)(hdr))->blink = agNULL; \
75   } while (0)
76 
77 #define DMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr)                              \
78   do {                                                                                \
79     *((dmList_t **)(atHeadHdr))                 = ((dmList_t *)(listHdr))->flink;     \
80     (*((dmList_t **)(atHeadHdr)))->flink->blink = (dmList_t *)(listHdr);              \
81     ((dmList_t *)(listHdr))->flink              = (*(dmList_t **)(atHeadHdr))->flink; \
82   } while (0)
83 
84 #define DMLIST_DEQUEUE_FROM_HEAD(atHeadHdr,listHdr)             \
85 do {                                                            \
86   if (DMLIST_NOT_EMPTY((listHdr)))                              \
87   {                                                             \
88     DMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr);           \
89   }                                                             \
90   else                                                          \
91   {                                                             \
92     (*((dmList_t **)(atHeadHdr))) = (dmList_t *)agNULL;         \
93   }                                                             \
94 } while (0)
95 
96 #define DMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr)                                \
97   do {                                                                                  \
98     (*((dmList_t **)(atTailHdr)))               = ((dmList_t *)(listHdr))->blink;       \
99     (*((dmList_t **)(atTailHdr)))->blink->flink = (dmList_t *)(listHdr);                \
100     ((dmList_t *)(listHdr))->blink              = (*((dmList_t **)(atTailHdr)))->blink; \
101   } while (0)
102 
103 #define DMLIST_DEQUEUE_FROM_TAIL(atTailHdr,listHdr)               \
104   do {                                                            \
105     if (DMLIST_NOT_EMPTY((listHdr)))                              \
106     {                                                             \
107       DMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr);           \
108     }                                                             \
109     else                                                          \
110     {                                                             \
111       (*((dmList_t **)(atTailHdr))) = (dmList_t *)agNULL;         \
112     }                                                             \
113   } while (0)
114 
115 #define DMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr)               \
116   do {                                                                        \
117     ((dmList_t *)toAddListHdr)->blink->flink = ((dmList_t *)listHdr);         \
118     ((dmList_t *)toAddListHdr)->flink->blink = ((dmList_t *)listHdr)->blink;  \
119     ((dmList_t *)listHdr)->blink->flink = ((dmList_t *)toAddListHdr)->flink;  \
120     ((dmList_t *)listHdr)->blink = ((dmList_t *)toAddListHdr)->blink;         \
121     DMLIST_INIT_HDR(toAddListHdr);                                            \
122   } while (0)
123 
124 #define DMLIST_ENQUEUE_LIST_AT_TAIL(toAddListHdr, listHdr)                    \
125   do {                                                                        \
126     if (DMLIST_NOT_EMPTY(toAddListHdr))                                       \
127     {                                                                         \
128       DMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr);                \
129     }                                                                         \
130   } while (0)
131 
132 #define DMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr)               \
133   do {                                                                        \
134     ((dmList_t *)toAddListHdr)->blink->flink = ((dmList_t *)listHdr)->flink;  \
135     ((dmList_t *)toAddListHdr)->flink->blink = ((dmList_t *)listHdr);         \
136     ((dmList_t *)listHdr)->flink->blink = ((dmList_t *)toAddListHdr)->blink;  \
137     ((dmList_t *)listHdr)->flink = ((dmList_t *)toAddListHdr)->flink;         \
138     DMLIST_INIT_HDR(toAddListHdr);                                            \
139   } while (0)
140 
141 #define DMLIST_ENQUEUE_LIST_AT_HEAD(toAddListHdr, listHdr)                    \
142   do {                                                                        \
143     if (DMLIST_NOT_EMPTY(toAddListHdr))                                       \
144     {                                                                         \
145       DMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr);                \
146     }                                                                         \
147   } while (0)
148 
149 #define TD_FIELD_OFFSET(baseType,fieldName) \
150                     ((bit32)((bitptr)(&(((baseType *)0)->fieldName))))
151 
152 #define DMLIST_OBJECT_BASE(baseType,fieldName,fieldPtr)         \
153                     (void *)fieldPtr == (void *)0 ? (baseType *)0 :             \
154                     ((baseType *)((bit8 *)(fieldPtr) - ((bitptr)(&(((baseType *)0)->fieldName)))))
155 
156 
157 
158 
159 #endif /* __DMLIST_H__ */
160 
161 
162