list.h revision 275599
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. 28219820Sjeff */ 29219820Sjeff#ifndef _LINUX_LIST_H_ 30219820Sjeff#define _LINUX_LIST_H_ 31219820Sjeff 32219820Sjeff/* 33219820Sjeff * Since LIST_HEAD conflicts with the linux definition we must include any 34219820Sjeff * FreeBSD header which requires it here so it is resolved with the correct 35219820Sjeff * definition prior to the undef. 36219820Sjeff */ 37219820Sjeff#include <linux/types.h> 38219820Sjeff 39219820Sjeff#include <sys/param.h> 40219820Sjeff#include <sys/kernel.h> 41219820Sjeff#include <sys/queue.h> 42222813Sattilio#include <sys/cpuset.h> 43275599Srodrigc#include <sys/jail.h> 44219820Sjeff#include <sys/lock.h> 45219820Sjeff#include <sys/mutex.h> 46219820Sjeff#include <sys/proc.h> 47219820Sjeff#include <sys/vnode.h> 48219820Sjeff#include <sys/conf.h> 49219820Sjeff#include <sys/socket.h> 50219820Sjeff#include <sys/mbuf.h> 51219820Sjeff 52219820Sjeff#include <net/bpf.h> 53219820Sjeff#include <net/if.h> 54257176Sglebius#include <net/if_var.h> 55219820Sjeff#include <net/if_types.h> 56219820Sjeff#include <net/if_media.h> 57275599Srodrigc#include <net/vnet.h> 58219820Sjeff 59219820Sjeff#include <netinet/in.h> 60219820Sjeff#include <netinet/in_pcb.h> 61219820Sjeff 62219820Sjeff#include <netinet6/in6_var.h> 63219820Sjeff#include <netinet6/nd6.h> 64219820Sjeff 65219820Sjeff#include <vm/vm.h> 66219820Sjeff#include <vm/vm_object.h> 67219820Sjeff 68219820Sjeff#define prefetch(x) 69219820Sjeff 70219820Sjeffstruct list_head { 71219820Sjeff struct list_head *next; 72219820Sjeff struct list_head *prev; 73219820Sjeff}; 74219820Sjeff 75219820Sjeffstatic inline void 76219820SjeffINIT_LIST_HEAD(struct list_head *list) 77219820Sjeff{ 78219820Sjeff 79219820Sjeff list->next = list->prev = list; 80219820Sjeff} 81219820Sjeff 82219820Sjeffstatic inline int 83219820Sjefflist_empty(const struct list_head *head) 84219820Sjeff{ 85219820Sjeff 86219820Sjeff return (head->next == head); 87219820Sjeff} 88219820Sjeff 89219820Sjeffstatic inline void 90219820Sjefflist_del(struct list_head *entry) 91219820Sjeff{ 92219820Sjeff 93219820Sjeff entry->next->prev = entry->prev; 94219820Sjeff entry->prev->next = entry->next; 95219820Sjeff} 96219820Sjeff 97219820Sjeffstatic inline void 98219820Sjeff_list_add(struct list_head *new, struct list_head *prev, 99219820Sjeff struct list_head *next) 100219820Sjeff{ 101219820Sjeff 102219820Sjeff next->prev = new; 103219820Sjeff new->next = next; 104219820Sjeff new->prev = prev; 105219820Sjeff prev->next = new; 106219820Sjeff} 107219820Sjeff 108219820Sjeffstatic inline void 109219820Sjefflist_del_init(struct list_head *entry) 110219820Sjeff{ 111219820Sjeff 112219820Sjeff list_del(entry); 113219820Sjeff INIT_LIST_HEAD(entry); 114219820Sjeff} 115219820Sjeff 116219820Sjeff#define list_entry(ptr, type, field) container_of(ptr, type, field) 117219820Sjeff 118255932Salfred#define list_first_entry(ptr, type, member) \ 119255932Salfred list_entry((ptr)->next, type, member) 120255932Salfred 121219820Sjeff#define list_for_each(p, head) \ 122219820Sjeff for (p = (head)->next; p != (head); p = p->next) 123219820Sjeff 124219820Sjeff#define list_for_each_safe(p, n, head) \ 125219820Sjeff for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next) 126219820Sjeff 127219820Sjeff#define list_for_each_entry(p, h, field) \ 128219820Sjeff for (p = list_entry((h)->next, typeof(*p), field); &p->field != (h); \ 129219820Sjeff p = list_entry(p->field.next, typeof(*p), field)) 130219820Sjeff 131219820Sjeff#define list_for_each_entry_safe(p, n, h, field) \ 132219820Sjeff for (p = list_entry((h)->next, typeof(*p), field), \ 133219820Sjeff n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\ 134219820Sjeff p = n, n = list_entry(n->field.next, typeof(*n), field)) 135219820Sjeff 136219820Sjeff#define list_for_each_entry_reverse(p, h, field) \ 137219820Sjeff for (p = list_entry((h)->prev, typeof(*p), field); &p->field != (h); \ 138219820Sjeff p = list_entry(p->field.prev, typeof(*p), field)) 139219820Sjeff 140219820Sjeff#define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev) 141219820Sjeff 142219820Sjeffstatic inline void 143219820Sjefflist_add(struct list_head *new, struct list_head *head) 144219820Sjeff{ 145219820Sjeff 146219820Sjeff _list_add(new, head, head->next); 147219820Sjeff} 148219820Sjeff 149219820Sjeffstatic inline void 150219820Sjefflist_add_tail(struct list_head *new, struct list_head *head) 151219820Sjeff{ 152219820Sjeff 153219820Sjeff _list_add(new, head->prev, head); 154219820Sjeff} 155219820Sjeff 156219820Sjeffstatic inline void 157219820Sjefflist_move(struct list_head *list, struct list_head *head) 158219820Sjeff{ 159219820Sjeff 160219820Sjeff list_del(list); 161219820Sjeff list_add(list, head); 162219820Sjeff} 163219820Sjeff 164219820Sjeffstatic inline void 165219820Sjefflist_move_tail(struct list_head *entry, struct list_head *head) 166219820Sjeff{ 167219820Sjeff 168219820Sjeff list_del(entry); 169219820Sjeff list_add_tail(entry, head); 170219820Sjeff} 171219820Sjeff 172219820Sjeffstatic inline void 173219820Sjeff_list_splice(const struct list_head *list, struct list_head *prev, 174219820Sjeff struct list_head *next) 175219820Sjeff{ 176219820Sjeff struct list_head *first; 177219820Sjeff struct list_head *last; 178219820Sjeff 179219820Sjeff if (list_empty(list)) 180219820Sjeff return; 181219820Sjeff first = list->next; 182219820Sjeff last = list->prev; 183219820Sjeff first->prev = prev; 184219820Sjeff prev->next = first; 185219820Sjeff last->next = next; 186219820Sjeff next->prev = last; 187219820Sjeff} 188219820Sjeff 189219820Sjeffstatic inline void 190219820Sjefflist_splice(const struct list_head *list, struct list_head *head) 191219820Sjeff{ 192219820Sjeff 193219820Sjeff _list_splice(list, head, head->next); 194219820Sjeff} 195219820Sjeff 196219820Sjeffstatic inline void 197219820Sjefflist_splice_tail(struct list_head *list, struct list_head *head) 198219820Sjeff{ 199219820Sjeff 200219820Sjeff _list_splice(list, head->prev, head); 201219820Sjeff} 202219820Sjeff 203219820Sjeffstatic inline void 204219820Sjefflist_splice_init(struct list_head *list, struct list_head *head) 205219820Sjeff{ 206219820Sjeff 207219820Sjeff _list_splice(list, head, head->next); 208219820Sjeff INIT_LIST_HEAD(list); 209219820Sjeff} 210219820Sjeff 211219820Sjeffstatic inline void 212219820Sjefflist_splice_tail_init(struct list_head *list, struct list_head *head) 213219820Sjeff{ 214219820Sjeff 215219820Sjeff _list_splice(list, head->prev, head); 216219820Sjeff INIT_LIST_HEAD(list); 217219820Sjeff} 218219820Sjeff 219219820Sjeff#undef LIST_HEAD 220219820Sjeff#define LIST_HEAD(name) struct list_head name = { &(name), &(name) } 221219820Sjeff 222219820Sjeff 223219820Sjeffstruct hlist_head { 224219820Sjeff struct hlist_node *first; 225219820Sjeff}; 226219820Sjeff 227219820Sjeffstruct hlist_node { 228219820Sjeff struct hlist_node *next, **pprev; 229219820Sjeff}; 230219820Sjeff 231219820Sjeff#define HLIST_HEAD_INIT { } 232219820Sjeff#define HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT 233219820Sjeff#define INIT_HLIST_HEAD(head) (head)->first = NULL 234219820Sjeff#define INIT_HLIST_NODE(node) \ 235219820Sjeffdo { \ 236219820Sjeff (node)->next = NULL; \ 237219820Sjeff (node)->pprev = NULL; \ 238219820Sjeff} while (0) 239219820Sjeff 240219820Sjeffstatic inline int 241219820Sjeffhlist_unhashed(const struct hlist_node *h) 242219820Sjeff{ 243219820Sjeff 244219820Sjeff return !h->pprev; 245219820Sjeff} 246219820Sjeff 247219820Sjeffstatic inline int 248219820Sjeffhlist_empty(const struct hlist_head *h) 249219820Sjeff{ 250219820Sjeff 251219820Sjeff return !h->first; 252219820Sjeff} 253219820Sjeff 254219820Sjeffstatic inline void 255219820Sjeffhlist_del(struct hlist_node *n) 256219820Sjeff{ 257219820Sjeff 258219820Sjeff if (n->next) 259219820Sjeff n->next->pprev = n->pprev; 260219820Sjeff *n->pprev = n->next; 261219820Sjeff} 262219820Sjeff 263219820Sjeffstatic inline void 264219820Sjeffhlist_del_init(struct hlist_node *n) 265219820Sjeff{ 266219820Sjeff 267219820Sjeff if (hlist_unhashed(n)) 268219820Sjeff return; 269219820Sjeff hlist_del(n); 270219820Sjeff INIT_HLIST_NODE(n); 271219820Sjeff} 272219820Sjeff 273219820Sjeffstatic inline void 274219820Sjeffhlist_add_head(struct hlist_node *n, struct hlist_head *h) 275219820Sjeff{ 276219820Sjeff 277219820Sjeff n->next = h->first; 278219820Sjeff if (h->first) 279219820Sjeff h->first->pprev = &n->next; 280219820Sjeff h->first = n; 281219820Sjeff n->pprev = &h->first; 282219820Sjeff} 283219820Sjeff 284219820Sjeffstatic inline void 285219820Sjeffhlist_add_before(struct hlist_node *n, struct hlist_node *next) 286219820Sjeff{ 287219820Sjeff 288219820Sjeff n->pprev = next->pprev; 289219820Sjeff n->next = next; 290219820Sjeff next->pprev = &n->next; 291219820Sjeff *(n->pprev) = n; 292219820Sjeff} 293219820Sjeff 294219820Sjeffstatic inline void 295219820Sjeffhlist_add_after(struct hlist_node *n, struct hlist_node *next) 296219820Sjeff{ 297219820Sjeff 298219820Sjeff next->next = n->next; 299219820Sjeff n->next = next; 300219820Sjeff next->pprev = &n->next; 301219820Sjeff if (next->next) 302219820Sjeff next->next->pprev = &next->next; 303219820Sjeff} 304219820Sjeff 305219820Sjeffstatic inline void 306219820Sjeffhlist_move_list(struct hlist_head *old, struct hlist_head *new) 307219820Sjeff{ 308219820Sjeff 309219820Sjeff new->first = old->first; 310219820Sjeff if (new->first) 311219820Sjeff new->first->pprev = &new->first; 312219820Sjeff old->first = NULL; 313219820Sjeff} 314270710Shselasky 315270710Shselasky/** 316270710Shselasky * list_is_singular - tests whether a list has just one entry. 317270710Shselasky * @head: the list to test. 318270710Shselasky */ 319270710Shselaskystatic inline int list_is_singular(const struct list_head *head) 320270710Shselasky{ 321270710Shselasky return !list_empty(head) && (head->next == head->prev); 322270710Shselasky} 323270710Shselasky 324270710Shselaskystatic inline void __list_cut_position(struct list_head *list, 325270710Shselasky struct list_head *head, struct list_head *entry) 326270710Shselasky{ 327270710Shselasky struct list_head *new_first = entry->next; 328270710Shselasky list->next = head->next; 329270710Shselasky list->next->prev = list; 330270710Shselasky list->prev = entry; 331270710Shselasky entry->next = list; 332270710Shselasky head->next = new_first; 333270710Shselasky new_first->prev = head; 334270710Shselasky} 335270710Shselasky 336270710Shselasky/** 337270710Shselasky * list_cut_position - cut a list into two 338270710Shselasky * @list: a new list to add all removed entries 339270710Shselasky * @head: a list with entries 340270710Shselasky * @entry: an entry within head, could be the head itself 341270710Shselasky * and if so we won't cut the list 342270710Shselasky * 343270710Shselasky * This helper moves the initial part of @head, up to and 344270710Shselasky * including @entry, from @head to @list. You should 345270710Shselasky * pass on @entry an element you know is on @head. @list 346270710Shselasky * should be an empty list or a list you do not care about 347270710Shselasky * losing its data. 348270710Shselasky * 349270710Shselasky */ 350270710Shselaskystatic inline void list_cut_position(struct list_head *list, 351270710Shselasky struct list_head *head, struct list_head *entry) 352270710Shselasky{ 353270710Shselasky if (list_empty(head)) 354270710Shselasky return; 355270710Shselasky if (list_is_singular(head) && 356270710Shselasky (head->next != entry && head != entry)) 357270710Shselasky return; 358270710Shselasky if (entry == head) 359270710Shselasky INIT_LIST_HEAD(list); 360270710Shselasky else 361270710Shselasky __list_cut_position(list, head, entry); 362270710Shselasky} 363270710Shselasky 364270710Shselasky/** 365270710Shselasky * list_is_last - tests whether @list is the last entry in list @head 366270710Shselasky * @list: the entry to test 367270710Shselasky * @head: the head of the list 368270710Shselasky */ 369270710Shselaskystatic inline int list_is_last(const struct list_head *list, 370270710Shselasky const struct list_head *head) 371270710Shselasky{ 372270710Shselasky return list->next == head; 373270710Shselasky} 374219820Sjeff 375219820Sjeff#define hlist_entry(ptr, type, field) container_of(ptr, type, field) 376219820Sjeff 377219820Sjeff#define hlist_for_each(p, head) \ 378219820Sjeff for (p = (head)->first; p; p = p->next) 379219820Sjeff 380219820Sjeff#define hlist_for_each_safe(p, n, head) \ 381219820Sjeff for (p = (head)->first; p && ({ n = p->next; 1; }); p = n) 382219820Sjeff 383219820Sjeff#define hlist_for_each_entry(tp, p, head, field) \ 384219820Sjeff for (p = (head)->first; \ 385219820Sjeff p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 386219820Sjeff 387219820Sjeff#define hlist_for_each_entry_continue(tp, p, field) \ 388219820Sjeff for (p = (p)->next; \ 389219820Sjeff p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 390219820Sjeff 391219820Sjeff#define hlist_for_each_entry_from(tp, p, field) \ 392219820Sjeff for (; p ? (tp = hlist_entry(p, typeof(*tp), field)): NULL; p = p->next) 393219820Sjeff 394270710Shselasky#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ 395270710Shselasky for (pos = (head)->first; \ 396270710Shselasky (pos) != 0 && ({ n = (pos)->next; \ 397270710Shselasky tpos = hlist_entry((pos), typeof(*(tpos)), member); 1;}); \ 398270710Shselasky pos = (n)) 399219820Sjeff 400219820Sjeff#endif /* _LINUX_LIST_H_ */ 401