xref: /gfx-drm/usr/src/uts/common/drm/drm_linux_list.h (revision 47dc10d7)
1 /* BEGIN CSTYLED */
2 
3 /*
4  * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
5  */
6 
7 /*
8  * drm_linux_list.h -- linux list functions for the BSDs.
9  * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
10  */
11 /*
12  * -
13  * Copyright 2003 Eric Anholt
14  * Copyright (c) 2009, 2012, Intel Corporation.
15  * All Rights Reserved.
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining a
18  * copy of this software and associated documentation files (the "Software"),
19  * to deal in the Software without restriction, including without limitation
20  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following conditions:
23  *
24  * The above copyright notice and this permission notice (including the next
25  * paragraph) shall be included in all copies or substantial portions of the
26  * Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  * OTHER DEALINGS IN THE SOFTWARE.
35  *
36  * Authors:
37  *    Eric Anholt <anholt@FreeBSD.org>
38  *
39  */
40 
41 #ifndef _DRM_LINUX_LIST_H_
42 #define	_DRM_LINUX_LIST_H_
43 
44 #include <sys/types.h>
45 #include <sys/param.h>
46 struct list_head {
47 	struct list_head *next, *prev;
48 	caddr_t contain_ptr;
49 };
50 
51 /* Cheat, assume the list_head is at the start of the struct */
52 #define container_of(ptr, type, member)				\
53 	((type *)(uintptr_t)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
54 
55 #define	list_entry(ptr, type, member)				\
56 	ptr ? ((type *)(uintptr_t)(ptr->contain_ptr)) : NULL
57 
58 #define list_first_entry(ptr, type, member)			\
59 	list_entry((ptr)->next, type, member)
60 
61 #define	list_empty(head)					\
62 	((head)->next == head)
63 
64 #define	INIT_LIST_HEAD(head)					\
65 do { 								\
66 	(head)->next = head;					\
67 	(head)->prev = head;					\
68 	(head)->contain_ptr = NULL;				\
69 } while (*"\0")
70 
71 #define	list_add(ptr, head, entry)				\
72 do {								\
73 	struct list_head *n_node = (head)->next;		\
74 	(ptr)->prev = head;					\
75 	(ptr)->next = (head)->next;				\
76 	n_node->prev = ptr;					\
77 	(head)->next = ptr;					\
78 	(ptr)->contain_ptr = entry;				\
79 } while (*"\0")
80 
81 #define	list_add_tail(ptr, head, entry)				\
82 do {								\
83 	struct list_head *p_node = (head)->prev;		\
84 	(ptr)->prev = (head)->prev;				\
85 	(ptr)->next = head;					\
86 	p_node->next = ptr;					\
87 	(head)->prev = ptr;					\
88 	(ptr)->contain_ptr = entry;				\
89 } while (*"\0")
90 
91 #define	list_del(ptr)						\
92 do {								\
93 	struct list_head *n_node = (ptr)->next;			\
94 	struct list_head *p_node = (ptr)->prev;			\
95 	n_node->prev = (ptr)->prev;				\
96 	p_node->next = (ptr)->next;				\
97 	(ptr)->prev = NULL;					\
98 	(ptr)->next = NULL;					\
99 } while (*"\0")
100 
101 #define	list_del_init(ptr)					\
102 do {								\
103 	list_del(ptr);						\
104 	INIT_LIST_HEAD(ptr);					\
105 } while (*"\0")
106 
107 #define	list_move(ptr, head, entry)			\
108 do {								\
109 	list_del(ptr);						\
110 	list_add(ptr, head, entry);			\
111 } while (*"\0")
112 
113 
114 #define	list_move_tail(ptr, head, entry)			\
115 do {								\
116 	list_del(ptr);						\
117 	list_add_tail(ptr, head, entry);			\
118 } while (*"\0")
119 
120 #define	list_splice(list, be, ne)				\
121 do {								\
122 	if (!list_empty(list)) {				\
123 		struct list_head *first = (list)->next;		\
124 		struct list_head *last = (list)->prev;		\
125 		first->prev = be;				\
126 		(be)->next = first;				\
127 		last->next = ne;				\
128 		(ne)->prev = last;				\
129 	}							\
130 } while (*"\0")
131 
132 #define list_replace(old, new)			\
133 do {								\
134 	struct list_head *old_list = old;			\
135 	struct list_head *new_list = new;			\
136 	new_list->next = old_list->next;			\
137 	new_list->next->prev = new_list;			\
138 	new_list->prev = old_list->prev;			\
139 	new_list->prev->next = new_list;			\
140 } while (*"\0")
141 
142 #define	list_for_each(pos, head)				\
143 	for (pos = (head)->next; pos != head; pos = (pos)->next)
144 
145 #define	list_for_each_safe(pos, n, head)			\
146 	for (pos = (head)->next, n = (pos)->next;		\
147 	    pos != head; 					\
148 	    pos = n, n = n->next)
149 
150 #define list_for_each_entry(pos, type, head, member)		\
151 	for (pos = list_entry((head)->next, type, member); pos; \
152 	    pos = list_entry(pos->member.next, type, member))
153 
154 #define list_for_each_entry_safe(pos, n, type, head, member)	\
155 	for (pos = list_entry((head)->next, type, member),	\
156 	    n = pos ? list_entry(pos->member.next, type, member) : pos;	\
157 	    pos;						\
158 	    pos = n,						\
159 	    n = list_entry((n ? n->member.next : (head)->next), type, member))
160 
161 #define list_for_each_entry_continue_reverse(pos, type, head, member)	\
162 	for (pos = list_entry(pos->member.prev, type, member);		\
163 		pos;							\
164 		pos = list_entry(pos->member.prev, type, member))
165 #endif /* _DRM_LINUX_LIST_H_ */
166