1 /*************************************************************************/ /*!
3 @Title Linked list shared functions templates.
4 @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description Definition of the linked list function templates.
6 @License Dual MIT/GPLv2
8 The contents of this file are subject to the MIT license as set out below.
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
20 Alternatively, the contents of this file may be used under the terms of
21 the GNU General Public License Version 2 ("GPL") in which case the provisions
22 of GPL are applicable instead of those above.
24 If you wish to allow use of your version of this file only under the terms of
25 GPL, and not to allow others to use your version of this file under the terms
26 of the MIT license, indicate your decision by deleting the provisions above
27 and replace them with the notice and other provisions required by GPL as set
28 out in the file called "GPL-COPYING" included in this distribution. If you do
29 not delete the provisions above, a recipient may use your version of this file
30 under the terms of either the MIT license or GPL.
32 This License is also included in this distribution in the file called
35 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
36 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
37 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
38 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
39 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
40 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
41 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 */ /**************************************************************************/
44 #ifndef __LISTS_UTILS__
45 #define __LISTS_UTILS__
47 /* instruct QAC to ignore warnings about the following custom formatted macros */
48 /* PRQA S 0881,3410 ++ */
50 #include "img_types.h"
57 The list functions work with any structure that provides the fields psNext and
58 ppsThis. In order to make a function available for a given type, it is required
59 to use the funcion template macro that creates the actual code.
61 There are 4 main types of functions:
62 - INSERT : given a pointer to the head pointer of the list and a pointer to
63 the node, inserts it as the new head.
64 - REMOVE : given a pointer to a node, removes it from its list.
65 - FOR EACH : apply a function over all the elements of a list.
66 - ANY : apply a function over the elements of a list, until one of them
67 return a non null value, and then returns it.
69 The two last functions can have a variable argument form, with allows to pass
70 additional parameters to the callback function. In order to do this, the
71 callback function must take two arguments, the first is the current node and
72 the second is a list of variable arguments (va_list).
74 The ANY functions have also another for wich specifies the return type of the
75 callback function and the default value returned by the callback function.
79 /*************************************************************************/ /*!
80 @Function List_##TYPE##_ForEach
81 @Description Apply a callback function to all the elements of a list.
82 @Input psHead The head of the list to be processed.
83 @Input pfnCallBack The function to be applied to each element of the list.
84 */ /**************************************************************************/
85 #define DECLARE_LIST_FOR_EACH(TYPE) \
86 IMG_VOID List_##TYPE##_ForEach(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))
88 #define IMPLEMENT_LIST_FOR_EACH(TYPE) \
89 IMG_VOID List_##TYPE##_ForEach(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))\
94 psHead = psHead->psNext;\
98 /*************************************************************************/ /*!
99 @Function List_##TYPE##_ForEachSafe
100 @Description Apply a callback function to all the elements of a list. Do it
101 in a safe way that handles the fact that a node might remove itself
102 from the list during the iteration.
103 @Input psHead The head of the list to be processed.
104 @Input pfnCallBack The function to be applied to each element of the list.
105 */ /**************************************************************************/
106 #define DECLARE_LIST_FOR_EACH_SAFE(TYPE) \
107 IMG_VOID List_##TYPE##_ForEachSafe(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))
109 #define IMPLEMENT_LIST_FOR_EACH_SAFE(TYPE) \
110 IMG_VOID List_##TYPE##_ForEachSafe(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))\
116 psNext = psHead->psNext; \
117 pfnCallBack(psHead);\
123 #define DECLARE_LIST_FOR_EACH_VA(TYPE) \
124 IMG_VOID List_##TYPE##_ForEach_va(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode, va_list va), ...)
126 #define IMPLEMENT_LIST_FOR_EACH_VA(TYPE) \
127 IMG_VOID List_##TYPE##_ForEach_va(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode, va_list va), ...) \
132 va_start(ap, pfnCallBack);\
133 pfnCallBack(psHead, ap);\
134 psHead = psHead->psNext;\
140 /*************************************************************************/ /*!
141 @Function List_##TYPE##_Any
142 @Description Applies a callback function to the elements of a list until the function
143 returns a non null value, then returns it.
144 @Input psHead The head of the list to be processed.
145 @Input pfnCallBack The function to be applied to each element of the list.
146 @Return The first non null value returned by the callback function.
147 */ /**************************************************************************/
148 #define DECLARE_LIST_ANY(TYPE) \
149 IMG_VOID* List_##TYPE##_Any(TYPE *psHead, IMG_VOID* (*pfnCallBack)(TYPE* psNode))
151 #define IMPLEMENT_LIST_ANY(TYPE) \
152 IMG_VOID* List_##TYPE##_Any(TYPE *psHead, IMG_VOID* (*pfnCallBack)(TYPE* psNode))\
157 psNextNode = psHead;\
158 while(psHead && !pResult)\
160 psNextNode = psNextNode->psNext;\
161 pResult = pfnCallBack(psHead);\
162 psHead = psNextNode;\
168 /*with variable arguments, that will be passed as a va_list to the callback function*/
170 #define DECLARE_LIST_ANY_VA(TYPE) \
171 IMG_VOID* List_##TYPE##_Any_va(TYPE *psHead, IMG_VOID*(*pfnCallBack)(TYPE* psNode, va_list va), ...)
173 #define IMPLEMENT_LIST_ANY_VA(TYPE) \
174 IMG_VOID* List_##TYPE##_Any_va(TYPE *psHead, IMG_VOID*(*pfnCallBack)(TYPE* psNode, va_list va), ...)\
178 IMG_VOID* pResult = IMG_NULL;\
179 while(psHead && !pResult)\
181 psNextNode = psHead->psNext;\
182 va_start(ap, pfnCallBack);\
183 pResult = pfnCallBack(psHead, ap);\
185 psHead = psNextNode;\
190 /*those ones are for extra type safety, so there's no need to use castings for the results*/
192 #define DECLARE_LIST_ANY_2(TYPE, RTYPE, CONTINUE) \
193 RTYPE List_##TYPE##_##RTYPE##_Any(TYPE *psHead, RTYPE (*pfnCallBack)(TYPE* psNode))
195 #define IMPLEMENT_LIST_ANY_2(TYPE, RTYPE, CONTINUE) \
196 RTYPE List_##TYPE##_##RTYPE##_Any(TYPE *psHead, RTYPE (*pfnCallBack)(TYPE* psNode))\
201 psNextNode = psHead;\
202 while(psHead && result == CONTINUE)\
204 psNextNode = psNextNode->psNext;\
205 result = pfnCallBack(psHead);\
206 psHead = psNextNode;\
212 #define DECLARE_LIST_ANY_VA_2(TYPE, RTYPE, CONTINUE) \
213 RTYPE List_##TYPE##_##RTYPE##_Any_va(TYPE *psHead, RTYPE(*pfnCallBack)(TYPE* psNode, va_list va), ...)
215 #define IMPLEMENT_LIST_ANY_VA_2(TYPE, RTYPE, CONTINUE) \
216 RTYPE List_##TYPE##_##RTYPE##_Any_va(TYPE *psHead, RTYPE(*pfnCallBack)(TYPE* psNode, va_list va), ...)\
220 RTYPE result = CONTINUE;\
221 while(psHead && result == CONTINUE)\
223 psNextNode = psHead->psNext;\
224 va_start(ap, pfnCallBack);\
225 result = pfnCallBack(psHead, ap);\
227 psHead = psNextNode;\
233 /*************************************************************************/ /*!
234 @Function List_##TYPE##_Remove
235 @Description Removes a given node from the list.
236 @Input psNode The pointer to the node to be removed.
237 */ /**************************************************************************/
238 #define DECLARE_LIST_REMOVE(TYPE) \
239 IMG_VOID List_##TYPE##_Remove(TYPE *psNode)
241 #define IMPLEMENT_LIST_REMOVE(TYPE) \
242 IMG_VOID List_##TYPE##_Remove(TYPE *psNode)\
244 (*psNode->ppsThis)=psNode->psNext;\
247 psNode->psNext->ppsThis = psNode->ppsThis;\
251 /*************************************************************************/ /*!
252 @Function List_##TYPE##_Insert
253 @Description Inserts a given node at the beginnning of the list.
254 @Input psHead The pointer to the pointer to the head node.
255 @Input psNode The pointer to the node to be inserted.
256 */ /**************************************************************************/
257 #define DECLARE_LIST_INSERT(TYPE) \
258 IMG_VOID List_##TYPE##_Insert(TYPE **ppsHead, TYPE *psNewNode)
260 #define IMPLEMENT_LIST_INSERT(TYPE) \
261 IMG_VOID List_##TYPE##_Insert(TYPE **ppsHead, TYPE *psNewNode)\
263 psNewNode->ppsThis = ppsHead;\
264 psNewNode->psNext = *ppsHead;\
265 *ppsHead = psNewNode;\
266 if(psNewNode->psNext)\
268 psNewNode->psNext->ppsThis = &(psNewNode->psNext);\
272 /*************************************************************************/ /*!
273 @Function List_##TYPE##_Reverse
274 @Description Reverse a list in place
275 @Input ppsHead The pointer to the pointer to the head node.
276 */ /**************************************************************************/
277 #define DECLARE_LIST_REVERSE(TYPE) \
278 IMG_VOID List_##TYPE##_Reverse(TYPE **ppsHead)
280 #define IMPLEMENT_LIST_REVERSE(TYPE) \
281 IMG_VOID List_##TYPE##_Reverse(TYPE **ppsHead)\
286 psTmpNode1 = IMG_NULL; \
287 psCurNode = *ppsHead; \
289 psTmpNode2 = psCurNode->psNext; \
290 psCurNode->psNext = psTmpNode1; \
291 psTmpNode1 = psCurNode; \
292 psCurNode = psTmpNode2; \
295 psTmpNode1->ppsThis = &(psCurNode->psNext); \
299 psTmpNode1->ppsThis = ppsHead; \
302 *ppsHead = psTmpNode1; \
305 #define IS_LAST_ELEMENT(x) ((x)->psNext == IMG_NULL)
308 DECLARE_LIST_ANY(PVRSRV_DEVICE_NODE);
309 DECLARE_LIST_ANY_2(PVRSRV_DEVICE_NODE, PVRSRV_ERROR, PVRSRV_OK);
310 DECLARE_LIST_ANY_VA(PVRSRV_DEVICE_NODE);
311 DECLARE_LIST_ANY_VA_2(PVRSRV_DEVICE_NODE, PVRSRV_ERROR, PVRSRV_OK);
312 DECLARE_LIST_FOR_EACH(PVRSRV_DEVICE_NODE);
313 DECLARE_LIST_FOR_EACH_VA(PVRSRV_DEVICE_NODE);
314 DECLARE_LIST_INSERT(PVRSRV_DEVICE_NODE);
315 DECLARE_LIST_REMOVE(PVRSRV_DEVICE_NODE);
317 DECLARE_LIST_ANY_VA(PVRSRV_POWER_DEV);
318 DECLARE_LIST_ANY_VA_2(PVRSRV_POWER_DEV, PVRSRV_ERROR, PVRSRV_OK);
319 DECLARE_LIST_INSERT(PVRSRV_POWER_DEV);
320 DECLARE_LIST_REMOVE(PVRSRV_POWER_DEV);
322 #undef DECLARE_LIST_ANY_2
323 #undef DECLARE_LIST_ANY_VA
324 #undef DECLARE_LIST_ANY_VA_2
325 #undef DECLARE_LIST_FOR_EACH
326 #undef DECLARE_LIST_FOR_EACH_VA
327 #undef DECLARE_LIST_INSERT
328 #undef DECLARE_LIST_REMOVE
330 IMG_VOID* MatchDeviceKM_AnyVaCb(PVRSRV_DEVICE_NODE* psDeviceNode, va_list va);
331 IMG_VOID* MatchPowerDeviceIndex_AnyVaCb(PVRSRV_POWER_DEV *psPowerDev, va_list va);
335 /* re-enable warnings */
336 /* PRQA S 0881,3410 -- */