xref: /freebsd/sys/dev/pms/RefTisa/sat/src/smlist.h (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
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 *
22 ********************************************************************************/
23 
24 #ifndef __SMLIST_H__
25 #define __SMLIST_H__
26 
27 typedef struct smList_s smList_t;
28 
29 struct smList_s {
30   smList_t  *flink;
31   smList_t  *blink;
32 };
33 
34 #define SMLIST_INIT_HDR(hdr)                        \
35   do {                                              \
36     ((smList_t *)(hdr))->flink = (smList_t *)(hdr); \
37     ((smList_t *)(hdr))->blink = (smList_t *)(hdr); \
38   } while (0)
39 
40 #define SMLIST_INIT_ELEMENT(hdr)                     \
41   do {                                               \
42     ((smList_t *)(hdr))->flink = (smList_t *)agNULL; \
43     ((smList_t *)(hdr))->blink = (smList_t *)agNULL; \
44   } while (0)
45 
46 #define SMLIST_ENQUEUE_AT_HEAD(toAddHdr,listHdr)                                \
47   do {                                                                          \
48     ((smList_t *)(toAddHdr))->flink           = ((smList_t *)(listHdr))->flink; \
49     ((smList_t *)(toAddHdr))->blink           = (smList_t *)(listHdr) ;         \
50     ((smList_t *)(listHdr))->flink->blink     = (smList_t *)(toAddHdr);         \
51     ((smList_t *)(listHdr))->flink            = (smList_t *)(toAddHdr);         \
52   } while (0)
53 
54 #define SMLIST_ENQUEUE_AT_TAIL(toAddHdr,listHdr)                                \
55   do {                                                                          \
56     ((smList_t *)(toAddHdr))->flink           = (smList_t *)(listHdr);          \
57     ((smList_t *)(toAddHdr))->blink           = ((smList_t *)(listHdr))->blink; \
58     ((smList_t *)(listHdr))->blink->flink     = (smList_t *)(toAddHdr);         \
59     ((smList_t *)(listHdr))->blink            = (smList_t *)(toAddHdr);         \
60   } while (0)
61 
62 #define SMLIST_EMPTY(listHdr) \
63   (((smList_t *)(listHdr))->flink == ((smList_t *)(listHdr)))
64 
65 #define SMLIST_NOT_EMPTY(listHdr) \
66   (!SMLIST_EMPTY(listHdr))
67 
68 #define SMLIST_DEQUEUE_THIS(hdr)                                      \
69   do {                                                                \
70     ((smList_t *)(hdr))->blink->flink = ((smList_t *)(hdr))->flink;   \
71     ((smList_t *)(hdr))->flink->blink = ((smList_t *)(hdr))->blink;   \
72     ((smList_t *)(hdr))->flink = ((smList_t *)(hdr))->blink = agNULL; \
73   } while (0)
74 
75 #define SMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr)                              \
76   do {                                                                                \
77     *((smList_t **)(atHeadHdr))                 = ((smList_t *)(listHdr))->flink;     \
78     (*((smList_t **)(atHeadHdr)))->flink->blink = (smList_t *)(listHdr);              \
79     ((smList_t *)(listHdr))->flink              = (*(smList_t **)(atHeadHdr))->flink; \
80   } while (0)
81 
82 #define SMLIST_DEQUEUE_FROM_HEAD(atHeadHdr,listHdr)             \
83 do {                                                            \
84   if (SMLIST_NOT_EMPTY((listHdr)))                              \
85   {                                                             \
86     SMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr);           \
87   }                                                             \
88   else                                                          \
89   {                                                             \
90     (*((smList_t **)(atHeadHdr))) = (smList_t *)agNULL;         \
91   }                                                             \
92 } while (0)
93 
94 #define SMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr)                                \
95   do {                                                                                  \
96     (*((smList_t **)(atTailHdr)))               = ((smList_t *)(listHdr))->blink;       \
97     (*((smList_t **)(atTailHdr)))->blink->flink = (smList_t *)(listHdr);                \
98     ((smList_t *)(listHdr))->blink              = (*((smList_t **)(atTailHdr)))->blink; \
99   } while (0)
100 
101 #define SMLIST_DEQUEUE_FROM_TAIL(atTailHdr,listHdr)               \
102   do {                                                            \
103     if (SMLIST_NOT_EMPTY((listHdr)))                              \
104     {                                                             \
105       SMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr);           \
106     }                                                             \
107     else                                                          \
108     {                                                             \
109       (*((smList_t **)(atTailHdr))) = (smList_t *)agNULL;         \
110     }                                                             \
111   } while (0)
112 
113 #define SMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr)               \
114   do {                                                                        \
115     ((smList_t *)toAddListHdr)->blink->flink = ((smList_t *)listHdr);         \
116     ((smList_t *)toAddListHdr)->flink->blink = ((smList_t *)listHdr)->blink;  \
117     ((smList_t *)listHdr)->blink->flink = ((smList_t *)toAddListHdr)->flink;  \
118     ((smList_t *)listHdr)->blink = ((smList_t *)toAddListHdr)->blink;         \
119     SMLIST_INIT_HDR(toAddListHdr);                                            \
120   } while (0)
121 
122 #define SMLIST_ENQUEUE_LIST_AT_TAIL(toAddListHdr, listHdr)                    \
123   do {                                                                        \
124     if (SMLIST_NOT_EMPTY(toAddListHdr))                                       \
125     {                                                                         \
126       SMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr);                \
127     }                                                                         \
128   } while (0)
129 
130 #define SMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr)               \
131   do {                                                                        \
132     ((smList_t *)toAddListHdr)->blink->flink = ((smList_t *)listHdr)->flink;  \
133     ((smList_t *)toAddListHdr)->flink->blink = ((smList_t *)listHdr);         \
134     ((smList_t *)listHdr)->flink->blink = ((smList_t *)toAddListHdr)->blink;  \
135     ((smList_t *)listHdr)->flink = ((smList_t *)toAddListHdr)->flink;         \
136     SMLIST_INIT_HDR(toAddListHdr);                                            \
137   } while (0)
138 
139 #define SMLIST_ENQUEUE_LIST_AT_HEAD(toAddListHdr, listHdr)                    \
140   do {                                                                        \
141     if (SMLIST_NOT_EMPTY(toAddListHdr))                                       \
142     {                                                                         \
143       SMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr);                \
144     }                                                                         \
145   } while (0)
146 
147 #define TD_FIELD_OFFSET(baseType,fieldName) \
148                     ((bit32)((bitptr)(&(((baseType *)0)->fieldName))))
149 
150 #define SMLIST_OBJECT_BASE(baseType,fieldName,fieldPtr)         \
151                     (void *)fieldPtr == (void *)0 ? (baseType *)0 :             \
152                     ((baseType *)((bit8 *)(fieldPtr) - ((bitptr)(&(((baseType *)0)->fieldName)))))
153 
154 
155 
156 
157 #endif /* __SMLIST_H__ */
158 
159 
160 
161