Rewritten cache code and fixed a bug that was in it.
[wine] / dlls / dplayx / dplayx_queue.h
1 /* A queue definition based on sys/queue.h TAILQ definitions
2  *
3  * Copyright 2000 Peter Hunnisett
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #ifndef __WINE_DPLAYX_QUEUE_H
21 #define __WINE_DPLAYX_QUEUE_H
22
23 #include <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27
28 #define DPQ_INSERT(a,b,c) DPQ_INSERT_IN_TAIL(a,b,c)
29
30 /*
31  * Tail queue definitions.
32  */
33 #define DPQ_HEAD(type)                                           \
34 struct {                                                         \
35         struct type *lpQHFirst; /* first element */              \
36         struct type **lpQHLast; /* addr of last next element */  \
37 }
38
39 #define DPQ_ENTRY(type)                                               \
40 struct {                                                              \
41         struct type *lpQNext;  /* next element */                     \
42         struct type **lpQPrev; /* address of previous next element */ \
43 }
44
45 /*
46  * Tail queue functions.
47  */
48 #define DPQ_INIT(head)                       \
49 do{                                          \
50         (head).lpQHFirst = NULL;             \
51         (head).lpQHLast = &(head).lpQHFirst; \
52 } while(0)
53
54 /* Front of the queue */
55 #define DPQ_FIRST( head ) ( (head).lpQHFirst )
56
57 /* Check if the queue has any elements */
58 #define DPQ_IS_EMPTY( head ) ( DPQ_FIRST(head) == NULL )
59
60 /* Next entry -- FIXME: Convert everything over to this macro ... */
61 #define DPQ_NEXT( elem ) (elem).lpQNext
62
63 #define DPQ_IS_ENDOFLIST( elem ) \
64     ( DPQ_NEXT(elem) == NULL )
65
66 /* Insert element at end of queue */
67 #define DPQ_INSERT_IN_TAIL(head, elm, field)     \
68 do {                                             \
69         (elm)->field.lpQNext = NULL;             \
70         (elm)->field.lpQPrev = (head).lpQHLast;  \
71         *(head).lpQHLast = (elm);                \
72         (head).lpQHLast = &(elm)->field.lpQNext; \
73 } while(0)
74
75 /* Remove element from the queue */
76 #define DPQ_REMOVE(head, elm, field)                    \
77 do {                                                    \
78         if (((elm)->field.lpQNext) != NULL)             \
79                 (elm)->field.lpQNext->field.lpQPrev =   \
80                     (elm)->field.lpQPrev;               \
81         else                                            \
82                 (head).lpQHLast = (elm)->field.lpQPrev; \
83         *(elm)->field.lpQPrev = (elm)->field.lpQNext;   \
84 } while(0)
85
86 /* head - pointer to DPQ_HEAD struct
87  * elm  - how to find the next element
88  * field - to be concatenated to rc to compare with fieldToCompare
89  * fieldToCompare - The value that we're comparing against
90  * fieldCompareOperator - The logical operator to compare field and
91  *                        fieldToCompare.
92  * rc - Variable to put the return code. Same type as (head).lpQHFirst
93  */
94 #define DPQ_FIND_ENTRY( head, elm, field, fieldCompareOperator, fieldToCompare, rc )\
95 do {                                                           \
96   (rc) = DPQ_FIRST(head); /* NULL head? */                     \
97                                                                \
98   while( rc )                                                  \
99   {                                                            \
100       /* What we're searching for? */                          \
101       if( (rc)->field fieldCompareOperator (fieldToCompare) )  \
102       {                                                        \
103         break; /* rc == correct element */                     \
104       }                                                        \
105                                                                \
106       /* End of list check */                                  \
107       if( ( (rc) = (rc)->elm.lpQNext ) == (head).lpQHFirst )   \
108       {                                                        \
109         rc = NULL;                                             \
110         break;                                                 \
111       }                                                        \
112   }                                                            \
113 } while(0)
114
115 /* head - pointer to DPQ_HEAD struct
116  * elm  - how to find the next element
117  * field - to be concatenated to rc to compare with fieldToCompare
118  * fieldToCompare - The value that we're comparing against
119  * compare_cb - Callback to invoke to determine if comparision should continue.
120  *              Callback must be defined with DPQ_DECL_COMPARECB.
121  * rc - Variable to put the return code. Same type as (head).lpQHFirst
122  */
123 #define DPQ_FIND_ENTRY_CB( head, elm, field, compare_cb, fieldToCompare, rc )\
124 do {                                                           \
125   (rc) = DPQ_FIRST(head); /* NULL head? */                     \
126                                                                \
127   while( rc )                                                  \
128   {                                                            \
129       /* What we're searching for? */                          \
130       if( compare_cb( &((rc)->field), &(fieldToCompare) ) )    \
131       {                                                        \
132         break; /* no more */                                   \
133       }                                                        \
134                                                                \
135       /* End of list check */                                  \
136       if( ( (rc) = (rc)->elm.lpQNext ) == (head).lpQHFirst )   \
137       {                                                        \
138         rc = NULL;                                             \
139         break;                                                 \
140       }                                                        \
141   }                                                            \
142 } while(0)
143
144 /* How to define the method to be passed to DPQ_DELETEQ */
145 #define DPQ_DECL_COMPARECB( name, type ) BOOL name( const type* elem1, const type* elem2 )
146
147
148 /* head - pointer to DPQ_HEAD struct
149  * elm  - how to find the next element
150  * field - to be concatenated to rc to compare with fieldToEqual
151  * fieldToCompare - The value that we're comparing against
152  * fieldCompareOperator - The logical operator to compare field and
153  *                        fieldToCompare.
154  * rc - Variable to put the return code. Same type as (head).lpQHFirst
155  */
156 #define DPQ_REMOVE_ENTRY( head, elm, field, fieldCompareOperator, fieldToCompare, rc )\
157 do {                                                           \
158   DPQ_FIND_ENTRY( head, elm, field, fieldCompareOperator, fieldToCompare, rc );\
159                                                                \
160   /* Was the element found? */                                 \
161   if( rc )                                                     \
162   {                                                            \
163     DPQ_REMOVE( head, rc, elm );                               \
164   }                                                            \
165 } while(0)
166
167 /* head - pointer to DPQ_HEAD struct
168  * elm  - how to find the next element
169  * field - to be concatenated to rc to compare with fieldToCompare
170  * fieldToCompare - The value that we're comparing against
171  * compare_cb - Callback to invoke to determine if comparision should continue.
172  *              Callback must be defined with DPQ_DECL_COMPARECB.
173  * rc - Variable to put the return code. Same type as (head).lpQHFirst
174  */
175 #define DPQ_REMOVE_ENTRY_CB( head, elm, field, compare_cb, fieldToCompare, rc )\
176 do {                                                           \
177   DPQ_FIND_ENTRY_CB( head, elm, field, compare_cb, fieldToCompare, rc );\
178                                                                \
179   /* Was the element found? */                                 \
180   if( rc )                                                     \
181   {                                                            \
182     DPQ_REMOVE( head, rc, elm );                               \
183   }                                                            \
184 } while(0)
185
186
187 /* Delete the entire queue
188  * head - pointer to the head of the queue
189  * field - field to access the next elements of the queue
190  * type - type of the pointer to the element element
191  * df - a delete function to be called. Declared with DPQ_DECL_DELETECB.
192  */
193 #define DPQ_DELETEQ( head, field, type, df )     \
194 do                                               \
195 {                                                \
196   while( !DPQ_IS_EMPTY(head) )                   \
197   {                                              \
198     type holder = DPQ_FIRST(head);               \
199     DPQ_REMOVE( head, holder, field );           \
200     df( holder );                                \
201   }                                              \
202 } while(0)
203
204 /* How to define the method to be passed to DPQ_DELETEQ */
205 #define DPQ_DECL_DELETECB( name, type ) void name( type elem )
206
207 /* Prototype of a method which just performs a HeapFree on the elem */
208 DPQ_DECL_DELETECB( cbDeleteElemFromHeap, LPVOID );
209
210 #endif /* __WINE_DPLAYX_QUEUE_H */