1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 6 * Copyright (c) 2015 François Tigeot 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 #ifndef _LINUX_LIST_H_ 31 #define _LINUX_LIST_H_ 32 33 /* 34 * Since LIST_HEAD conflicts with the linux definition we must include any 35 * FreeBSD header which requires it here so it is resolved with the correct 36 * definition prior to the undef. 37 */ 38 #include <linux/types.h> 39 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/queue.h> 43 #include <sys/jail.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/proc.h> 47 #include <sys/conf.h> 48 #include <sys/socket.h> 49 #include <sys/mbuf.h> 50 51 #include <net/bpf.h> 52 #include <net/if.h> 53 #include <net/if_var.h> 54 #include <net/if_types.h> 55 #include <net/if_media.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_pcb.h> 59 #include <netinet/in_var.h> 60 61 #include <netinet6/in6_var.h> 62 #include <netinet6/nd6.h> 63 64 #include <vm/vm.h> 65 #include <vm/vm_object.h> 66 67 #define prefetch(x) 68 69 struct list_head { 70 struct list_head *next; 71 struct list_head *prev; 72 }; 73 74 static inline void 75 INIT_LIST_HEAD(struct list_head *list) 76 { 77 78 list->next = list->prev = list; 79 } 80 81 static inline struct list_head * 82 list_first(const struct list_head *head) 83 { 84 return head->next; 85 } 86 87 static inline struct list_head * 88 list_last(const struct list_head *head) 89 { 90 return head->prev; 91 } 92 93 static inline int 94 list_empty(const struct list_head *head) 95 { 96 97 return (head->next == head); 98 } 99 100 static inline void 101 list_del(struct list_head *entry) 102 { 103 104 entry->next->prev = entry->prev; 105 entry->prev->next = entry->next; 106 } 107 108 static inline void list_replace(struct list_head *old, 109 struct list_head *new) 110 { 111 new->next = old->next; 112 new->next->prev = new; 113 new->prev = old->prev; 114 new->prev->next = new; 115 } 116 117 static inline void list_replace_init(struct list_head *old, 118 struct list_head *new) 119 { 120 list_replace(old, new); 121 INIT_LIST_HEAD(old); 122 } 123 124 static inline void 125 _list_add(struct list_head *new, struct list_head *prev, 126 struct list_head *next) 127 { 128 129 next->prev = new; 130 new->next = next; 131 new->prev = prev; 132 prev->next = new; 133 } 134 135 static inline void 136 list_del_init(struct list_head *entry) 137 { 138 139 list_del(entry); 140 INIT_LIST_HEAD(entry); 141 } 142 143 #define list_entry(ptr, type, field) container_of(ptr, type, field) 144 145 #define list_first_entry(ptr, type, member) \ 146 list_entry((ptr)->next, type, member) 147 148 #define list_first_entry_or_null(ptr, type, member) \ 149 (list_empty(ptr) ? NULL: list_first_entry(ptr, type, member)) 150 151 #define list_last_entry(ptr, type, field) \ 152 list_entry(list_last((ptr)), type, field) 153 154 #define list_next_entry(ptr, member) \ 155 list_entry(((ptr)->member.next), typeof(*(ptr)), member) 156 157 #define list_for_each(p, head) \ 158 for (p = (head)->next; p != (head); p = p->next) 159 160 #define list_for_each_safe(p, n, head) \ 161 for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next) 162 163 #define list_for_each_entry(p, h, field) \ 164 for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \ 165 p = list_entry(p->field.next, typeof(*p), field)) 166 167 #define list_for_each_entry_safe(p, n, h, field) \ 168 for (p = list_entry((h)->next, typeof(*p), field), \ 169 n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\ 170 p = n, n = list_entry(n->field.next, typeof(*n), field)) 171 172 #define list_for_each_entry_continue(p, h, field) \ 173 for (p = list_next_entry((p), field); &p->field != (h); \ 174 p = list_next_entry((p), field)) 175 176 #define list_for_each_entry_safe_from(pos, n, head, member) \ 177 for (n = list_entry(pos->member.next, typeof(*pos), member); \ 178 &pos->member != (head); \ 179 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 180 181 #define list_for_each_entry_reverse(p, h, field) \ 182 for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \ 183 p = list_entry(p->field.prev, typeof(*p), field)) 184 185 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev) 186 187 static inline void 188 list_add(struct list_head *new, struct list_head *head) 189 { 190 191 _list_add(new, head, head->next); 192 } 193 194 static inline void 195 list_add_tail(struct list_head *new, struct list_head *head) 196 { 197 198 _list_add(new, head->prev, head); 199 } 200 201 static inline void 202 list_move(struct list_head *list, struct list_head *head) 203 { 204 205 list_del(list); 206 list_add(list, head); 207 } 208 209 static inline void 210 list_move_tail(struct list_head *entry, struct list_head *head) 211 { 212 213 list_del(entry); 214 list_add_tail(entry, head); 215 } 216 217 static inline void 218 _list_splice(const struct list_head *list, struct list_head *prev, 219 struct list_head *next) 220 { 221 struct list_head *first; 222 struct list_head *last; 223 224 if (list_empty(list)) 225 return; 226 first = list->next; 227 last = list->prev; 228 first->prev = prev; 229 prev->next = first; 230 last->next = next; 231 next->prev = last; 232 } 233 234 static inline void 235 list_splice(const struct list_head *list, struct list_head *head) 236 { 237 238 _list_splice(list, head, head->next); 239 } 240 241 static inline void 242 list_splice_tail(struct list_head *list, struct list_head *head) 243 { 244 245 _list_splice(list, head->prev, head); 246 } 247 248 static inline void 249 list_splice_init(struct list_head *list, struct list_head *head) 250 { 251 252 _list_splice(list, head, head->next); 253 INIT_LIST_HEAD(list); 254 } 255 256 static inline void 257 list_splice_tail_init(struct list_head *list, struct list_head *head) 258 { 259 260 _list_splice(list, head->prev, head); 261 INIT_LIST_HEAD(list); 262 } 263 264 #define LINUX_LIST_HEAD(name) struct list_head name = { &(name), &(name) } 265 266 267 struct hlist_head { 268 struct hlist_node *first; 269 }; 270 271 struct hlist_node { 272 struct hlist_node *next, **pprev; 273 }; 274 275 #define HLIST_HEAD_INIT { } 276 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 277 #define INIT_HLIST_HEAD(head) (head)->first = NULL 278 #define INIT_HLIST_NODE(node) \ 279 do { \ 280 (node)->next = NULL; \ 281 (node)->pprev = NULL; \ 282 } while (0) 283 284 static inline int 285 hlist_unhashed(const struct hlist_node *h) 286 { 287 288 return !h->pprev; 289 } 290 291 static inline int 292 hlist_empty(const struct hlist_head *h) 293 { 294 295 return !h->first; 296 } 297 298 static inline void 299 hlist_del(struct hlist_node *n) 300 { 301 302 if (n->next) 303 n->next->pprev = n->pprev; 304 *n->pprev = n->next; 305 } 306 307 static inline void 308 hlist_del_init(struct hlist_node *n) 309 { 310 311 if (hlist_unhashed(n)) 312 return; 313 hlist_del(n); 314 INIT_HLIST_NODE(n); 315 } 316 317 static inline void 318 hlist_add_head(struct hlist_node *n, struct hlist_head *h) 319 { 320 321 n->next = h->first; 322 if (h->first) 323 h->first->pprev = &n->next; 324 h->first = n; 325 n->pprev = &h->first; 326 } 327 328 static inline void 329 hlist_add_before(struct hlist_node *n, struct hlist_node *next) 330 { 331 332 n->pprev = next->pprev; 333 n->next = next; 334 next->pprev = &n->next; 335 *(n->pprev) = n; 336 } 337 338 static inline void 339 hlist_add_after(struct hlist_node *n, struct hlist_node *next) 340 { 341 342 next->next = n->next; 343 n->next = next; 344 next->pprev = &n->next; 345 if (next->next) 346 next->next->pprev = &next->next; 347 } 348 349 static inline void 350 hlist_move_list(struct hlist_head *old, struct hlist_head *new) 351 { 352 353 new->first = old->first; 354 if (new->first) 355 new->first->pprev = &new->first; 356 old->first = NULL; 357 } 358 359 /** 360 * list_is_singular - tests whether a list has just one entry. 361 * @head: the list to test. 362 */ 363 static inline int list_is_singular(const struct list_head *head) 364 { 365 return !list_empty(head) && (head->next == head->prev); 366 } 367 368 static inline void __list_cut_position(struct list_head *list, 369 struct list_head *head, struct list_head *entry) 370 { 371 struct list_head *new_first = entry->next; 372 list->next = head->next; 373 list->next->prev = list; 374 list->prev = entry; 375 entry->next = list; 376 head->next = new_first; 377 new_first->prev = head; 378 } 379 380 /** 381 * list_cut_position - cut a list into two 382 * @list: a new list to add all removed entries 383 * @head: a list with entries 384 * @entry: an entry within head, could be the head itself 385 * and if so we won't cut the list 386 * 387 * This helper moves the initial part of @head, up to and 388 * including @entry, from @head to @list. You should 389 * pass on @entry an element you know is on @head. @list 390 * should be an empty list or a list you do not care about 391 * losing its data. 392 * 393 */ 394 static inline void list_cut_position(struct list_head *list, 395 struct list_head *head, struct list_head *entry) 396 { 397 if (list_empty(head)) 398 return; 399 if (list_is_singular(head) && 400 (head->next != entry && head != entry)) 401 return; 402 if (entry == head) 403 INIT_LIST_HEAD(list); 404 else 405 __list_cut_position(list, head, entry); 406 } 407 408 /** 409 * list_is_last - tests whether @list is the last entry in list @head 410 * @list: the entry to test 411 * @head: the head of the list 412 */ 413 static inline int list_is_last(const struct list_head *list, 414 const struct list_head *head) 415 { 416 return list->next == head; 417 } 418 419 #define hlist_entry(ptr, type, field) container_of(ptr, type, field) 420 421 #define hlist_for_each(p, head) \ 422 for (p = (head)->first; p; p = p->next) 423 424 #define hlist_for_each_safe(p, n, head) \ 425 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n) 426 427 #define hlist_entry_safe(ptr, type, member) \ 428 (ptr) ? hlist_entry(ptr, type, member) : NULL 429 430 #define hlist_for_each_entry(pos, head, member) \ 431 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 432 pos; \ 433 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 434 435 #define hlist_for_each_entry_continue(tp, p, field) \ 436 for (p = (p)->next; \ 437 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 438 439 #define hlist_for_each_entry_from(tp, p, field) \ 440 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 441 442 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ 443 for (pos = (head)->first; \ 444 (pos) != 0 && ({ n = (pos)->next; \ 445 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \ 446 pos = (n)) 447 448 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 449 struct list_head *a, struct list_head *b)); 450 451 #define hlist_add_head_rcu(n, h) hlist_add_head(n, h) 452 453 #define hlist_del_init_rcu(n) hlist_del_init(n) 454 455 #endif /* _LINUX_LIST_H_ */ 456