1 /* drm_linux_list.h -- linux list functions for the BSDs.
2 * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
5 * Copyright 2003 Eric Anholt
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 * Eric Anholt <anholt@FreeBSD.org>
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #ifndef _DRM_LINUX_LIST_H_
36 #define _DRM_LINUX_LIST_H_
39 struct list_head *next, *prev;
42 #define list_entry(ptr, type, member) container_of(ptr,type,member)
44 static __inline__ void
45 INIT_LIST_HEAD(struct list_head *head) {
50 #define LIST_HEAD_INIT(name) { &(name), &(name) }
52 #define DRM_LIST_HEAD(name) \
53 struct list_head name = LIST_HEAD_INIT(name)
56 list_empty(const struct list_head *head) {
57 return (head)->next == head;
60 static __inline__ void
61 list_add(struct list_head *new, struct list_head *head) {
62 (head)->next->prev = new;
63 (new)->next = (head)->next;
68 static __inline__ void
69 list_add_tail(struct list_head *entry, struct list_head *head) {
70 (entry)->prev = (head)->prev;
72 (head)->prev->next = entry;
76 static __inline__ void
77 list_del(struct list_head *entry) {
78 (entry)->next->prev = (entry)->prev;
79 (entry)->prev->next = (entry)->next;
82 static inline void list_replace(struct list_head *old,
83 struct list_head *new)
85 new->next = old->next;
86 new->next->prev = new;
87 new->prev = old->prev;
88 new->prev->next = new;
91 static inline void list_move(struct list_head *list, struct list_head *head)
97 static inline void list_move_tail(struct list_head *list,
98 struct list_head *head)
101 list_add_tail(list, head);
104 static __inline__ void
105 list_del_init(struct list_head *entry) {
106 (entry)->next->prev = (entry)->prev;
107 (entry)->prev->next = (entry)->next;
108 INIT_LIST_HEAD(entry);
111 #define list_for_each(entry, head) \
112 for (entry = (head)->next; entry != head; entry = (entry)->next)
114 #define list_for_each_prev(entry, head) \
115 for (entry = (head)->prev; entry != (head); \
118 #define list_for_each_safe(entry, temp, head) \
119 for (entry = (head)->next, temp = (entry)->next; \
121 entry = temp, temp = entry->next)
123 #define list_for_each_entry(pos, head, member) \
124 for (pos = list_entry((head)->next, __typeof(*pos), member); \
125 &pos->member != (head); \
126 pos = list_entry(pos->member.next, __typeof(*pos), member))
128 #define list_for_each_entry_continue_reverse(pos, head, member) \
129 for (pos = list_entry(pos->member.prev, __typeof(*pos), member); \
130 &pos->member != (head); \
131 pos = list_entry(pos->member.prev, __typeof(*pos), member))
134 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
135 * @pos: the type * to use as a loop cursor.
136 * @n: another type * to use as temporary storage
137 * @head: the head for your list.
138 * @member: the name of the list_struct within the struct.
140 #define list_for_each_entry_safe(pos, n, head, member) \
141 for (pos = list_entry((head)->next, __typeof(*pos), member), \
142 n = list_entry(pos->member.next, __typeof(*pos), member); \
143 &pos->member != (head); \
144 pos = n, n = list_entry(n->member.next, __typeof(*n), member))
146 #define list_for_each_entry_safe_from(pos, n, head, member) \
147 for (n = list_entry(pos->member.next, __typeof(*pos), member); \
148 &pos->member != (head); \
149 pos = n, n = list_entry(n->member.next, __typeof(*n), member))
151 #define list_first_entry(ptr, type, member) \
152 list_entry((ptr)->next, type, member)
156 __list_splice(const struct list_head *list, struct list_head *prev,
157 struct list_head *next)
159 struct list_head *first = list->next;
160 struct list_head *last = list->prev;
170 list_splice(const struct list_head *list, struct list_head *head)
172 if (list_empty(list))
175 __list_splice(list, head, head->next);
178 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
179 struct list_head *a, struct list_head *b));
181 /* hlist, copied from sys/dev/ofed/linux/list.h */
184 struct hlist_node *first;
188 struct hlist_node *next, **pprev;
191 #define HLIST_HEAD_INIT { }
192 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
193 #define INIT_HLIST_HEAD(head) (head)->first = NULL
194 #define INIT_HLIST_NODE(node) \
196 (node)->next = NULL; \
197 (node)->pprev = NULL; \
201 hlist_unhashed(const struct hlist_node *h)
208 hlist_empty(const struct hlist_head *h)
215 hlist_del(struct hlist_node *n)
219 n->next->pprev = n->pprev;
224 hlist_del_init(struct hlist_node *n)
227 if (hlist_unhashed(n))
234 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
239 h->first->pprev = &n->next;
241 n->pprev = &h->first;
245 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
248 n->pprev = next->pprev;
250 next->pprev = &n->next;
255 hlist_add_after(struct hlist_node *n, struct hlist_node *next)
258 next->next = n->next;
260 next->pprev = &n->next;
262 next->next->pprev = &next->next;
266 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
269 new->first = old->first;
271 new->first->pprev = &new->first;
275 #define hlist_entry(ptr, type, field) container_of(ptr, type, field)
277 #define hlist_for_each(p, head) \
278 for (p = (head)->first; p; p = p->next)
280 #define hlist_for_each_safe(p, n, head) \
281 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n)
283 #define hlist_for_each_entry(tp, p, head, field) \
284 for (p = (head)->first; \
285 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
287 #define hlist_for_each_entry_continue(tp, p, field) \
288 for (p = (p)->next; \
289 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
291 #define hlist_for_each_entry_from(tp, p, field) \
292 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next)
294 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
295 for (pos = (head)->first; \
296 (pos) != 0 && ({ n = (pos)->next; \
297 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \
300 #endif /* _DRM_LINUX_LIST_H_ */