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)
43 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
45 static __inline__ void
46 INIT_LIST_HEAD(struct list_head *head) {
51 #define LIST_HEAD_INIT(name) { &(name), &(name) }
53 #define DRM_LIST_HEAD(name) \
54 struct list_head name = LIST_HEAD_INIT(name)
57 list_empty(const struct list_head *head) {
58 return (head)->next == head;
61 static __inline__ void
62 list_add(struct list_head *new, struct list_head *head) {
63 (head)->next->prev = new;
64 (new)->next = (head)->next;
69 static __inline__ void
70 list_add_tail(struct list_head *entry, struct list_head *head) {
71 (entry)->prev = (head)->prev;
73 (head)->prev->next = entry;
77 static __inline__ void
78 list_del(struct list_head *entry) {
79 (entry)->next->prev = (entry)->prev;
80 (entry)->prev->next = (entry)->next;
83 static inline void list_replace(struct list_head *old,
84 struct list_head *new)
86 new->next = old->next;
87 new->next->prev = new;
88 new->prev = old->prev;
89 new->prev->next = new;
92 static inline void list_move(struct list_head *list, struct list_head *head)
98 static inline void list_move_tail(struct list_head *list,
99 struct list_head *head)
102 list_add_tail(list, head);
105 static __inline__ void
106 list_del_init(struct list_head *entry) {
107 (entry)->next->prev = (entry)->prev;
108 (entry)->prev->next = (entry)->next;
109 INIT_LIST_HEAD(entry);
112 #define list_for_each(entry, head) \
113 for (entry = (head)->next; entry != head; entry = (entry)->next)
115 #define list_for_each_prev(entry, head) \
116 for (entry = (head)->prev; entry != (head); \
119 #define list_for_each_safe(entry, temp, head) \
120 for (entry = (head)->next, temp = (entry)->next; \
122 entry = temp, temp = entry->next)
124 #define list_for_each_entry(pos, head, member) \
125 for (pos = list_entry((head)->next, __typeof(*pos), member); \
126 &pos->member != (head); \
127 pos = list_entry(pos->member.next, __typeof(*pos), member))
129 #define list_for_each_entry_continue_reverse(pos, head, member) \
130 for (pos = list_entry(pos->member.prev, __typeof(*pos), member); \
131 &pos->member != (head); \
132 pos = list_entry(pos->member.prev, __typeof(*pos), member))
135 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
136 * @pos: the type * to use as a loop cursor.
137 * @n: another type * to use as temporary storage
138 * @head: the head for your list.
139 * @member: the name of the list_struct within the struct.
141 #define list_for_each_entry_safe(pos, n, head, member) \
142 for (pos = list_entry((head)->next, __typeof(*pos), member), \
143 n = list_entry(pos->member.next, __typeof(*pos), member); \
144 &pos->member != (head); \
145 pos = n, n = list_entry(n->member.next, __typeof(*n), member))
147 #define list_for_each_entry_safe_from(pos, n, head, member) \
148 for (n = list_entry(pos->member.next, __typeof(*pos), member); \
149 &pos->member != (head); \
150 pos = n, n = list_entry(n->member.next, __typeof(*n), member))
152 #define list_first_entry(ptr, type, member) \
153 list_entry((ptr)->next, type, member)
157 __list_splice(const struct list_head *list, struct list_head *prev,
158 struct list_head *next)
160 struct list_head *first = list->next;
161 struct list_head *last = list->prev;
171 list_splice(const struct list_head *list, struct list_head *head)
173 if (list_empty(list))
176 __list_splice(list, head, head->next);
179 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
180 struct list_head *a, struct list_head *b));
182 #endif /* _DRM_LINUX_LIST_H_ */