1 /* drm_linux_list.h -- linux list functions for the BSDs. 2 * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org 3 */ 4 /*- 5 * Copyright 2003 Eric Anholt 6 * All Rights Reserved. 7 * 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: 14 * 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 17 * Software. 18 * 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. 26 * 27 * Authors: 28 * Eric Anholt <anholt@FreeBSD.org> 29 * 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef _DRM_LINUX_LIST_H_ 34 #define _DRM_LINUX_LIST_H_ 35 36 struct list_head { 37 struct list_head *next, *prev; 38 }; 39 40 #define list_entry(ptr, type, member) container_of(ptr,type,member) 41 42 static __inline__ void 43 INIT_LIST_HEAD(struct list_head *head) { 44 (head)->next = head; 45 (head)->prev = head; 46 } 47 48 #define LIST_HEAD_INIT(name) { &(name), &(name) } 49 50 #define DRM_LIST_HEAD(name) \ 51 struct list_head name = LIST_HEAD_INIT(name) 52 53 static __inline__ int 54 list_empty(const struct list_head *head) { 55 return (head)->next == head; 56 } 57 58 static __inline__ void 59 list_add(struct list_head *new, struct list_head *head) { 60 (head)->next->prev = new; 61 (new)->next = (head)->next; 62 (new)->prev = head; 63 (head)->next = new; 64 } 65 66 static __inline__ void 67 list_add_tail(struct list_head *entry, struct list_head *head) { 68 (entry)->prev = (head)->prev; 69 (entry)->next = head; 70 (head)->prev->next = entry; 71 (head)->prev = entry; 72 } 73 74 static __inline__ void 75 list_del(struct list_head *entry) { 76 (entry)->next->prev = (entry)->prev; 77 (entry)->prev->next = (entry)->next; 78 } 79 80 static inline void list_replace(struct list_head *old, 81 struct list_head *new) 82 { 83 new->next = old->next; 84 new->next->prev = new; 85 new->prev = old->prev; 86 new->prev->next = new; 87 } 88 89 static inline void list_move(struct list_head *list, struct list_head *head) 90 { 91 list_del(list); 92 list_add(list, head); 93 } 94 95 static inline void list_move_tail(struct list_head *list, 96 struct list_head *head) 97 { 98 list_del(list); 99 list_add_tail(list, head); 100 } 101 102 static __inline__ void 103 list_del_init(struct list_head *entry) { 104 (entry)->next->prev = (entry)->prev; 105 (entry)->prev->next = (entry)->next; 106 INIT_LIST_HEAD(entry); 107 } 108 109 #define list_for_each(entry, head) \ 110 for (entry = (head)->next; entry != head; entry = (entry)->next) 111 112 #define list_for_each_prev(entry, head) \ 113 for (entry = (head)->prev; entry != (head); \ 114 entry = entry->prev) 115 116 #define list_for_each_safe(entry, temp, head) \ 117 for (entry = (head)->next, temp = (entry)->next; \ 118 entry != head; \ 119 entry = temp, temp = entry->next) 120 121 #define list_for_each_entry(pos, head, member) \ 122 for (pos = list_entry((head)->next, __typeof(*pos), member); \ 123 &pos->member != (head); \ 124 pos = list_entry(pos->member.next, __typeof(*pos), member)) 125 126 #define list_for_each_entry_continue_reverse(pos, head, member) \ 127 for (pos = list_entry(pos->member.prev, __typeof(*pos), member); \ 128 &pos->member != (head); \ 129 pos = list_entry(pos->member.prev, __typeof(*pos), member)) 130 131 /** 132 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 133 * @pos: the type * to use as a loop cursor. 134 * @n: another type * to use as temporary storage 135 * @head: the head for your list. 136 * @member: the name of the list_struct within the struct. 137 */ 138 #define list_for_each_entry_safe(pos, n, head, member) \ 139 for (pos = list_entry((head)->next, __typeof(*pos), member), \ 140 n = list_entry(pos->member.next, __typeof(*pos), member); \ 141 &pos->member != (head); \ 142 pos = n, n = list_entry(n->member.next, __typeof(*n), member)) 143 144 #define list_for_each_entry_safe_from(pos, n, head, member) \ 145 for (n = list_entry(pos->member.next, __typeof(*pos), member); \ 146 &pos->member != (head); \ 147 pos = n, n = list_entry(n->member.next, __typeof(*n), member)) 148 149 #define list_first_entry(ptr, type, member) \ 150 list_entry((ptr)->next, type, member) 151 152 153 static inline void 154 __list_splice(const struct list_head *list, struct list_head *prev, 155 struct list_head *next) 156 { 157 struct list_head *first = list->next; 158 struct list_head *last = list->prev; 159 160 first->prev = prev; 161 prev->next = first; 162 163 last->next = next; 164 next->prev = last; 165 } 166 167 static inline void 168 list_splice(const struct list_head *list, struct list_head *head) 169 { 170 if (list_empty(list)) 171 return; 172 173 __list_splice(list, head, head->next); 174 } 175 176 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 177 struct list_head *a, struct list_head *b)); 178 179 /* hlist, copied from sys/dev/ofed/linux/list.h */ 180 181 struct hlist_head { 182 struct hlist_node *first; 183 }; 184 185 struct hlist_node { 186 struct hlist_node *next, **pprev; 187 }; 188 189 #define HLIST_HEAD_INIT { } 190 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 191 #define INIT_HLIST_HEAD(head) (head)->first = NULL 192 #define INIT_HLIST_NODE(node) \ 193 do { \ 194 (node)->next = NULL; \ 195 (node)->pprev = NULL; \ 196 } while (0) 197 198 static inline int 199 hlist_unhashed(const struct hlist_node *h) 200 { 201 202 return !h->pprev; 203 } 204 205 static inline int 206 hlist_empty(const struct hlist_head *h) 207 { 208 209 return !h->first; 210 } 211 212 static inline void 213 hlist_del(struct hlist_node *n) 214 { 215 216 if (n->next) 217 n->next->pprev = n->pprev; 218 *n->pprev = n->next; 219 } 220 221 static inline void 222 hlist_del_init(struct hlist_node *n) 223 { 224 225 if (hlist_unhashed(n)) 226 return; 227 hlist_del(n); 228 INIT_HLIST_NODE(n); 229 } 230 231 static inline void 232 hlist_add_head(struct hlist_node *n, struct hlist_head *h) 233 { 234 235 n->next = h->first; 236 if (h->first) 237 h->first->pprev = &n->next; 238 h->first = n; 239 n->pprev = &h->first; 240 } 241 242 static inline void 243 hlist_add_before(struct hlist_node *n, struct hlist_node *next) 244 { 245 246 n->pprev = next->pprev; 247 n->next = next; 248 next->pprev = &n->next; 249 *(n->pprev) = n; 250 } 251 252 static inline void 253 hlist_add_after(struct hlist_node *n, struct hlist_node *next) 254 { 255 256 next->next = n->next; 257 n->next = next; 258 next->pprev = &n->next; 259 if (next->next) 260 next->next->pprev = &next->next; 261 } 262 263 static inline void 264 hlist_move_list(struct hlist_head *old, struct hlist_head *new) 265 { 266 267 new->first = old->first; 268 if (new->first) 269 new->first->pprev = &new->first; 270 old->first = NULL; 271 } 272 273 #define hlist_entry(ptr, type, field) container_of(ptr, type, field) 274 275 #define hlist_for_each(p, head) \ 276 for (p = (head)->first; p; p = p->next) 277 278 #define hlist_for_each_safe(p, n, head) \ 279 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n) 280 281 #define hlist_for_each_entry(tp, p, head, field) \ 282 for (p = (head)->first; \ 283 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 284 285 #define hlist_for_each_entry_continue(tp, p, field) \ 286 for (p = (p)->next; \ 287 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 288 289 #define hlist_for_each_entry_from(tp, p, field) \ 290 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 291 292 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ 293 for (pos = (head)->first; \ 294 (pos) != 0 && ({ n = (pos)->next; \ 295 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \ 296 pos = (n)) 297 298 #endif /* _DRM_LINUX_LIST_H_ */ 299