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