1285809Sscottl/*******************************************************************************
2285809Sscottl**
3285809Sscottl*Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
4285809Sscottl*
5285809Sscottl*Redistribution and use in source and binary forms, with or without modification, are permitted provided
6285809Sscottl*that the following conditions are met:
7285809Sscottl*1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8285809Sscottl*following disclaimer.
9285809Sscottl*2. Redistributions in binary form must reproduce the above copyright notice,
10285809Sscottl*this list of conditions and the following disclaimer in the documentation and/or other materials provided
11285809Sscottl*with the distribution.
12285809Sscottl*
13285809Sscottl*THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
14285809Sscottl*WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15285809Sscottl*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16285809Sscottl*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17285809Sscottl*NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
18285809Sscottl*BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19285809Sscottl*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20285809Sscottl*SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
21285809Sscottl*
22285809Sscottl* $FreeBSD$
23285809Sscottl*
24285809Sscottl********************************************************************************/
25285809Sscottl
26285809Sscottl#ifndef __DMLIST_H__
27285809Sscottl#define __DMLIST_H__
28285809Sscottl
29285809Sscottltypedef struct dmList_s dmList_t;
30285809Sscottl
31285809Sscottlstruct dmList_s {
32285809Sscottl  dmList_t  *flink;
33285809Sscottl  dmList_t  *blink;
34285809Sscottl};
35285809Sscottl
36285809Sscottl#define DMLIST_INIT_HDR(hdr)                        \
37285809Sscottl  do {                                              \
38285809Sscottl    ((dmList_t *)(hdr))->flink = (dmList_t *)(hdr); \
39285809Sscottl    ((dmList_t *)(hdr))->blink = (dmList_t *)(hdr); \
40285809Sscottl  } while (0)
41285809Sscottl
42285809Sscottl#define DMLIST_INIT_ELEMENT(hdr)                     \
43285809Sscottl  do {                                               \
44285809Sscottl    ((dmList_t *)(hdr))->flink = (dmList_t *)agNULL; \
45285809Sscottl    ((dmList_t *)(hdr))->blink = (dmList_t *)agNULL; \
46285809Sscottl  } while (0)
47285809Sscottl
48285809Sscottl#define DMLIST_ENQUEUE_AT_HEAD(toAddHdr,listHdr)                                \
49285809Sscottl  do {                                                                          \
50285809Sscottl    ((dmList_t *)(toAddHdr))->flink           = ((dmList_t *)(listHdr))->flink; \
51285809Sscottl    ((dmList_t *)(toAddHdr))->blink           = (dmList_t *)(listHdr) ;         \
52285809Sscottl    ((dmList_t *)(listHdr))->flink->blink     = (dmList_t *)(toAddHdr);         \
53285809Sscottl    ((dmList_t *)(listHdr))->flink            = (dmList_t *)(toAddHdr);         \
54285809Sscottl  } while (0)
55285809Sscottl
56285809Sscottl#define DMLIST_ENQUEUE_AT_TAIL(toAddHdr,listHdr)                                \
57285809Sscottl  do {                                                                          \
58285809Sscottl    ((dmList_t *)(toAddHdr))->flink           = (dmList_t *)(listHdr);          \
59285809Sscottl    ((dmList_t *)(toAddHdr))->blink           = ((dmList_t *)(listHdr))->blink; \
60285809Sscottl    ((dmList_t *)(listHdr))->blink->flink     = (dmList_t *)(toAddHdr);         \
61285809Sscottl    ((dmList_t *)(listHdr))->blink            = (dmList_t *)(toAddHdr);         \
62285809Sscottl  } while (0)
63285809Sscottl
64285809Sscottl#define DMLIST_EMPTY(listHdr) \
65285809Sscottl  (((dmList_t *)(listHdr))->flink == ((dmList_t *)(listHdr)))
66285809Sscottl
67285809Sscottl#define DMLIST_NOT_EMPTY(listHdr) \
68285809Sscottl  (!DMLIST_EMPTY(listHdr))
69285809Sscottl
70285809Sscottl#define DMLIST_DEQUEUE_THIS(hdr)                                      \
71285809Sscottl  do {                                                                \
72285809Sscottl    ((dmList_t *)(hdr))->blink->flink = ((dmList_t *)(hdr))->flink;   \
73285809Sscottl    ((dmList_t *)(hdr))->flink->blink = ((dmList_t *)(hdr))->blink;   \
74285809Sscottl    ((dmList_t *)(hdr))->flink = ((dmList_t *)(hdr))->blink = agNULL; \
75285809Sscottl  } while (0)
76285809Sscottl
77285809Sscottl#define DMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr)                              \
78285809Sscottl  do {                                                                                \
79285809Sscottl    *((dmList_t **)(atHeadHdr))                 = ((dmList_t *)(listHdr))->flink;     \
80285809Sscottl    (*((dmList_t **)(atHeadHdr)))->flink->blink = (dmList_t *)(listHdr);              \
81285809Sscottl    ((dmList_t *)(listHdr))->flink              = (*(dmList_t **)(atHeadHdr))->flink; \
82285809Sscottl  } while (0)
83285809Sscottl
84285809Sscottl#define DMLIST_DEQUEUE_FROM_HEAD(atHeadHdr,listHdr)             \
85285809Sscottldo {                                                            \
86285809Sscottl  if (DMLIST_NOT_EMPTY((listHdr)))                              \
87285809Sscottl  {                                                             \
88285809Sscottl    DMLIST_DEQUEUE_FROM_HEAD_FAST(atHeadHdr,listHdr);           \
89285809Sscottl  }                                                             \
90285809Sscottl  else                                                          \
91285809Sscottl  {                                                             \
92285809Sscottl    (*((dmList_t **)(atHeadHdr))) = (dmList_t *)agNULL;         \
93285809Sscottl  }                                                             \
94285809Sscottl} while (0)
95285809Sscottl
96285809Sscottl#define DMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr)                                \
97285809Sscottl  do {                                                                                  \
98285809Sscottl    (*((dmList_t **)(atTailHdr)))               = ((dmList_t *)(listHdr))->blink;       \
99285809Sscottl    (*((dmList_t **)(atTailHdr)))->blink->flink = (dmList_t *)(listHdr);                \
100285809Sscottl    ((dmList_t *)(listHdr))->blink              = (*((dmList_t **)(atTailHdr)))->blink; \
101285809Sscottl  } while (0)
102285809Sscottl
103285809Sscottl#define DMLIST_DEQUEUE_FROM_TAIL(atTailHdr,listHdr)               \
104285809Sscottl  do {                                                            \
105285809Sscottl    if (DMLIST_NOT_EMPTY((listHdr)))                              \
106285809Sscottl    {                                                             \
107285809Sscottl      DMLIST_DEQUEUE_FROM_TAIL_FAST(atTailHdr,listHdr);           \
108285809Sscottl    }                                                             \
109285809Sscottl    else                                                          \
110285809Sscottl    {                                                             \
111285809Sscottl      (*((dmList_t **)(atTailHdr))) = (dmList_t *)agNULL;         \
112285809Sscottl    }                                                             \
113285809Sscottl  } while (0)
114285809Sscottl
115285809Sscottl#define DMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr)               \
116285809Sscottl  do {                                                                        \
117285809Sscottl    ((dmList_t *)toAddListHdr)->blink->flink = ((dmList_t *)listHdr);         \
118285809Sscottl    ((dmList_t *)toAddListHdr)->flink->blink = ((dmList_t *)listHdr)->blink;  \
119285809Sscottl    ((dmList_t *)listHdr)->blink->flink = ((dmList_t *)toAddListHdr)->flink;  \
120285809Sscottl    ((dmList_t *)listHdr)->blink = ((dmList_t *)toAddListHdr)->blink;         \
121285809Sscottl    DMLIST_INIT_HDR(toAddListHdr);                                            \
122285809Sscottl  } while (0)
123285809Sscottl
124285809Sscottl#define DMLIST_ENQUEUE_LIST_AT_TAIL(toAddListHdr, listHdr)                    \
125285809Sscottl  do {                                                                        \
126285809Sscottl    if (DMLIST_NOT_EMPTY(toAddListHdr))                                       \
127285809Sscottl    {                                                                         \
128285809Sscottl      DMLIST_ENQUEUE_LIST_AT_TAIL_FAST(toAddListHdr, listHdr);                \
129285809Sscottl    }                                                                         \
130285809Sscottl  } while (0)
131285809Sscottl
132285809Sscottl#define DMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr)               \
133285809Sscottl  do {                                                                        \
134285809Sscottl    ((dmList_t *)toAddListHdr)->blink->flink = ((dmList_t *)listHdr)->flink;  \
135285809Sscottl    ((dmList_t *)toAddListHdr)->flink->blink = ((dmList_t *)listHdr);         \
136285809Sscottl    ((dmList_t *)listHdr)->flink->blink = ((dmList_t *)toAddListHdr)->blink;  \
137285809Sscottl    ((dmList_t *)listHdr)->flink = ((dmList_t *)toAddListHdr)->flink;         \
138285809Sscottl    DMLIST_INIT_HDR(toAddListHdr);                                            \
139285809Sscottl  } while (0)
140285809Sscottl
141285809Sscottl#define DMLIST_ENQUEUE_LIST_AT_HEAD(toAddListHdr, listHdr)                    \
142285809Sscottl  do {                                                                        \
143285809Sscottl    if (DMLIST_NOT_EMPTY(toAddListHdr))                                       \
144285809Sscottl    {                                                                         \
145285809Sscottl      DMLIST_ENQUEUE_LIST_AT_HEAD_FAST(toAddListHdr, listHdr);                \
146285809Sscottl    }                                                                         \
147285809Sscottl  } while (0)
148285809Sscottl
149285809Sscottl#define TD_FIELD_OFFSET(baseType,fieldName) \
150285809Sscottl                    ((bit32)((bitptr)(&(((baseType *)0)->fieldName))))
151285809Sscottl
152285809Sscottl#define DMLIST_OBJECT_BASE(baseType,fieldName,fieldPtr)         \
153285809Sscottl                    (void *)fieldPtr == (void *)0 ? (baseType *)0 :             \
154285809Sscottl                    ((baseType *)((bit8 *)(fieldPtr) - ((bitptr)(&(((baseType *)0)->fieldName)))))
155285809Sscottl
156285809Sscottl
157285809Sscottl
158285809Sscottl
159285809Sscottl#endif /* __DMLIST_H__ */
160285809Sscottl
161285809Sscottl
162