xref: /freebsd/sys/dev/pms/RefTisa/sat/src/smlist.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*******************************************************************************
2 *Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
3 *
4 *Redistribution and use in source and binary forms, with or without modification, are permitted provided
5 *that the following conditions are met:
6 *1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
7 *following disclaimer.
8 *2. Redistributions in binary form must reproduce the above copyright notice,
9 *this list of conditions and the following disclaimer in the documentation and/or other materials provided
10 *with the distribution.
11 *
12 *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 *WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 *FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16 *NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18 *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19 *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
20 *
21 * $FreeBSD$
22 *
23 ********************************************************************************/
24 
25 #ifndef __SMLIST_H__
26 #define __SMLIST_H__
27 
28 typedef struct smList_s smList_t;
29 
30 struct smList_s {
31   smList_t  *flink;
32   smList_t  *blink;
33 };
34 
35 #define SMLIST_INIT_HDR(hdr)                        \
36   do {                                              \
37     ((smList_t *)(hdr))->flink = (smList_t *)(hdr); \
38     ((smList_t *)(hdr))->blink = (smList_t *)(hdr); \
39   } while (0)
40 
41 #define SMLIST_INIT_ELEMENT(hdr)                     \
42   do {                                               \
43     ((smList_t *)(hdr))->flink = (smList_t *)agNULL; \
44     ((smList_t *)(hdr))->blink = (smList_t *)agNULL; \
45   } while (0)
46 
47 #define SMLIST_ENQUEUE_AT_HEAD(toAddHdr,listHdr)                                \
48   do {                                                                          \
49     ((smList_t *)(toAddHdr))->flink           = ((smList_t *)(listHdr))->flink; \
50     ((smList_t *)(toAddHdr))->blink           = (smList_t *)(listHdr) ;         \
51     ((smList_t *)(listHdr))->flink->blink     = (smList_t *)(toAddHdr);         \
52     ((smList_t *)(listHdr))->flink            = (smList_t *)(toAddHdr);         \
53   } while (0)
54 
55 #define SMLIST_ENQUEUE_AT_TAIL(toAddHdr,listHdr)                                \
56   do {                                                                          \
57     ((smList_t *)(toAddHdr))->flink           = (smList_t *)(listHdr);          \
58     ((smList_t *)(toAddHdr))->blink           = ((smList_t *)(listHdr))->blink; \
59     ((smList_t *)(listHdr))->blink->flink     = (smList_t *)(toAddHdr);         \
60     ((smList_t *)(listHdr))->blink            = (smList_t *)(toAddHdr);         \
61   } while (0)
62 
63 #define SMLIST_EMPTY(listHdr) \
64   (((smList_t *)(listHdr))->flink == ((smList_t *)(listHdr)))
65 
66 #define SMLIST_NOT_EMPTY(listHdr) \
67   (!SMLIST_EMPTY(listHdr))
68 
69 #define SMLIST_DEQUEUE_THIS(hdr)                                      \
70   do {                                                                \
71     ((smList_t *)(hdr))->blink->flink = ((smList_t *)(hdr))->flink;   \
72     ((smList_t *)(hdr))->flink->blink = ((smList_t *)(hdr))->blink;   \
73     ((smList_t *)(hdr))->flink = ((smList_t *)(hdr))->blink = agNULL; \
74   } while (0)
75 
76 #define SMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr)                              \
77   do {                                                                                \
78     *((smList_t **)(atHeadHdr))                 = ((smList_t *)(listHdr))->flink;     \
79     (*((smList_t **)(atHeadHdr)))->flink->blink = (smList_t *)(listHdr);              \
80     ((smList_t *)(listHdr))->flink              = (*(smList_t **)(atHeadHdr))->flink; \
81   } while (0)
82 
83 #define SMLIST_DEQUEUE_FROM_HEAD(atHeadHdr,listHdr)             \
84 do {                                                            \
85   if (SMLIST_NOT_EMPTY((listHdr)))                              \
86   {                                                             \
87     SMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr);           \
88   }                                                             \
89   else                                                          \
90   {                                                             \
91     (*((smList_t **)(atHeadHdr))) = (smList_t *)agNULL;         \
92   }                                                             \
93 } while (0)
94 
95 #define SMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr)                                \
96   do {                                                                                  \
97     (*((smList_t **)(atTailHdr)))               = ((smList_t *)(listHdr))->blink;       \
98     (*((smList_t **)(atTailHdr)))->blink->flink = (smList_t *)(listHdr);                \
99     ((smList_t *)(listHdr))->blink              = (*((smList_t **)(atTailHdr)))->blink; \
100   } while (0)
101 
102 #define SMLIST_DEQUEUE_FROM_TAIL(atTailHdr,listHdr)               \
103   do {                                                            \
104     if (SMLIST_NOT_EMPTY((listHdr)))                              \
105     {                                                             \
106       SMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr);           \
107     }                                                             \
108     else                                                          \
109     {                                                             \
110       (*((smList_t **)(atTailHdr))) = (smList_t *)agNULL;         \
111     }                                                             \
112   } while (0)
113 
114 #define SMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr)               \
115   do {                                                                        \
116     ((smList_t *)toAddListHdr)->blink->flink = ((smList_t *)listHdr);         \
117     ((smList_t *)toAddListHdr)->flink->blink = ((smList_t *)listHdr)->blink;  \
118     ((smList_t *)listHdr)->blink->flink = ((smList_t *)toAddListHdr)->flink;  \
119     ((smList_t *)listHdr)->blink = ((smList_t *)toAddListHdr)->blink;         \
120     SMLIST_INIT_HDR(toAddListHdr);                                            \
121   } while (0)
122 
123 #define SMLIST_ENQUEUE_LIST_AT_TAIL(toAddListHdr, listHdr)                    \
124   do {                                                                        \
125     if (SMLIST_NOT_EMPTY(toAddListHdr))                                       \
126     {                                                                         \
127       SMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr);                \
128     }                                                                         \
129   } while (0)
130 
131 #define SMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr)               \
132   do {                                                                        \
133     ((smList_t *)toAddListHdr)->blink->flink = ((smList_t *)listHdr)->flink;  \
134     ((smList_t *)toAddListHdr)->flink->blink = ((smList_t *)listHdr);         \
135     ((smList_t *)listHdr)->flink->blink = ((smList_t *)toAddListHdr)->blink;  \
136     ((smList_t *)listHdr)->flink = ((smList_t *)toAddListHdr)->flink;         \
137     SMLIST_INIT_HDR(toAddListHdr);                                            \
138   } while (0)
139 
140 #define SMLIST_ENQUEUE_LIST_AT_HEAD(toAddListHdr, listHdr)                    \
141   do {                                                                        \
142     if (SMLIST_NOT_EMPTY(toAddListHdr))                                       \
143     {                                                                         \
144       SMLIST_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 SMLIST_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 /* __SMLIST_H__ */
159 
160 
161 
162