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-2017 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 return (head->next == head); 97 } 98 99 static inline int 100 list_empty_careful(const struct list_head *head) 101 { 102 return (head == head->next) && (head == head->prev); 103 } 104 105 static inline void 106 list_del(struct list_head *entry) 107 { 108 109 entry->next->prev = entry->prev; 110 entry->prev->next = entry->next; 111 } 112 113 static inline void list_replace(struct list_head *old, 114 struct list_head *new) 115 { 116 new->next = old->next; 117 new->next->prev = new; 118 new->prev = old->prev; 119 new->prev->next = new; 120 } 121 122 static inline void list_replace_init(struct list_head *old, 123 struct list_head *new) 124 { 125 list_replace(old, new); 126 INIT_LIST_HEAD(old); 127 } 128 129 static inline void 130 _list_add(struct list_head *new, struct list_head *prev, 131 struct list_head *next) 132 { 133 134 next->prev = new; 135 new->next = next; 136 new->prev = prev; 137 prev->next = new; 138 } 139 140 static inline void 141 list_del_init(struct list_head *entry) 142 { 143 144 list_del(entry); 145 INIT_LIST_HEAD(entry); 146 } 147 148 #define list_entry(ptr, type, field) container_of(ptr, type, field) 149 150 #define list_first_entry(ptr, type, member) \ 151 list_entry((ptr)->next, type, member) 152 153 #define list_first_entry_or_null(ptr, type, member) \ 154 (list_empty(ptr) ? NULL: list_first_entry(ptr, type, member)) 155 156 #define list_last_entry(ptr, type, field) \ 157 list_entry(list_last((ptr)), type, field) 158 159 #define list_next_entry(ptr, member) \ 160 list_entry(((ptr)->member.next), typeof(*(ptr)), member) 161 162 #define list_for_each(p, head) \ 163 for (p = (head)->next; p != (head); p = p->next) 164 165 #define list_for_each_safe(p, n, head) \ 166 for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next) 167 168 #define list_for_each_entry(p, h, field) \ 169 for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \ 170 p = list_entry(p->field.next, typeof(*p), field)) 171 172 #define list_for_each_entry_safe(p, n, h, field) \ 173 for (p = list_entry((h)->next, typeof(*p), field), \ 174 n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\ 175 p = n, n = list_entry(n->field.next, typeof(*n), field)) 176 177 #define list_for_each_entry_continue(p, h, field) \ 178 for (p = list_next_entry((p), field); &p->field != (h); \ 179 p = list_next_entry((p), field)) 180 181 #define list_for_each_entry_safe_from(pos, n, head, member) \ 182 for (n = list_entry(pos->member.next, typeof(*pos), member); \ 183 &pos->member != (head); \ 184 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 185 186 #define list_for_each_entry_reverse(p, h, field) \ 187 for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \ 188 p = list_entry(p->field.prev, typeof(*p), field)) 189 190 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev) 191 192 static inline void 193 list_add(struct list_head *new, struct list_head *head) 194 { 195 196 _list_add(new, head, head->next); 197 } 198 199 static inline void 200 list_add_tail(struct list_head *new, struct list_head *head) 201 { 202 203 _list_add(new, head->prev, head); 204 } 205 206 static inline void 207 list_move(struct list_head *list, struct list_head *head) 208 { 209 210 list_del(list); 211 list_add(list, head); 212 } 213 214 static inline void 215 list_move_tail(struct list_head *entry, struct list_head *head) 216 { 217 218 list_del(entry); 219 list_add_tail(entry, head); 220 } 221 222 static inline void 223 _list_splice(const struct list_head *list, struct list_head *prev, 224 struct list_head *next) 225 { 226 struct list_head *first; 227 struct list_head *last; 228 229 if (list_empty(list)) 230 return; 231 first = list->next; 232 last = list->prev; 233 first->prev = prev; 234 prev->next = first; 235 last->next = next; 236 next->prev = last; 237 } 238 239 static inline void 240 list_splice(const struct list_head *list, struct list_head *head) 241 { 242 243 _list_splice(list, head, head->next); 244 } 245 246 static inline void 247 list_splice_tail(struct list_head *list, struct list_head *head) 248 { 249 250 _list_splice(list, head->prev, head); 251 } 252 253 static inline void 254 list_splice_init(struct list_head *list, struct list_head *head) 255 { 256 257 _list_splice(list, head, head->next); 258 INIT_LIST_HEAD(list); 259 } 260 261 static inline void 262 list_splice_tail_init(struct list_head *list, struct list_head *head) 263 { 264 265 _list_splice(list, head->prev, head); 266 INIT_LIST_HEAD(list); 267 } 268 269 #define LINUX_LIST_HEAD(name) struct list_head name = { &(name), &(name) } 270 271 272 struct hlist_head { 273 struct hlist_node *first; 274 }; 275 276 struct hlist_node { 277 struct hlist_node *next, **pprev; 278 }; 279 280 #define HLIST_HEAD_INIT { } 281 #define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 282 #define INIT_HLIST_HEAD(head) (head)->first = NULL 283 #define INIT_HLIST_NODE(node) \ 284 do { \ 285 (node)->next = NULL; \ 286 (node)->pprev = NULL; \ 287 } while (0) 288 289 static inline int 290 hlist_unhashed(const struct hlist_node *h) 291 { 292 293 return !h->pprev; 294 } 295 296 static inline int 297 hlist_empty(const struct hlist_head *h) 298 { 299 300 return !h->first; 301 } 302 303 static inline void 304 hlist_del(struct hlist_node *n) 305 { 306 307 if (n->next) 308 n->next->pprev = n->pprev; 309 *n->pprev = n->next; 310 } 311 312 static inline void 313 hlist_del_init(struct hlist_node *n) 314 { 315 316 if (hlist_unhashed(n)) 317 return; 318 hlist_del(n); 319 INIT_HLIST_NODE(n); 320 } 321 322 static inline void 323 hlist_add_head(struct hlist_node *n, struct hlist_head *h) 324 { 325 326 n->next = h->first; 327 if (h->first) 328 h->first->pprev = &n->next; 329 h->first = n; 330 n->pprev = &h->first; 331 } 332 333 static inline void 334 hlist_add_before(struct hlist_node *n, struct hlist_node *next) 335 { 336 337 n->pprev = next->pprev; 338 n->next = next; 339 next->pprev = &n->next; 340 *(n->pprev) = n; 341 } 342 343 static inline void 344 hlist_add_after(struct hlist_node *n, struct hlist_node *next) 345 { 346 347 next->next = n->next; 348 n->next = next; 349 next->pprev = &n->next; 350 if (next->next) 351 next->next->pprev = &next->next; 352 } 353 354 static inline void 355 hlist_move_list(struct hlist_head *old, struct hlist_head *new) 356 { 357 358 new->first = old->first; 359 if (new->first) 360 new->first->pprev = &new->first; 361 old->first = NULL; 362 } 363 364 /** 365 * list_is_singular - tests whether a list has just one entry. 366 * @head: the list to test. 367 */ 368 static inline int list_is_singular(const struct list_head *head) 369 { 370 return !list_empty(head) && (head->next == head->prev); 371 } 372 373 static inline void __list_cut_position(struct list_head *list, 374 struct list_head *head, struct list_head *entry) 375 { 376 struct list_head *new_first = entry->next; 377 list->next = head->next; 378 list->next->prev = list; 379 list->prev = entry; 380 entry->next = list; 381 head->next = new_first; 382 new_first->prev = head; 383 } 384 385 /** 386 * list_cut_position - cut a list into two 387 * @list: a new list to add all removed entries 388 * @head: a list with entries 389 * @entry: an entry within head, could be the head itself 390 * and if so we won't cut the list 391 * 392 * This helper moves the initial part of @head, up to and 393 * including @entry, from @head to @list. You should 394 * pass on @entry an element you know is on @head. @list 395 * should be an empty list or a list you do not care about 396 * losing its data. 397 * 398 */ 399 static inline void list_cut_position(struct list_head *list, 400 struct list_head *head, struct list_head *entry) 401 { 402 if (list_empty(head)) 403 return; 404 if (list_is_singular(head) && 405 (head->next != entry && head != entry)) 406 return; 407 if (entry == head) 408 INIT_LIST_HEAD(list); 409 else 410 __list_cut_position(list, head, entry); 411 } 412 413 /** 414 * list_is_last - tests whether @list is the last entry in list @head 415 * @list: the entry to test 416 * @head: the head of the list 417 */ 418 static inline int list_is_last(const struct list_head *list, 419 const struct list_head *head) 420 { 421 return list->next == head; 422 } 423 424 #define hlist_entry(ptr, type, field) container_of(ptr, type, field) 425 426 #define hlist_for_each(p, head) \ 427 for (p = (head)->first; p; p = p->next) 428 429 #define hlist_for_each_safe(p, n, head) \ 430 for (p = (head)->first; p && ({ n = p->next; 1; }); p = n) 431 432 #define hlist_entry_safe(ptr, type, member) \ 433 (ptr) ? hlist_entry(ptr, type, member) : NULL 434 435 #define hlist_for_each_entry(pos, head, member) \ 436 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 437 pos; \ 438 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 439 440 #define hlist_for_each_entry_continue(tp, p, field) \ 441 for (p = (p)->next; \ 442 p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 443 444 #define hlist_for_each_entry_from(tp, p, field) \ 445 for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 446 447 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ 448 for (pos = (head)->first; \ 449 (pos) != 0 && ({ n = (pos)->next; \ 450 tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \ 451 pos = (n)) 452 453 void drm_list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv, 454 struct list_head *a, struct list_head *b)); 455 456 #define hlist_add_head_rcu(n, h) hlist_add_head(n, h) 457 458 #define hlist_del_init_rcu(n) hlist_del_init(n) 459 460 #endif /* _LINUX_LIST_H_ */ 461