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