1f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt/**************************************************************************
2f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt *
3f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
4f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * All Rights Reserved.
5f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt *
6f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
7f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * copy of this software and associated documentation files (the
8f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * "Software"), to deal in the Software without restriction, including
9f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * without limitation the rights to use, copy, modify, merge, publish,
10f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * distribute, sub license, and/or sell copies of the Software, and to
11f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * permit persons to whom the Software is furnished to do so, subject to
12f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * the following conditions:
13f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt *
14f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * USE OR OTHER DEALINGS IN THE SOFTWARE.
21f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt *
22f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * The above copyright notice and this permission notice (including the
23f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * next paragraph) shall be included in all copies or substantial portions
24f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * of the Software.
25f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt */
26f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
27f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt/*
28f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * List macros heavily inspired by the Linux kernel
29f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt * list handling. No list looping yet.
30f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt */
31f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
3272abe983adfe7e8dcdcec11f1bc11d0b3daae063Eric Anholt#include <stddef.h>
3372abe983adfe7e8dcdcec11f1bc11d0b3daae063Eric Anholt
34f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholttypedef struct _drmMMListHead
35f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt{
36f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    struct _drmMMListHead *prev;
37f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    struct _drmMMListHead *next;
38f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt} drmMMListHead;
39f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
40f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMINITLISTHEAD(__item)		       \
41f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  do{					       \
42f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->prev = (__item);		       \
43f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->next = (__item);		       \
44f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  } while (0)
45f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
46f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMLISTADD(__item, __list)		\
47f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  do {						\
48f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->prev = (__list);			\
49f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->next = (__list)->next;		\
50f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__list)->next->prev = (__item);		\
51f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__list)->next = (__item);			\
52f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  } while (0)
53f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
54f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMLISTADDTAIL(__item, __list)		\
55f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  do {						\
56f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->next = (__list);			\
57f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->prev = (__list)->prev;		\
58f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__list)->prev->next = (__item);		\
59f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__list)->prev = (__item);			\
60f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  } while(0)
61f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
62f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMLISTDEL(__item)			\
63f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  do {						\
64f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->prev->next = (__item)->next;	\
65f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->next->prev = (__item)->prev;	\
66f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  } while(0)
67f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
68f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMLISTDELINIT(__item)			\
69f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  do {						\
70f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->prev->next = (__item)->next;	\
71f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->next->prev = (__item)->prev;	\
72f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->next = (__item);			\
73f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    (__item)->prev = (__item);			\
74f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt  } while(0)
75f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
76f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMLISTENTRY(__type, __item, __field)   \
77f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt    ((__type *)(((char *) (__item)) - offsetof(__type, __field)))
78f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
79f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMLISTEMPTY(__item) ((__item)->next == (__item))
80f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
810d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs#define DRMLISTSINGLE(__list) \
820d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	(!DRMLISTEMPTY(__list) && ((__list)->next == (__list)->prev))
830d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs
840d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs#define DRMLISTFOREACH(__item, __list)					\
850d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	for ((__item) = (__list)->next;					\
860d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	     (__item) != (__list); (__item) = (__item)->next)
870d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs
88f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMLISTFOREACHSAFE(__item, __temp, __list)			\
89f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt	for ((__item) = (__list)->next, (__temp) = (__item)->next;	\
90f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt	     (__item) != (__list);					\
91f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt	     (__item) = (__temp), (__temp) = (__item)->next)
92f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt
93f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt#define DRMLISTFOREACHSAFEREVERSE(__item, __temp, __list)		\
94f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt	for ((__item) = (__list)->prev, (__temp) = (__item)->prev;	\
95f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt	     (__item) != (__list);					\
96f7a99407153eaba5724b6f1f2cadab62c6a50a26Eric Anholt	     (__item) = (__temp), (__temp) = (__item)->prev)
970d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs
980d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs#define DRMLISTFOREACHENTRY(__item, __list, __head)                            \
990d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	for ((__item) = DRMLISTENTRY(typeof(*__item), (__list)->next, __head); \
1000d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	     &(__item)->__head != (__list);                                    \
1010d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	     (__item) = DRMLISTENTRY(typeof(*__item),                          \
1020d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs				     (__item)->__head.next, __head))
1030d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs
1040d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs#define DRMLISTFOREACHENTRYSAFE(__item, __temp, __list, __head)                \
1050d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	for ((__item) = DRMLISTENTRY(typeof(*__item), (__list)->next, __head), \
1060d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	     (__temp) = DRMLISTENTRY(typeof(*__item),                          \
1070d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs				     (__item)->__head.next, __head);           \
1080d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	     &(__item)->__head != (__list);                                    \
1090d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	     (__item) = (__temp),                                              \
1100d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	     (__temp) = DRMLISTENTRY(typeof(*__item),                          \
1110d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs				     (__temp)->__head.next, __head))
1120d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs
1130d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs#define DRMLISTJOIN(__list, __join) if (!DRMLISTEMPTY(__list)) {	\
1140d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	(__list)->next->prev = (__join);				\
1150d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	(__list)->prev->next = (__join)->next;				\
1160d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	(__join)->next->prev = (__list)->prev;				\
1170d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs	(__join)->next = (__list)->next;				\
1180d6350002d74848dd3d1e4d28d514a7ac9d580c9Ben Skeggs}
119