xref: /freebsd/sys/dev/drm2/drm_linux_list.h (revision 685dc743)
1592ffb21SWarner Losh /* drm_linux_list.h -- linux list functions for the BSDs.
2592ffb21SWarner Losh  * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
3592ffb21SWarner Losh  */
4592ffb21SWarner Losh /*-
5592ffb21SWarner Losh  * Copyright 2003 Eric Anholt
6592ffb21SWarner Losh  * All Rights Reserved.
7592ffb21SWarner Losh  *
8592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
9592ffb21SWarner Losh  * copy of this software and associated documentation files (the "Software"),
10592ffb21SWarner Losh  * to deal in the Software without restriction, including without limitation
11592ffb21SWarner Losh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12592ffb21SWarner Losh  * and/or sell copies of the Software, and to permit persons to whom the
13592ffb21SWarner Losh  * Software is furnished to do so, subject to the following conditions:
14592ffb21SWarner Losh  *
15592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the next
16592ffb21SWarner Losh  * paragraph) shall be included in all copies or substantial portions of the
17592ffb21SWarner Losh  * Software.
18592ffb21SWarner Losh  *
19592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22592ffb21SWarner Losh  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23592ffb21SWarner Losh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24592ffb21SWarner Losh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25592ffb21SWarner Losh  * OTHER DEALINGS IN THE SOFTWARE.
26592ffb21SWarner Losh  *
27592ffb21SWarner Losh  * Authors:
28592ffb21SWarner Losh  *    Eric Anholt <anholt@FreeBSD.org>
29592ffb21SWarner Losh  *
30592ffb21SWarner Losh  */
31592ffb21SWarner Losh 
32592ffb21SWarner Losh #include <sys/cdefs.h>
33592ffb21SWarner Losh #ifndef _DRM_LINUX_LIST_H_
34592ffb21SWarner Losh #define _DRM_LINUX_LIST_H_
35592ffb21SWarner Losh 
36592ffb21SWarner Losh struct list_head {
37592ffb21SWarner Losh 	struct list_head *next, *prev;
38592ffb21SWarner Losh };
39592ffb21SWarner Losh 
40592ffb21SWarner Losh #define list_entry(ptr, type, member) container_of(ptr,type,member)
41592ffb21SWarner Losh 
42592ffb21SWarner Losh static __inline__ void
INIT_LIST_HEAD(struct list_head * head)43592ffb21SWarner Losh INIT_LIST_HEAD(struct list_head *head) {
44592ffb21SWarner Losh 	(head)->next = head;
45592ffb21SWarner Losh 	(head)->prev = head;
46592ffb21SWarner Losh }
47592ffb21SWarner Losh 
48592ffb21SWarner Losh #define LIST_HEAD_INIT(name) { &(name), &(name) }
49592ffb21SWarner Losh 
50592ffb21SWarner Losh #define DRM_LIST_HEAD(name) \
51592ffb21SWarner Losh 	struct list_head name = LIST_HEAD_INIT(name)
52592ffb21SWarner Losh 
53592ffb21SWarner Losh static __inline__ int
list_empty(const struct list_head * head)54592ffb21SWarner Losh list_empty(const struct list_head *head) {
55592ffb21SWarner Losh 	return (head)->next == head;
56592ffb21SWarner Losh }
57592ffb21SWarner Losh 
58592ffb21SWarner Losh static __inline__ void
list_add(struct list_head * new,struct list_head * head)59592ffb21SWarner Losh list_add(struct list_head *new, struct list_head *head) {
60592ffb21SWarner Losh         (head)->next->prev = new;
61592ffb21SWarner Losh         (new)->next = (head)->next;
62592ffb21SWarner Losh         (new)->prev = head;
63592ffb21SWarner Losh         (head)->next = new;
64592ffb21SWarner Losh }
65592ffb21SWarner Losh 
66592ffb21SWarner Losh static __inline__ void
list_add_tail(struct list_head * entry,struct list_head * head)67592ffb21SWarner Losh list_add_tail(struct list_head *entry, struct list_head *head) {
68592ffb21SWarner Losh 	(entry)->prev = (head)->prev;
69592ffb21SWarner Losh 	(entry)->next = head;
70592ffb21SWarner Losh 	(head)->prev->next = entry;
71592ffb21SWarner Losh 	(head)->prev = entry;
72592ffb21SWarner Losh }
73592ffb21SWarner Losh 
74592ffb21SWarner Losh static __inline__ void
list_del(struct list_head * entry)75592ffb21SWarner Losh list_del(struct list_head *entry) {
76592ffb21SWarner Losh 	(entry)->next->prev = (entry)->prev;
77592ffb21SWarner Losh 	(entry)->prev->next = (entry)->next;
78592ffb21SWarner Losh }
79592ffb21SWarner Losh 
list_replace(struct list_head * old,struct list_head * new)80592ffb21SWarner Losh static inline void list_replace(struct list_head *old,
81592ffb21SWarner Losh 				struct list_head *new)
82592ffb21SWarner Losh {
83592ffb21SWarner Losh 	new->next = old->next;
84592ffb21SWarner Losh 	new->next->prev = new;
85592ffb21SWarner Losh 	new->prev = old->prev;
86592ffb21SWarner Losh 	new->prev->next = new;
87592ffb21SWarner Losh }
88592ffb21SWarner Losh 
list_move(struct list_head * list,struct list_head * head)89592ffb21SWarner Losh static inline void list_move(struct list_head *list, struct list_head *head)
90592ffb21SWarner Losh {
91592ffb21SWarner Losh 	list_del(list);
92592ffb21SWarner Losh 	list_add(list, head);
93592ffb21SWarner Losh }
94592ffb21SWarner Losh 
list_move_tail(struct list_head * list,struct list_head * head)95592ffb21SWarner Losh static inline void list_move_tail(struct list_head *list,
96592ffb21SWarner Losh     struct list_head *head)
97592ffb21SWarner Losh {
98592ffb21SWarner Losh 	list_del(list);
99592ffb21SWarner Losh 	list_add_tail(list, head);
100592ffb21SWarner Losh }
101592ffb21SWarner Losh 
102592ffb21SWarner Losh static __inline__ void
list_del_init(struct list_head * entry)103592ffb21SWarner Losh list_del_init(struct list_head *entry) {
104592ffb21SWarner Losh 	(entry)->next->prev = (entry)->prev;
105592ffb21SWarner Losh 	(entry)->prev->next = (entry)->next;
106592ffb21SWarner Losh 	INIT_LIST_HEAD(entry);
107592ffb21SWarner Losh }
108592ffb21SWarner Losh 
109592ffb21SWarner Losh #define list_for_each(entry, head)				\
110592ffb21SWarner Losh     for (entry = (head)->next; entry != head; entry = (entry)->next)
111592ffb21SWarner Losh 
112592ffb21SWarner Losh #define list_for_each_prev(entry, head) \
113592ffb21SWarner Losh         for (entry = (head)->prev; entry != (head); \
114592ffb21SWarner Losh                 entry = entry->prev)
115592ffb21SWarner Losh 
116592ffb21SWarner Losh #define list_for_each_safe(entry, temp, head)			\
117592ffb21SWarner Losh     for (entry = (head)->next, temp = (entry)->next;		\
118592ffb21SWarner Losh 	entry != head; 						\
119592ffb21SWarner Losh 	entry = temp, temp = entry->next)
120592ffb21SWarner Losh 
121592ffb21SWarner Losh #define list_for_each_entry(pos, head, member)				\
122592ffb21SWarner Losh     for (pos = list_entry((head)->next, __typeof(*pos), member);	\
123592ffb21SWarner Losh 	&pos->member != (head);					 	\
124592ffb21SWarner Losh 	pos = list_entry(pos->member.next, __typeof(*pos), member))
125592ffb21SWarner Losh 
126592ffb21SWarner Losh #define list_for_each_entry_continue_reverse(pos, head, member)         \
127592ffb21SWarner Losh         for (pos = list_entry(pos->member.prev, __typeof(*pos), member);  \
128592ffb21SWarner Losh              &pos->member != (head);    				\
129592ffb21SWarner Losh              pos = list_entry(pos->member.prev, __typeof(*pos), member))
130592ffb21SWarner Losh 
131592ffb21SWarner Losh /**
132592ffb21SWarner Losh  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
133592ffb21SWarner Losh  * @pos:        the type * to use as a loop cursor.
134592ffb21SWarner Losh  * @n:          another type * to use as temporary storage
135592ffb21SWarner Losh  * @head:       the head for your list.
136592ffb21SWarner Losh  * @member:     the name of the list_struct within the struct.
137592ffb21SWarner Losh  */
138592ffb21SWarner Losh #define list_for_each_entry_safe(pos, n, head, member)			\
139592ffb21SWarner Losh 	for (pos = list_entry((head)->next, __typeof(*pos), member),	\
140592ffb21SWarner Losh 	    n = list_entry(pos->member.next, __typeof(*pos), member);	\
141592ffb21SWarner Losh 	    &pos->member != (head);					\
142592ffb21SWarner Losh 	    pos = n, n = list_entry(n->member.next, __typeof(*n), member))
143592ffb21SWarner Losh 
144592ffb21SWarner Losh #define list_for_each_entry_safe_from(pos, n, head, member) 			\
145592ffb21SWarner Losh 	for (n = list_entry(pos->member.next, __typeof(*pos), member);		\
146592ffb21SWarner Losh 	     &pos->member != (head);						\
147592ffb21SWarner Losh 	     pos = n, n = list_entry(n->member.next, __typeof(*n), member))
148592ffb21SWarner Losh 
149592ffb21SWarner Losh #define list_first_entry(ptr, type, member) \
150592ffb21SWarner Losh 	list_entry((ptr)->next, type, member)
151592ffb21SWarner Losh 
152592ffb21SWarner Losh 
153592ffb21SWarner Losh static inline void
__list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)154592ffb21SWarner Losh __list_splice(const struct list_head *list, struct list_head *prev,
155592ffb21SWarner Losh     struct list_head *next)
156592ffb21SWarner Losh {
157592ffb21SWarner Losh 	struct list_head *first = list->next;
158592ffb21SWarner Losh 	struct list_head *last = list->prev;
159592ffb21SWarner Losh 
160592ffb21SWarner Losh 	first->prev = prev;
161592ffb21SWarner Losh 	prev->next = first;
162592ffb21SWarner Losh 
163592ffb21SWarner Losh 	last->next = next;
164592ffb21SWarner Losh 	next->prev = last;
165592ffb21SWarner Losh }
166592ffb21SWarner Losh 
167592ffb21SWarner Losh static inline void
list_splice(const struct list_head * list,struct list_head * head)168592ffb21SWarner Losh list_splice(const struct list_head *list, struct list_head *head)
169592ffb21SWarner Losh {
170592ffb21SWarner Losh 	if (list_empty(list))
171592ffb21SWarner Losh 		return;
172592ffb21SWarner Losh 
173592ffb21SWarner Losh 	__list_splice(list, head, head->next);
174592ffb21SWarner Losh }
175592ffb21SWarner Losh 
176592ffb21SWarner Losh void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
177592ffb21SWarner Losh     struct list_head *a, struct list_head *b));
178592ffb21SWarner Losh 
179592ffb21SWarner Losh /* hlist, copied from sys/dev/ofed/linux/list.h */
180592ffb21SWarner Losh 
181592ffb21SWarner Losh struct hlist_head {
182592ffb21SWarner Losh 	struct hlist_node *first;
183592ffb21SWarner Losh };
184592ffb21SWarner Losh 
185592ffb21SWarner Losh struct hlist_node {
186592ffb21SWarner Losh 	struct hlist_node *next, **pprev;
187592ffb21SWarner Losh };
188592ffb21SWarner Losh 
189592ffb21SWarner Losh #define	HLIST_HEAD_INIT { }
190592ffb21SWarner Losh #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
191592ffb21SWarner Losh #define	INIT_HLIST_HEAD(head) (head)->first = NULL
192592ffb21SWarner Losh #define	INIT_HLIST_NODE(node)						\
193592ffb21SWarner Losh do {									\
194592ffb21SWarner Losh 	(node)->next = NULL;						\
195592ffb21SWarner Losh 	(node)->pprev = NULL;						\
196592ffb21SWarner Losh } while (0)
197592ffb21SWarner Losh 
198592ffb21SWarner Losh static inline int
hlist_unhashed(const struct hlist_node * h)199592ffb21SWarner Losh hlist_unhashed(const struct hlist_node *h)
200592ffb21SWarner Losh {
201592ffb21SWarner Losh 
202592ffb21SWarner Losh 	return !h->pprev;
203592ffb21SWarner Losh }
204592ffb21SWarner Losh 
205592ffb21SWarner Losh static inline int
hlist_empty(const struct hlist_head * h)206592ffb21SWarner Losh hlist_empty(const struct hlist_head *h)
207592ffb21SWarner Losh {
208592ffb21SWarner Losh 
209592ffb21SWarner Losh 	return !h->first;
210592ffb21SWarner Losh }
211592ffb21SWarner Losh 
212592ffb21SWarner Losh static inline void
hlist_del(struct hlist_node * n)213592ffb21SWarner Losh hlist_del(struct hlist_node *n)
214592ffb21SWarner Losh {
215592ffb21SWarner Losh 
216592ffb21SWarner Losh         if (n->next)
217592ffb21SWarner Losh                 n->next->pprev = n->pprev;
218592ffb21SWarner Losh         *n->pprev = n->next;
219592ffb21SWarner Losh }
220592ffb21SWarner Losh 
221592ffb21SWarner Losh static inline void
hlist_del_init(struct hlist_node * n)222592ffb21SWarner Losh hlist_del_init(struct hlist_node *n)
223592ffb21SWarner Losh {
224592ffb21SWarner Losh 
225592ffb21SWarner Losh 	if (hlist_unhashed(n))
226592ffb21SWarner Losh 		return;
227592ffb21SWarner Losh 	hlist_del(n);
228592ffb21SWarner Losh 	INIT_HLIST_NODE(n);
229592ffb21SWarner Losh }
230592ffb21SWarner Losh 
231592ffb21SWarner Losh static inline void
hlist_add_head(struct hlist_node * n,struct hlist_head * h)232592ffb21SWarner Losh hlist_add_head(struct hlist_node *n, struct hlist_head *h)
233592ffb21SWarner Losh {
234592ffb21SWarner Losh 
235592ffb21SWarner Losh 	n->next = h->first;
236592ffb21SWarner Losh 	if (h->first)
237592ffb21SWarner Losh 		h->first->pprev = &n->next;
238592ffb21SWarner Losh 	h->first = n;
239592ffb21SWarner Losh 	n->pprev = &h->first;
240592ffb21SWarner Losh }
241592ffb21SWarner Losh 
242592ffb21SWarner Losh static inline void
hlist_add_before(struct hlist_node * n,struct hlist_node * next)243592ffb21SWarner Losh hlist_add_before(struct hlist_node *n, struct hlist_node *next)
244592ffb21SWarner Losh {
245592ffb21SWarner Losh 
246592ffb21SWarner Losh 	n->pprev = next->pprev;
247592ffb21SWarner Losh 	n->next = next;
248592ffb21SWarner Losh 	next->pprev = &n->next;
249592ffb21SWarner Losh 	*(n->pprev) = n;
250592ffb21SWarner Losh }
251592ffb21SWarner Losh 
252592ffb21SWarner Losh static inline void
hlist_add_after(struct hlist_node * n,struct hlist_node * next)253592ffb21SWarner Losh hlist_add_after(struct hlist_node *n, struct hlist_node *next)
254592ffb21SWarner Losh {
255592ffb21SWarner Losh 
256592ffb21SWarner Losh 	next->next = n->next;
257592ffb21SWarner Losh 	n->next = next;
258592ffb21SWarner Losh 	next->pprev = &n->next;
259592ffb21SWarner Losh 	if (next->next)
260592ffb21SWarner Losh 		next->next->pprev = &next->next;
261592ffb21SWarner Losh }
262592ffb21SWarner Losh 
263592ffb21SWarner Losh static inline void
hlist_move_list(struct hlist_head * old,struct hlist_head * new)264592ffb21SWarner Losh hlist_move_list(struct hlist_head *old, struct hlist_head *new)
265592ffb21SWarner Losh {
266592ffb21SWarner Losh 
267592ffb21SWarner Losh 	new->first = old->first;
268592ffb21SWarner Losh 	if (new->first)
269592ffb21SWarner Losh 		new->first->pprev = &new->first;
270592ffb21SWarner Losh 	old->first = NULL;
271592ffb21SWarner Losh }
272592ffb21SWarner Losh 
273592ffb21SWarner Losh #define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
274592ffb21SWarner Losh 
275592ffb21SWarner Losh #define	hlist_for_each(p, head)						\
276592ffb21SWarner Losh 	for (p = (head)->first; p; p = p->next)
277592ffb21SWarner Losh 
278592ffb21SWarner Losh #define	hlist_for_each_safe(p, n, head)					\
279592ffb21SWarner Losh 	for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
280592ffb21SWarner Losh 
281592ffb21SWarner Losh #define	hlist_for_each_entry(tp, p, head, field)			\
282592ffb21SWarner Losh 	for (p = (head)->first;						\
283592ffb21SWarner Losh 	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
284592ffb21SWarner Losh 
285592ffb21SWarner Losh #define hlist_for_each_entry_continue(tp, p, field)			\
286592ffb21SWarner Losh 	for (p = (p)->next;						\
287592ffb21SWarner Losh 	    p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
288592ffb21SWarner Losh 
289592ffb21SWarner Losh #define	hlist_for_each_entry_from(tp, p, field)				\
290592ffb21SWarner Losh 	for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
291592ffb21SWarner Losh 
292592ffb21SWarner Losh #define hlist_for_each_entry_safe(tpos, pos, n, head, member) 		 \
293592ffb21SWarner Losh 	for (pos = (head)->first;					 \
294592ffb21SWarner Losh 	     (pos) != 0 && ({ n = (pos)->next; \
295592ffb21SWarner Losh 		 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
296592ffb21SWarner Losh 	     pos = (n))
297592ffb21SWarner Losh 
298592ffb21SWarner Losh #endif /* _DRM_LINUX_LIST_H_ */
299