list.h revision 289644
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc. 3219820Sjeff * Copyright (c) 2010 iX Systems, Inc. 4219820Sjeff * Copyright (c) 2010 Panasas, Inc. 5270710Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 6219820Sjeff * All rights reserved. 7219820Sjeff * 8219820Sjeff * Redistribution and use in source and binary forms, with or without 9219820Sjeff * modification, are permitted provided that the following conditions 10219820Sjeff * are met: 11219820Sjeff * 1. Redistributions of source code must retain the above copyright 12219820Sjeff * notice unmodified, this list of conditions, and the following 13219820Sjeff * disclaimer. 14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 15219820Sjeff * notice, this list of conditions and the following disclaimer in the 16219820Sjeff * documentation and/or other materials provided with the distribution. 17219820Sjeff * 18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28289644Shselasky * 29289644Shselasky * $FreeBSD: head/sys/ofed/include/linux/list.h 289644 2015-10-20 19:08:26Z hselasky $ 30219820Sjeff */ 31219820Sjeff#ifndef _LINUX_LIST_H_ 32219820Sjeff#define _LINUX_LIST_H_ 33219820Sjeff 34219820Sjeff/* 35219820Sjeff * Since LIST_HEAD conflicts with the linux definition we must include any 36219820Sjeff * FreeBSD header which requires it here so it is resolved with the correct 37219820Sjeff * definition prior to the undef. 38219820Sjeff */ 39219820Sjeff#include <linux/types.h> 40219820Sjeff 41219820Sjeff#include <sys/param.h> 42219820Sjeff#include <sys/kernel.h> 43219820Sjeff#include <sys/queue.h> 44222813Sattilio#include <sys/cpuset.h> 45275599Srodrigc#include <sys/jail.h> 46219820Sjeff#include <sys/lock.h> 47219820Sjeff#include <sys/mutex.h> 48219820Sjeff#include <sys/proc.h> 49219820Sjeff#include <sys/vnode.h> 50219820Sjeff#include <sys/conf.h> 51219820Sjeff#include <sys/socket.h> 52219820Sjeff#include <sys/mbuf.h> 53219820Sjeff 54219820Sjeff#include <net/bpf.h> 55219820Sjeff#include <net/if.h> 56257176Sglebius#include <net/if_var.h> 57219820Sjeff#include <net/if_types.h> 58219820Sjeff#include <net/if_media.h> 59275599Srodrigc#include <net/vnet.h> 60219820Sjeff 61219820Sjeff#include <netinet/in.h> 62219820Sjeff#include <netinet/in_pcb.h> 63276749Shselasky#include <netinet/in_var.h> 64219820Sjeff 65219820Sjeff#include <netinet6/in6_var.h> 66219820Sjeff#include <netinet6/nd6.h> 67219820Sjeff 68219820Sjeff#include <vm/vm.h> 69219820Sjeff#include <vm/vm_object.h> 70219820Sjeff 71219820Sjeff#define prefetch(x) 72219820Sjeff 73219820Sjeffstruct list_head { 74219820Sjeff struct list_head *next; 75219820Sjeff struct list_head *prev; 76219820Sjeff}; 77219820Sjeff 78219820Sjeffstatic inline void 79219820SjeffINIT_LIST_HEAD(struct list_head *list) 80219820Sjeff{ 81219820Sjeff 82219820Sjeff list->next = list->prev = list; 83219820Sjeff} 84219820Sjeff 85219820Sjeffstatic inline int 86219820Sjefflist_empty(const struct list_head *head) 87219820Sjeff{ 88219820Sjeff 89219820Sjeff return (head->next == head); 90219820Sjeff} 91219820Sjeff 92219820Sjeffstatic inline void 93219820Sjefflist_del(struct list_head *entry) 94219820Sjeff{ 95219820Sjeff 96219820Sjeff entry->next->prev = entry->prev; 97219820Sjeff entry->prev->next = entry->next; 98219820Sjeff} 99219820Sjeff 100219820Sjeffstatic inline void 101289574Shselaskylist_replace(struct list_head *old, struct list_head *new) 102289574Shselasky{ 103289574Shselasky new->next = old->next; 104289574Shselasky new->next->prev = new; 105289574Shselasky new->prev = old->prev; 106289574Shselasky new->prev->next = new; 107289574Shselasky} 108289574Shselasky 109289574Shselaskystatic inline void 110219820Sjeff_list_add(struct list_head *new, struct list_head *prev, 111219820Sjeff struct list_head *next) 112219820Sjeff{ 113219820Sjeff 114219820Sjeff next->prev = new; 115219820Sjeff new->next = next; 116219820Sjeff new->prev = prev; 117219820Sjeff prev->next = new; 118219820Sjeff} 119219820Sjeff 120219820Sjeffstatic inline void 121219820Sjefflist_del_init(struct list_head *entry) 122219820Sjeff{ 123219820Sjeff 124219820Sjeff list_del(entry); 125219820Sjeff INIT_LIST_HEAD(entry); 126219820Sjeff} 127219820Sjeff 128219820Sjeff#define list_entry(ptr, type, field) container_of(ptr, type, field) 129219820Sjeff 130255932Salfred#define list_first_entry(ptr, type, member) \ 131255932Salfred list_entry((ptr)->next, type, member) 132255932Salfred 133289574Shselasky#define list_next_entry(ptr, member) \ 134289574Shselasky list_entry(((ptr)->member.next), typeof(*(ptr)), member) 135289574Shselasky 136219820Sjeff#define list_for_each(p, head) \ 137219820Sjeff for (p = (head)->next; p != (head); p = p->next) 138219820Sjeff 139219820Sjeff#define list_for_each_safe(p, n, head) \ 140219820Sjeff for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next) 141219820Sjeff 142219820Sjeff#define list_for_each_entry(p, h, field) \ 143219820Sjeff for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \ 144219820Sjeff p = list_entry(p->field.next, typeof(*p), field)) 145219820Sjeff 146219820Sjeff#define list_for_each_entry_safe(p, n, h, field) \ 147219820Sjeff for (p = list_entry((h)->next, typeof(*p), field), \ 148219820Sjeff n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\ 149219820Sjeff p = n, n = list_entry(n->field.next, typeof(*n), field)) 150219820Sjeff 151289574Shselasky#define list_for_each_entry_continue(p, h, field) \ 152289574Shselasky for (p = list_next_entry((p), field); &p->field != (h); \ 153289574Shselasky p = list_next_entry((p), field)) 154289574Shselasky 155289574Shselasky#define list_for_each_entry_safe_from(pos, n, head, member) \ 156289574Shselasky for (n = list_entry(pos->member.next, typeof(*pos), member); \ 157289574Shselasky &pos->member != (head); \ 158289574Shselasky pos = n, n = list_entry(n->member.next, typeof(*n), member)) 159289574Shselasky 160219820Sjeff#define list_for_each_entry_reverse(p, h, field) \ 161219820Sjeff for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \ 162219820Sjeff p = list_entry(p->field.prev, typeof(*p), field)) 163219820Sjeff 164219820Sjeff#define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev) 165219820Sjeff 166219820Sjeffstatic inline void 167219820Sjefflist_add(struct list_head *new, struct list_head *head) 168219820Sjeff{ 169219820Sjeff 170219820Sjeff _list_add(new, head, head->next); 171219820Sjeff} 172219820Sjeff 173219820Sjeffstatic inline void 174219820Sjefflist_add_tail(struct list_head *new, struct list_head *head) 175219820Sjeff{ 176219820Sjeff 177219820Sjeff _list_add(new, head->prev, head); 178219820Sjeff} 179219820Sjeff 180219820Sjeffstatic inline void 181219820Sjefflist_move(struct list_head *list, struct list_head *head) 182219820Sjeff{ 183219820Sjeff 184219820Sjeff list_del(list); 185219820Sjeff list_add(list, head); 186219820Sjeff} 187219820Sjeff 188219820Sjeffstatic inline void 189219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head) 190219820Sjeff{ 191219820Sjeff 192219820Sjeff list_del(entry); 193219820Sjeff list_add_tail(entry, head); 194219820Sjeff} 195219820Sjeff 196219820Sjeffstatic inline void 197219820Sjeff_list_splice(const struct list_head *list, struct list_head *prev, 198219820Sjeff struct list_head *next) 199219820Sjeff{ 200219820Sjeff struct list_head *first; 201219820Sjeff struct list_head *last; 202219820Sjeff 203219820Sjeff if (list_empty(list)) 204219820Sjeff return; 205219820Sjeff first = list->next; 206219820Sjeff last = list->prev; 207219820Sjeff first->prev = prev; 208219820Sjeff prev->next = first; 209219820Sjeff last->next = next; 210219820Sjeff next->prev = last; 211219820Sjeff} 212219820Sjeff 213219820Sjeffstatic inline void 214219820Sjefflist_splice(const struct list_head *list, struct list_head *head) 215219820Sjeff{ 216219820Sjeff 217219820Sjeff _list_splice(list, head, head->next); 218219820Sjeff} 219219820Sjeff 220219820Sjeffstatic inline void 221219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head) 222219820Sjeff{ 223219820Sjeff 224219820Sjeff _list_splice(list, head->prev, head); 225219820Sjeff} 226219820Sjeff 227219820Sjeffstatic inline void 228219820Sjefflist_splice_init(struct list_head *list, struct list_head *head) 229219820Sjeff{ 230219820Sjeff 231219820Sjeff _list_splice(list, head, head->next); 232219820Sjeff INIT_LIST_HEAD(list); 233219820Sjeff} 234219820Sjeff 235219820Sjeffstatic inline void 236219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head) 237219820Sjeff{ 238219820Sjeff 239219820Sjeff _list_splice(list, head->prev, head); 240219820Sjeff INIT_LIST_HEAD(list); 241219820Sjeff} 242219820Sjeff 243219820Sjeff#undef LIST_HEAD 244219820Sjeff#define LIST_HEAD(name) struct list_head name = { &(name), &(name) } 245219820Sjeff 246219820Sjeff 247219820Sjeffstruct hlist_head { 248219820Sjeff struct hlist_node *first; 249219820Sjeff}; 250219820Sjeff 251219820Sjeffstruct hlist_node { 252219820Sjeff struct hlist_node *next, **pprev; 253219820Sjeff}; 254219820Sjeff 255219820Sjeff#define HLIST_HEAD_INIT { } 256219820Sjeff#define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 257219820Sjeff#define INIT_HLIST_HEAD(head) (head)->first = NULL 258219820Sjeff#define INIT_HLIST_NODE(node) \ 259219820Sjeffdo { \ 260219820Sjeff (node)->next = NULL; \ 261219820Sjeff (node)->pprev = NULL; \ 262219820Sjeff} while (0) 263219820Sjeff 264219820Sjeffstatic inline int 265219820Sjeffhlist_unhashed(const struct hlist_node *h) 266219820Sjeff{ 267219820Sjeff 268219820Sjeff return !h->pprev; 269219820Sjeff} 270219820Sjeff 271219820Sjeffstatic inline int 272219820Sjeffhlist_empty(const struct hlist_head *h) 273219820Sjeff{ 274219820Sjeff 275219820Sjeff return !h->first; 276219820Sjeff} 277219820Sjeff 278219820Sjeffstatic inline void 279219820Sjeffhlist_del(struct hlist_node *n) 280219820Sjeff{ 281219820Sjeff 282219820Sjeff if (n->next) 283219820Sjeff n->next->pprev = n->pprev; 284219820Sjeff *n->pprev = n->next; 285219820Sjeff} 286219820Sjeff 287219820Sjeffstatic inline void 288219820Sjeffhlist_del_init(struct hlist_node *n) 289219820Sjeff{ 290219820Sjeff 291219820Sjeff if (hlist_unhashed(n)) 292219820Sjeff return; 293219820Sjeff hlist_del(n); 294219820Sjeff INIT_HLIST_NODE(n); 295219820Sjeff} 296219820Sjeff 297219820Sjeffstatic inline void 298219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h) 299219820Sjeff{ 300219820Sjeff 301219820Sjeff n->next = h->first; 302219820Sjeff if (h->first) 303219820Sjeff h->first->pprev = &n->next; 304219820Sjeff h->first = n; 305219820Sjeff n->pprev = &h->first; 306219820Sjeff} 307219820Sjeff 308219820Sjeffstatic inline void 309219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next) 310219820Sjeff{ 311219820Sjeff 312219820Sjeff n->pprev = next->pprev; 313219820Sjeff n->next = next; 314219820Sjeff next->pprev = &n->next; 315219820Sjeff *(n->pprev) = n; 316219820Sjeff} 317219820Sjeff 318219820Sjeffstatic inline void 319219820Sjeffhlist_add_after(struct hlist_node *n, struct hlist_node *next) 320219820Sjeff{ 321219820Sjeff 322219820Sjeff next->next = n->next; 323219820Sjeff n->next = next; 324219820Sjeff next->pprev = &n->next; 325219820Sjeff if (next->next) 326219820Sjeff next->next->pprev = &next->next; 327219820Sjeff} 328219820Sjeff 329219820Sjeffstatic inline void 330219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new) 331219820Sjeff{ 332219820Sjeff 333219820Sjeff new->first = old->first; 334219820Sjeff if (new->first) 335219820Sjeff new->first->pprev = &new->first; 336219820Sjeff old->first = NULL; 337219820Sjeff} 338270710Shselasky 339270710Shselasky/** 340270710Shselasky * list_is_singular - tests whether a list has just one entry. 341270710Shselasky * @head: the list to test. 342270710Shselasky */ 343270710Shselaskystatic inline int list_is_singular(const struct list_head *head) 344270710Shselasky{ 345270710Shselasky return !list_empty(head) && (head->next == head->prev); 346270710Shselasky} 347270710Shselasky 348270710Shselaskystatic inline void __list_cut_position(struct list_head *list, 349270710Shselasky struct list_head *head, struct list_head *entry) 350270710Shselasky{ 351270710Shselasky struct list_head *new_first = entry->next; 352270710Shselasky list->next = head->next; 353270710Shselasky list->next->prev = list; 354270710Shselasky list->prev = entry; 355270710Shselasky entry->next = list; 356270710Shselasky head->next = new_first; 357270710Shselasky new_first->prev = head; 358270710Shselasky} 359270710Shselasky 360270710Shselasky/** 361270710Shselasky * list_cut_position - cut a list into two 362270710Shselasky * @list: a new list to add all removed entries 363270710Shselasky * @head: a list with entries 364270710Shselasky * @entry: an entry within head, could be the head itself 365270710Shselasky * and if so we won't cut the list 366270710Shselasky * 367270710Shselasky * This helper moves the initial part of @head, up to and 368270710Shselasky * including @entry, from @head to @list. You should 369270710Shselasky * pass on @entry an element you know is on @head. @list 370270710Shselasky * should be an empty list or a list you do not care about 371270710Shselasky * losing its data. 372270710Shselasky * 373270710Shselasky */ 374270710Shselaskystatic inline void list_cut_position(struct list_head *list, 375270710Shselasky struct list_head *head, struct list_head *entry) 376270710Shselasky{ 377270710Shselasky if (list_empty(head)) 378270710Shselasky return; 379270710Shselasky if (list_is_singular(head) && 380270710Shselasky (head->next != entry && head != entry)) 381270710Shselasky return; 382270710Shselasky if (entry == head) 383270710Shselasky INIT_LIST_HEAD(list); 384270710Shselasky else 385270710Shselasky __list_cut_position(list, head, entry); 386270710Shselasky} 387270710Shselasky 388270710Shselasky/** 389270710Shselasky * list_is_last - tests whether @list is the last entry in list @head 390270710Shselasky * @list: the entry to test 391270710Shselasky * @head: the head of the list 392270710Shselasky */ 393270710Shselaskystatic inline int list_is_last(const struct list_head *list, 394270710Shselasky const struct list_head *head) 395270710Shselasky{ 396270710Shselasky return list->next == head; 397270710Shselasky} 398219820Sjeff 399219820Sjeff#define hlist_entry(ptr, type, field) container_of(ptr, type, field) 400219820Sjeff 401219820Sjeff#define hlist_for_each(p, head) \ 402219820Sjeff for (p = (head)->first; p; p = p->next) 403219820Sjeff 404219820Sjeff#define hlist_for_each_safe(p, n, head) \ 405219820Sjeff for (p = (head)->first; p && ({ n = p->next; 1; }); p = n) 406219820Sjeff 407289574Shselasky#define hlist_entry_safe(ptr, type, member) \ 408289574Shselasky ((ptr) ? hlist_entry(ptr, type, member) : NULL) 409289574Shselasky 410289574Shselasky#define hlist_for_each_entry(pos, head, member) \ 411289574Shselasky for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 412289574Shselasky pos; \ 413289574Shselasky pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 414289574Shselasky 415219820Sjeff#define hlist_for_each_entry_continue(tp, p, field) \ 416219820Sjeff for (p = (p)->next; \ 417219820Sjeff p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 418219820Sjeff 419219820Sjeff#define hlist_for_each_entry_from(tp, p, field) \ 420219820Sjeff for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 421219820Sjeff 422270710Shselasky#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ 423270710Shselasky for (pos = (head)->first; \ 424270710Shselasky (pos) != 0 && ({ n = (pos)->next; \ 425270710Shselasky tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \ 426270710Shselasky pos = (n)) 427219820Sjeff 428289574Shselasky#define hlist_add_head_rcu(n, h) hlist_add_head(n, h) 429289574Shselasky 430289574Shselasky#define hlist_del_init_rcu(n) hlist_del_init(n) 431289574Shselasky 432219820Sjeff#endif /* _LINUX_LIST_H_ */ 433