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>
31 #ifndef _DRM_LINUX_LIST_
32 #define _DRM_LINUX_LIST_
35 struct list_head
*next
, *prev
;
38 #define list_entry(ptr, type, member) \
39 container_of(ptr, type, member)
41 static __inline__
void
42 INIT_LIST_HEAD(struct list_head
*head
) {
47 #define LIST_HEAD(name) struct list_head name = {&name, &name}
50 list_empty(struct list_head
*head
) {
51 return (head
)->next
== head
;
55 list_is_singular(struct list_head
*head
) {
56 return !list_empty(head
) && ((head
)->next
== (head
->prev
));
59 /* Insert just after head (at start of list) */
60 static __inline__
void
61 list_add(struct list_head
*entry
, struct list_head
*head
) {
62 (head
)->next
->prev
= entry
;
63 (entry
)->next
= (head
)->next
;
68 /* Insert just before head (at end of list) */
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
84 list_replace(struct list_head
*old
, struct list_head
*head
) {
85 (head
)->next
= (old
)->next
;
86 (head
)->next
->prev
= head
;
87 (head
)->prev
= (old
)->prev
;
88 (head
)->prev
->next
= head
;
92 #define list_for_each(entry, head) \
93 for (entry = (head)->next; entry != head; entry = (entry)->next)
95 #define list_for_each_prev(entry, head) \
96 for (entry = (head)->prev; entry != (head); \
99 #define list_for_each_safe(entry, temp, head) \
100 for (entry = (head)->next, temp = (entry)->next; \
102 entry = temp, temp = entry->next)
104 #define list_for_each_entry(pos, head, member) \
105 for (pos = list_entry((head)->next, typeof(*pos), member); \
106 &pos->member != (head); \
107 pos = list_entry(pos->member.next, typeof(*pos), member))
109 #define list_for_each_entry_safe(pos, n, head, member) \
110 for (pos = list_entry((head)->next, typeof(*pos), member), \
111 n = list_entry(pos->member.next, typeof(*pos), member); \
112 &pos->member != (head); \
113 pos = n, n = list_entry(n->member.next, typeof(*n), member))
115 static inline void list_del_init(struct list_head
*entry
)
118 INIT_LIST_HEAD(entry
);
121 #define list_first_entry(ptr, type, member) \
122 list_entry((ptr)->next, type, member)
124 static inline void list_move_tail(struct list_head
*list
,
125 struct list_head
*head
)
128 list_add_tail(list
, head
);
131 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
134 list_add(list
, head
);
137 static inline void list_sort(void * priv
, struct list_head
*head
,
138 int (*cmp
)(void * priv
, struct list_head
*a
, struct list_head
*b
))
142 if (list_empty(head
))
146 struct list_head
* elem
= head
->next
;
149 while(elem
->next
!= head
)
151 if (cmp(priv
, elem
, elem
->next
) < 0)
153 /* Need to switch both elements */
154 struct list_head
* elemnext
= elem
->next
;
155 elem
->prev
->next
= elemnext
;
156 elemnext
->prev
= elem
->prev
;
157 elem
->prev
= elemnext
;
158 elem
->next
= elemnext
->next
;
159 elemnext
->next
->prev
= elem
;
160 elemnext
->next
= elem
;
169 #endif /* _DRM_LINUX_LIST_ */