1*592ffb21SWarner Losh /* drm_linux_list.h -- linux list functions for the BSDs.
2*592ffb21SWarner Losh * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
3*592ffb21SWarner Losh */
4*592ffb21SWarner Losh /*-
5*592ffb21SWarner Losh * Copyright 2003 Eric Anholt
6*592ffb21SWarner Losh * All Rights Reserved.
7*592ffb21SWarner Losh *
8*592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
9*592ffb21SWarner Losh * copy of this software and associated documentation files (the "Software"),
10*592ffb21SWarner Losh * to deal in the Software without restriction, including without limitation
11*592ffb21SWarner Losh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12*592ffb21SWarner Losh * and/or sell copies of the Software, and to permit persons to whom the
13*592ffb21SWarner Losh * Software is furnished to do so, subject to the following conditions:
14*592ffb21SWarner Losh *
15*592ffb21SWarner Losh * The above copyright notice and this permission notice (including the next
16*592ffb21SWarner Losh * paragraph) shall be included in all copies or substantial portions of the
17*592ffb21SWarner Losh * Software.
18*592ffb21SWarner Losh *
19*592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22*592ffb21SWarner Losh * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23*592ffb21SWarner Losh * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24*592ffb21SWarner Losh * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25*592ffb21SWarner Losh * OTHER DEALINGS IN THE SOFTWARE.
26*592ffb21SWarner Losh *
27*592ffb21SWarner Losh * Authors:
28*592ffb21SWarner Losh * Eric Anholt <anholt@FreeBSD.org>
29*592ffb21SWarner Losh *
30*592ffb21SWarner Losh */
31*592ffb21SWarner Losh
32*592ffb21SWarner Losh #include <sys/cdefs.h>
33*592ffb21SWarner Losh #ifndef _DRM_LINUX_LIST_H_
34*592ffb21SWarner Losh #define _DRM_LINUX_LIST_H_
35*592ffb21SWarner Losh
36*592ffb21SWarner Losh struct list_head {
37*592ffb21SWarner Losh struct list_head *next, *prev;
38*592ffb21SWarner Losh };
39*592ffb21SWarner Losh
40*592ffb21SWarner Losh #define list_entry(ptr, type, member) container_of(ptr,type,member)
41*592ffb21SWarner Losh
42*592ffb21SWarner Losh static __inline__ void
INIT_LIST_HEAD(struct list_head * head)43*592ffb21SWarner Losh INIT_LIST_HEAD(struct list_head *head) {
44*592ffb21SWarner Losh (head)->next = head;
45*592ffb21SWarner Losh (head)->prev = head;
46*592ffb21SWarner Losh }
47*592ffb21SWarner Losh
48*592ffb21SWarner Losh #define LIST_HEAD_INIT(name) { &(name), &(name) }
49*592ffb21SWarner Losh
50*592ffb21SWarner Losh #define DRM_LIST_HEAD(name) \
51*592ffb21SWarner Losh struct list_head name = LIST_HEAD_INIT(name)
52*592ffb21SWarner Losh
53*592ffb21SWarner Losh static __inline__ int
list_empty(const struct list_head * head)54*592ffb21SWarner Losh list_empty(const struct list_head *head) {
55*592ffb21SWarner Losh return (head)->next == head;
56*592ffb21SWarner Losh }
57*592ffb21SWarner Losh
58*592ffb21SWarner Losh static __inline__ void
list_add(struct list_head * new,struct list_head * head)59*592ffb21SWarner Losh list_add(struct list_head *new, struct list_head *head) {
60*592ffb21SWarner Losh (head)->next->prev = new;
61*592ffb21SWarner Losh (new)->next = (head)->next;
62*592ffb21SWarner Losh (new)->prev = head;
63*592ffb21SWarner Losh (head)->next = new;
64*592ffb21SWarner Losh }
65*592ffb21SWarner Losh
66*592ffb21SWarner Losh static __inline__ void
list_add_tail(struct list_head * entry,struct list_head * head)67*592ffb21SWarner Losh list_add_tail(struct list_head *entry, struct list_head *head) {
68*592ffb21SWarner Losh (entry)->prev = (head)->prev;
69*592ffb21SWarner Losh (entry)->next = head;
70*592ffb21SWarner Losh (head)->prev->next = entry;
71*592ffb21SWarner Losh (head)->prev = entry;
72*592ffb21SWarner Losh }
73*592ffb21SWarner Losh
74*592ffb21SWarner Losh static __inline__ void
list_del(struct list_head * entry)75*592ffb21SWarner Losh list_del(struct list_head *entry) {
76*592ffb21SWarner Losh (entry)->next->prev = (entry)->prev;
77*592ffb21SWarner Losh (entry)->prev->next = (entry)->next;
78*592ffb21SWarner Losh }
79*592ffb21SWarner Losh
list_replace(struct list_head * old,struct list_head * new)80*592ffb21SWarner Losh static inline void list_replace(struct list_head *old,
81*592ffb21SWarner Losh struct list_head *new)
82*592ffb21SWarner Losh {
83*592ffb21SWarner Losh new->next = old->next;
84*592ffb21SWarner Losh new->next->prev = new;
85*592ffb21SWarner Losh new->prev = old->prev;
86*592ffb21SWarner Losh new->prev->next = new;
87*592ffb21SWarner Losh }
88*592ffb21SWarner Losh
list_move(struct list_head * list,struct list_head * head)89*592ffb21SWarner Losh static inline void list_move(struct list_head *list, struct list_head *head)
90*592ffb21SWarner Losh {
91*592ffb21SWarner Losh list_del(list);
92*592ffb21SWarner Losh list_add(list, head);
93*592ffb21SWarner Losh }
94*592ffb21SWarner Losh
list_move_tail(struct list_head * list,struct list_head * head)95*592ffb21SWarner Losh static inline void list_move_tail(struct list_head *list,
96*592ffb21SWarner Losh struct list_head *head)
97*592ffb21SWarner Losh {
98*592ffb21SWarner Losh list_del(list);
99*592ffb21SWarner Losh list_add_tail(list, head);
100*592ffb21SWarner Losh }
101*592ffb21SWarner Losh
102*592ffb21SWarner Losh static __inline__ void
list_del_init(struct list_head * entry)103*592ffb21SWarner Losh list_del_init(struct list_head *entry) {
104*592ffb21SWarner Losh (entry)->next->prev = (entry)->prev;
105*592ffb21SWarner Losh (entry)->prev->next = (entry)->next;
106*592ffb21SWarner Losh INIT_LIST_HEAD(entry);
107*592ffb21SWarner Losh }
108*592ffb21SWarner Losh
109*592ffb21SWarner Losh #define list_for_each(entry, head) \
110*592ffb21SWarner Losh for (entry = (head)->next; entry != head; entry = (entry)->next)
111*592ffb21SWarner Losh
112*592ffb21SWarner Losh #define list_for_each_prev(entry, head) \
113*592ffb21SWarner Losh for (entry = (head)->prev; entry != (head); \
114*592ffb21SWarner Losh entry = entry->prev)
115*592ffb21SWarner Losh
116*592ffb21SWarner Losh #define list_for_each_safe(entry, temp, head) \
117*592ffb21SWarner Losh for (entry = (head)->next, temp = (entry)->next; \
118*592ffb21SWarner Losh entry != head; \
119*592ffb21SWarner Losh entry = temp, temp = entry->next)
120*592ffb21SWarner Losh
121*592ffb21SWarner Losh #define list_for_each_entry(pos, head, member) \
122*592ffb21SWarner Losh for (pos = list_entry((head)->next, __typeof(*pos), member); \
123*592ffb21SWarner Losh &pos->member != (head); \
124*592ffb21SWarner Losh pos = list_entry(pos->member.next, __typeof(*pos), member))
125*592ffb21SWarner Losh
126*592ffb21SWarner Losh #define list_for_each_entry_continue_reverse(pos, head, member) \
127*592ffb21SWarner Losh for (pos = list_entry(pos->member.prev, __typeof(*pos), member); \
128*592ffb21SWarner Losh &pos->member != (head); \
129*592ffb21SWarner Losh pos = list_entry(pos->member.prev, __typeof(*pos), member))
130*592ffb21SWarner Losh
131*592ffb21SWarner Losh /**
132*592ffb21SWarner Losh * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
133*592ffb21SWarner Losh * @pos: the type * to use as a loop cursor.
134*592ffb21SWarner Losh * @n: another type * to use as temporary storage
135*592ffb21SWarner Losh * @head: the head for your list.
136*592ffb21SWarner Losh * @member: the name of the list_struct within the struct.
137*592ffb21SWarner Losh */
138*592ffb21SWarner Losh #define list_for_each_entry_safe(pos, n, head, member) \
139*592ffb21SWarner Losh for (pos = list_entry((head)->next, __typeof(*pos), member), \
140*592ffb21SWarner Losh n = list_entry(pos->member.next, __typeof(*pos), member); \
141*592ffb21SWarner Losh &pos->member != (head); \
142*592ffb21SWarner Losh pos = n, n = list_entry(n->member.next, __typeof(*n), member))
143*592ffb21SWarner Losh
144*592ffb21SWarner Losh #define list_for_each_entry_safe_from(pos, n, head, member) \
145*592ffb21SWarner Losh for (n = list_entry(pos->member.next, __typeof(*pos), member); \
146*592ffb21SWarner Losh &pos->member != (head); \
147*592ffb21SWarner Losh pos = n, n = list_entry(n->member.next, __typeof(*n), member))
148*592ffb21SWarner Losh
149*592ffb21SWarner Losh #define list_first_entry(ptr, type, member) \
150*592ffb21SWarner Losh list_entry((ptr)->next, type, member)
151*592ffb21SWarner Losh
152*592ffb21SWarner Losh
153*592ffb21SWarner Losh static inline void
__list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)154*592ffb21SWarner Losh __list_splice(const struct list_head *list, struct list_head *prev,
155*592ffb21SWarner Losh struct list_head *next)
156*592ffb21SWarner Losh {
157*592ffb21SWarner Losh struct list_head *first = list->next;
158*592ffb21SWarner Losh struct list_head *last = list->prev;
159*592ffb21SWarner Losh
160*592ffb21SWarner Losh first->prev = prev;
161*592ffb21SWarner Losh prev->next = first;
162*592ffb21SWarner Losh
163*592ffb21SWarner Losh last->next = next;
164*592ffb21SWarner Losh next->prev = last;
165*592ffb21SWarner Losh }
166*592ffb21SWarner Losh
167*592ffb21SWarner Losh static inline void
list_splice(const struct list_head * list,struct list_head * head)168*592ffb21SWarner Losh list_splice(const struct list_head *list, struct list_head *head)
169*592ffb21SWarner Losh {
170*592ffb21SWarner Losh if (list_empty(list))
171*592ffb21SWarner Losh return;
172*592ffb21SWarner Losh
173*592ffb21SWarner Losh __list_splice(list, head, head->next);
174*592ffb21SWarner Losh }
175*592ffb21SWarner Losh
176*592ffb21SWarner Losh void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
177*592ffb21SWarner Losh struct list_head *a, struct list_head *b));
178*592ffb21SWarner Losh
179*592ffb21SWarner Losh /* hlist, copied from sys/dev/ofed/linux/list.h */
180*592ffb21SWarner Losh
181*592ffb21SWarner Losh struct hlist_head {
182*592ffb21SWarner Losh struct hlist_node *first;
183*592ffb21SWarner Losh };
184*592ffb21SWarner Losh
185*592ffb21SWarner Losh struct hlist_node {
186*592ffb21SWarner Losh struct hlist_node *next, **pprev;
187*592ffb21SWarner Losh };
188*592ffb21SWarner Losh
189*592ffb21SWarner Losh #define HLIST_HEAD_INIT { }
190*592ffb21SWarner Losh #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
191*592ffb21SWarner Losh #define INIT_HLIST_HEAD(head) (head)->first = NULL
192*592ffb21SWarner Losh #define INIT_HLIST_NODE(node) \
193*592ffb21SWarner Losh do { \
194*592ffb21SWarner Losh (node)->next = NULL; \
195*592ffb21SWarner Losh (node)->pprev = NULL; \
196*592ffb21SWarner Losh } while (0)
197*592ffb21SWarner Losh
198*592ffb21SWarner Losh static inline int
hlist_unhashed(const struct hlist_node * h)199*592ffb21SWarner Losh hlist_unhashed(const struct hlist_node *h)
200*592ffb21SWarner Losh {
201*592ffb21SWarner Losh
202*592ffb21SWarner Losh return !h->pprev;
203*592ffb21SWarner Losh }
204*592ffb21SWarner Losh
205*592ffb21SWarner Losh static inline int
hlist_empty(const struct hlist_head * h)206*592ffb21SWarner Losh hlist_empty(const struct hlist_head *h)
207*592ffb21SWarner Losh {
208*592ffb21SWarner Losh
209*592ffb21SWarner Losh return !h->first;
210*592ffb21SWarner Losh }
211*592ffb21SWarner Losh
212*592ffb21SWarner Losh static inline void
hlist_del(struct hlist_node * n)213*592ffb21SWarner Losh hlist_del(struct hlist_node *n)
214*592ffb21SWarner Losh {
215*592ffb21SWarner Losh
216*592ffb21SWarner Losh if (n->next)
217*592ffb21SWarner Losh n->next->pprev = n->pprev;
218*592ffb21SWarner Losh *n->pprev = n->next;
219*592ffb21SWarner Losh }
220*592ffb21SWarner Losh
221*592ffb21SWarner Losh static inline void
hlist_del_init(struct hlist_node * n)222*592ffb21SWarner Losh hlist_del_init(struct hlist_node *n)
223*592ffb21SWarner Losh {
224*592ffb21SWarner Losh
225*592ffb21SWarner Losh if (hlist_unhashed(n))
226*592ffb21SWarner Losh return;
227*592ffb21SWarner Losh hlist_del(n);
228*592ffb21SWarner Losh INIT_HLIST_NODE(n);
229*592ffb21SWarner Losh }
230*592ffb21SWarner Losh
231*592ffb21SWarner Losh static inline void
hlist_add_head(struct hlist_node * n,struct hlist_head * h)232*592ffb21SWarner Losh hlist_add_head(struct hlist_node *n, struct hlist_head *h)
233*592ffb21SWarner Losh {
234*592ffb21SWarner Losh
235*592ffb21SWarner Losh n->next = h->first;
236*592ffb21SWarner Losh if (h->first)
237*592ffb21SWarner Losh h->first->pprev = &n->next;
238*592ffb21SWarner Losh h->first = n;
239*592ffb21SWarner Losh n->pprev = &h->first;
240*592ffb21SWarner Losh }
241*592ffb21SWarner Losh
242*592ffb21SWarner Losh static inline void
hlist_add_before(struct hlist_node * n,struct hlist_node * next)243*592ffb21SWarner Losh hlist_add_before(struct hlist_node *n, struct hlist_node *next)
244*592ffb21SWarner Losh {
245*592ffb21SWarner Losh
246*592ffb21SWarner Losh n->pprev = next->pprev;
247*592ffb21SWarner Losh n->next = next;
248*592ffb21SWarner Losh next->pprev = &n->next;
249*592ffb21SWarner Losh *(n->pprev) = n;
250*592ffb21SWarner Losh }
251*592ffb21SWarner Losh
252*592ffb21SWarner Losh static inline void
hlist_add_after(struct hlist_node * n,struct hlist_node * next)253*592ffb21SWarner Losh hlist_add_after(struct hlist_node *n, struct hlist_node *next)
254*592ffb21SWarner Losh {
255*592ffb21SWarner Losh
256*592ffb21SWarner Losh next->next = n->next;
257*592ffb21SWarner Losh n->next = next;
258*592ffb21SWarner Losh next->pprev = &n->next;
259*592ffb21SWarner Losh if (next->next)
260*592ffb21SWarner Losh next->next->pprev = &next->next;
261*592ffb21SWarner Losh }
262*592ffb21SWarner Losh
263*592ffb21SWarner Losh static inline void
hlist_move_list(struct hlist_head * old,struct hlist_head * new)264*592ffb21SWarner Losh hlist_move_list(struct hlist_head *old, struct hlist_head *new)
265*592ffb21SWarner Losh {
266*592ffb21SWarner Losh
267*592ffb21SWarner Losh new->first = old->first;
268*592ffb21SWarner Losh if (new->first)
269*592ffb21SWarner Losh new->first->pprev = &new->first;
270*592ffb21SWarner Losh old->first = NULL;
271*592ffb21SWarner Losh }
272*592ffb21SWarner Losh
273*592ffb21SWarner Losh #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
274*592ffb21SWarner Losh
275*592ffb21SWarner Losh #define hlist_for_each(p, head) \
276*592ffb21SWarner Losh for (p = (head)->first; p; p = p->next)
277*592ffb21SWarner Losh
278*592ffb21SWarner Losh #define hlist_for_each_safe(p, n, head) \
279*592ffb21SWarner Losh for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
280*592ffb21SWarner Losh
281*592ffb21SWarner Losh #define hlist_for_each_entry(tp, p, head, field) \
282*592ffb21SWarner Losh for (p = (head)->first; \
283*592ffb21SWarner Losh p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
284*592ffb21SWarner Losh
285*592ffb21SWarner Losh #define hlist_for_each_entry_continue(tp, p, field) \
286*592ffb21SWarner Losh for (p = (p)->next; \
287*592ffb21SWarner Losh p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
288*592ffb21SWarner Losh
289*592ffb21SWarner Losh #define hlist_for_each_entry_from(tp, p, field) \
290*592ffb21SWarner Losh for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
291*592ffb21SWarner Losh
292*592ffb21SWarner Losh #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
293*592ffb21SWarner Losh for (pos = (head)->first; \
294*592ffb21SWarner Losh (pos) != 0 && ({ n = (pos)->next; \
295*592ffb21SWarner Losh tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
296*592ffb21SWarner Losh pos = (n))
297*592ffb21SWarner Losh
298*592ffb21SWarner Losh #endif /* _DRM_LINUX_LIST_H_ */
299