1*d1b39d41SJosh Poimboeuf #ifndef __TOOLS_LINUX_LIST_H 2*d1b39d41SJosh Poimboeuf #define __TOOLS_LINUX_LIST_H 3*d1b39d41SJosh Poimboeuf 44fc62a89SWang Nan #include <linux/types.h> 5*d1b39d41SJosh Poimboeuf #include <linux/poison.h> 6*d1b39d41SJosh Poimboeuf #include <linux/kernel.h> 7*d1b39d41SJosh Poimboeuf #include <linux/compiler.h> 84fc62a89SWang Nan 9*d1b39d41SJosh Poimboeuf /* 10*d1b39d41SJosh Poimboeuf * Simple doubly linked list implementation. 11*d1b39d41SJosh Poimboeuf * 12*d1b39d41SJosh Poimboeuf * Some of the internal functions ("__xxx") are useful when 13*d1b39d41SJosh Poimboeuf * manipulating whole lists rather than single entries, as 14*d1b39d41SJosh Poimboeuf * sometimes we already know the next/prev entries and we can 15*d1b39d41SJosh Poimboeuf * generate better code by using them directly rather than 16*d1b39d41SJosh Poimboeuf * using the generic single-entry routines. 17*d1b39d41SJosh Poimboeuf */ 184fc62a89SWang Nan 19*d1b39d41SJosh Poimboeuf #define LIST_HEAD_INIT(name) { &(name), &(name) } 20*d1b39d41SJosh Poimboeuf 21*d1b39d41SJosh Poimboeuf #define LIST_HEAD(name) \ 22*d1b39d41SJosh Poimboeuf struct list_head name = LIST_HEAD_INIT(name) 23*d1b39d41SJosh Poimboeuf 24*d1b39d41SJosh Poimboeuf static inline void INIT_LIST_HEAD(struct list_head *list) 25*d1b39d41SJosh Poimboeuf { 26*d1b39d41SJosh Poimboeuf list->next = list; 27*d1b39d41SJosh Poimboeuf list->prev = list; 28*d1b39d41SJosh Poimboeuf } 29*d1b39d41SJosh Poimboeuf 30*d1b39d41SJosh Poimboeuf /* 31*d1b39d41SJosh Poimboeuf * Insert a new entry between two known consecutive entries. 32*d1b39d41SJosh Poimboeuf * 33*d1b39d41SJosh Poimboeuf * This is only for internal list manipulation where we know 34*d1b39d41SJosh Poimboeuf * the prev/next entries already! 35*d1b39d41SJosh Poimboeuf */ 36*d1b39d41SJosh Poimboeuf #ifndef CONFIG_DEBUG_LIST 37*d1b39d41SJosh Poimboeuf static inline void __list_add(struct list_head *new, 38*d1b39d41SJosh Poimboeuf struct list_head *prev, 39*d1b39d41SJosh Poimboeuf struct list_head *next) 40*d1b39d41SJosh Poimboeuf { 41*d1b39d41SJosh Poimboeuf next->prev = new; 42*d1b39d41SJosh Poimboeuf new->next = next; 43*d1b39d41SJosh Poimboeuf new->prev = prev; 44*d1b39d41SJosh Poimboeuf prev->next = new; 45*d1b39d41SJosh Poimboeuf } 46*d1b39d41SJosh Poimboeuf #else 47*d1b39d41SJosh Poimboeuf extern void __list_add(struct list_head *new, 48*d1b39d41SJosh Poimboeuf struct list_head *prev, 49*d1b39d41SJosh Poimboeuf struct list_head *next); 50*d1b39d41SJosh Poimboeuf #endif 51*d1b39d41SJosh Poimboeuf 52*d1b39d41SJosh Poimboeuf /** 53*d1b39d41SJosh Poimboeuf * list_add - add a new entry 54*d1b39d41SJosh Poimboeuf * @new: new entry to be added 55*d1b39d41SJosh Poimboeuf * @head: list head to add it after 56*d1b39d41SJosh Poimboeuf * 57*d1b39d41SJosh Poimboeuf * Insert a new entry after the specified head. 58*d1b39d41SJosh Poimboeuf * This is good for implementing stacks. 59*d1b39d41SJosh Poimboeuf */ 60*d1b39d41SJosh Poimboeuf static inline void list_add(struct list_head *new, struct list_head *head) 61*d1b39d41SJosh Poimboeuf { 62*d1b39d41SJosh Poimboeuf __list_add(new, head, head->next); 63*d1b39d41SJosh Poimboeuf } 64*d1b39d41SJosh Poimboeuf 65*d1b39d41SJosh Poimboeuf 66*d1b39d41SJosh Poimboeuf /** 67*d1b39d41SJosh Poimboeuf * list_add_tail - add a new entry 68*d1b39d41SJosh Poimboeuf * @new: new entry to be added 69*d1b39d41SJosh Poimboeuf * @head: list head to add it before 70*d1b39d41SJosh Poimboeuf * 71*d1b39d41SJosh Poimboeuf * Insert a new entry before the specified head. 72*d1b39d41SJosh Poimboeuf * This is useful for implementing queues. 73*d1b39d41SJosh Poimboeuf */ 74*d1b39d41SJosh Poimboeuf static inline void list_add_tail(struct list_head *new, struct list_head *head) 75*d1b39d41SJosh Poimboeuf { 76*d1b39d41SJosh Poimboeuf __list_add(new, head->prev, head); 77*d1b39d41SJosh Poimboeuf } 78*d1b39d41SJosh Poimboeuf 79*d1b39d41SJosh Poimboeuf /* 80*d1b39d41SJosh Poimboeuf * Delete a list entry by making the prev/next entries 81*d1b39d41SJosh Poimboeuf * point to each other. 82*d1b39d41SJosh Poimboeuf * 83*d1b39d41SJosh Poimboeuf * This is only for internal list manipulation where we know 84*d1b39d41SJosh Poimboeuf * the prev/next entries already! 85*d1b39d41SJosh Poimboeuf */ 86*d1b39d41SJosh Poimboeuf static inline void __list_del(struct list_head * prev, struct list_head * next) 87*d1b39d41SJosh Poimboeuf { 88*d1b39d41SJosh Poimboeuf next->prev = prev; 89*d1b39d41SJosh Poimboeuf WRITE_ONCE(prev->next, next); 90*d1b39d41SJosh Poimboeuf } 91*d1b39d41SJosh Poimboeuf 92*d1b39d41SJosh Poimboeuf /** 93*d1b39d41SJosh Poimboeuf * list_del - deletes entry from list. 94*d1b39d41SJosh Poimboeuf * @entry: the element to delete from the list. 95*d1b39d41SJosh Poimboeuf * Note: list_empty() on entry does not return true after this, the entry is 96*d1b39d41SJosh Poimboeuf * in an undefined state. 97*d1b39d41SJosh Poimboeuf */ 98*d1b39d41SJosh Poimboeuf #ifndef CONFIG_DEBUG_LIST 99*d1b39d41SJosh Poimboeuf static inline void __list_del_entry(struct list_head *entry) 100*d1b39d41SJosh Poimboeuf { 101*d1b39d41SJosh Poimboeuf __list_del(entry->prev, entry->next); 102*d1b39d41SJosh Poimboeuf } 103*d1b39d41SJosh Poimboeuf 104*d1b39d41SJosh Poimboeuf static inline void list_del(struct list_head *entry) 105*d1b39d41SJosh Poimboeuf { 106*d1b39d41SJosh Poimboeuf __list_del(entry->prev, entry->next); 107*d1b39d41SJosh Poimboeuf entry->next = LIST_POISON1; 108*d1b39d41SJosh Poimboeuf entry->prev = LIST_POISON2; 109*d1b39d41SJosh Poimboeuf } 110*d1b39d41SJosh Poimboeuf #else 111*d1b39d41SJosh Poimboeuf extern void __list_del_entry(struct list_head *entry); 112*d1b39d41SJosh Poimboeuf extern void list_del(struct list_head *entry); 113*d1b39d41SJosh Poimboeuf #endif 114*d1b39d41SJosh Poimboeuf 115*d1b39d41SJosh Poimboeuf /** 116*d1b39d41SJosh Poimboeuf * list_replace - replace old entry by new one 117*d1b39d41SJosh Poimboeuf * @old : the element to be replaced 118*d1b39d41SJosh Poimboeuf * @new : the new element to insert 119*d1b39d41SJosh Poimboeuf * 120*d1b39d41SJosh Poimboeuf * If @old was empty, it will be overwritten. 121*d1b39d41SJosh Poimboeuf */ 122*d1b39d41SJosh Poimboeuf static inline void list_replace(struct list_head *old, 123*d1b39d41SJosh Poimboeuf struct list_head *new) 124*d1b39d41SJosh Poimboeuf { 125*d1b39d41SJosh Poimboeuf new->next = old->next; 126*d1b39d41SJosh Poimboeuf new->next->prev = new; 127*d1b39d41SJosh Poimboeuf new->prev = old->prev; 128*d1b39d41SJosh Poimboeuf new->prev->next = new; 129*d1b39d41SJosh Poimboeuf } 130*d1b39d41SJosh Poimboeuf 131*d1b39d41SJosh Poimboeuf static inline void list_replace_init(struct list_head *old, 132*d1b39d41SJosh Poimboeuf struct list_head *new) 133*d1b39d41SJosh Poimboeuf { 134*d1b39d41SJosh Poimboeuf list_replace(old, new); 135*d1b39d41SJosh Poimboeuf INIT_LIST_HEAD(old); 136*d1b39d41SJosh Poimboeuf } 137*d1b39d41SJosh Poimboeuf 138*d1b39d41SJosh Poimboeuf /** 139*d1b39d41SJosh Poimboeuf * list_del_init - deletes entry from list and reinitialize it. 140*d1b39d41SJosh Poimboeuf * @entry: the element to delete from the list. 141*d1b39d41SJosh Poimboeuf */ 142*d1b39d41SJosh Poimboeuf static inline void list_del_init(struct list_head *entry) 143*d1b39d41SJosh Poimboeuf { 144*d1b39d41SJosh Poimboeuf __list_del_entry(entry); 145*d1b39d41SJosh Poimboeuf INIT_LIST_HEAD(entry); 146*d1b39d41SJosh Poimboeuf } 147*d1b39d41SJosh Poimboeuf 148*d1b39d41SJosh Poimboeuf /** 149*d1b39d41SJosh Poimboeuf * list_move - delete from one list and add as another's head 150*d1b39d41SJosh Poimboeuf * @list: the entry to move 151*d1b39d41SJosh Poimboeuf * @head: the head that will precede our entry 152*d1b39d41SJosh Poimboeuf */ 153*d1b39d41SJosh Poimboeuf static inline void list_move(struct list_head *list, struct list_head *head) 154*d1b39d41SJosh Poimboeuf { 155*d1b39d41SJosh Poimboeuf __list_del_entry(list); 156*d1b39d41SJosh Poimboeuf list_add(list, head); 157*d1b39d41SJosh Poimboeuf } 158*d1b39d41SJosh Poimboeuf 159*d1b39d41SJosh Poimboeuf /** 160*d1b39d41SJosh Poimboeuf * list_move_tail - delete from one list and add as another's tail 161*d1b39d41SJosh Poimboeuf * @list: the entry to move 162*d1b39d41SJosh Poimboeuf * @head: the head that will follow our entry 163*d1b39d41SJosh Poimboeuf */ 164*d1b39d41SJosh Poimboeuf static inline void list_move_tail(struct list_head *list, 165*d1b39d41SJosh Poimboeuf struct list_head *head) 166*d1b39d41SJosh Poimboeuf { 167*d1b39d41SJosh Poimboeuf __list_del_entry(list); 168*d1b39d41SJosh Poimboeuf list_add_tail(list, head); 169*d1b39d41SJosh Poimboeuf } 170*d1b39d41SJosh Poimboeuf 171*d1b39d41SJosh Poimboeuf /** 172*d1b39d41SJosh Poimboeuf * list_is_last - tests whether @list is the last entry in list @head 173*d1b39d41SJosh Poimboeuf * @list: the entry to test 174*d1b39d41SJosh Poimboeuf * @head: the head of the list 175*d1b39d41SJosh Poimboeuf */ 176*d1b39d41SJosh Poimboeuf static inline int list_is_last(const struct list_head *list, 177*d1b39d41SJosh Poimboeuf const struct list_head *head) 178*d1b39d41SJosh Poimboeuf { 179*d1b39d41SJosh Poimboeuf return list->next == head; 180*d1b39d41SJosh Poimboeuf } 181*d1b39d41SJosh Poimboeuf 182*d1b39d41SJosh Poimboeuf /** 183*d1b39d41SJosh Poimboeuf * list_empty - tests whether a list is empty 184*d1b39d41SJosh Poimboeuf * @head: the list to test. 185*d1b39d41SJosh Poimboeuf */ 186*d1b39d41SJosh Poimboeuf static inline int list_empty(const struct list_head *head) 187*d1b39d41SJosh Poimboeuf { 188*d1b39d41SJosh Poimboeuf return head->next == head; 189*d1b39d41SJosh Poimboeuf } 190*d1b39d41SJosh Poimboeuf 191*d1b39d41SJosh Poimboeuf /** 192*d1b39d41SJosh Poimboeuf * list_empty_careful - tests whether a list is empty and not being modified 193*d1b39d41SJosh Poimboeuf * @head: the list to test 194*d1b39d41SJosh Poimboeuf * 195*d1b39d41SJosh Poimboeuf * Description: 196*d1b39d41SJosh Poimboeuf * tests whether a list is empty _and_ checks that no other CPU might be 197*d1b39d41SJosh Poimboeuf * in the process of modifying either member (next or prev) 198*d1b39d41SJosh Poimboeuf * 199*d1b39d41SJosh Poimboeuf * NOTE: using list_empty_careful() without synchronization 200*d1b39d41SJosh Poimboeuf * can only be safe if the only activity that can happen 201*d1b39d41SJosh Poimboeuf * to the list entry is list_del_init(). Eg. it cannot be used 202*d1b39d41SJosh Poimboeuf * if another CPU could re-list_add() it. 203*d1b39d41SJosh Poimboeuf */ 204*d1b39d41SJosh Poimboeuf static inline int list_empty_careful(const struct list_head *head) 205*d1b39d41SJosh Poimboeuf { 206*d1b39d41SJosh Poimboeuf struct list_head *next = head->next; 207*d1b39d41SJosh Poimboeuf return (next == head) && (next == head->prev); 208*d1b39d41SJosh Poimboeuf } 209*d1b39d41SJosh Poimboeuf 210*d1b39d41SJosh Poimboeuf /** 211*d1b39d41SJosh Poimboeuf * list_rotate_left - rotate the list to the left 212*d1b39d41SJosh Poimboeuf * @head: the head of the list 213*d1b39d41SJosh Poimboeuf */ 214*d1b39d41SJosh Poimboeuf static inline void list_rotate_left(struct list_head *head) 215*d1b39d41SJosh Poimboeuf { 216*d1b39d41SJosh Poimboeuf struct list_head *first; 217*d1b39d41SJosh Poimboeuf 218*d1b39d41SJosh Poimboeuf if (!list_empty(head)) { 219*d1b39d41SJosh Poimboeuf first = head->next; 220*d1b39d41SJosh Poimboeuf list_move_tail(first, head); 221*d1b39d41SJosh Poimboeuf } 222*d1b39d41SJosh Poimboeuf } 223*d1b39d41SJosh Poimboeuf 224*d1b39d41SJosh Poimboeuf /** 225*d1b39d41SJosh Poimboeuf * list_is_singular - tests whether a list has just one entry. 226*d1b39d41SJosh Poimboeuf * @head: the list to test. 227*d1b39d41SJosh Poimboeuf */ 228*d1b39d41SJosh Poimboeuf static inline int list_is_singular(const struct list_head *head) 229*d1b39d41SJosh Poimboeuf { 230*d1b39d41SJosh Poimboeuf return !list_empty(head) && (head->next == head->prev); 231*d1b39d41SJosh Poimboeuf } 232*d1b39d41SJosh Poimboeuf 233*d1b39d41SJosh Poimboeuf static inline void __list_cut_position(struct list_head *list, 234*d1b39d41SJosh Poimboeuf struct list_head *head, struct list_head *entry) 235*d1b39d41SJosh Poimboeuf { 236*d1b39d41SJosh Poimboeuf struct list_head *new_first = entry->next; 237*d1b39d41SJosh Poimboeuf list->next = head->next; 238*d1b39d41SJosh Poimboeuf list->next->prev = list; 239*d1b39d41SJosh Poimboeuf list->prev = entry; 240*d1b39d41SJosh Poimboeuf entry->next = list; 241*d1b39d41SJosh Poimboeuf head->next = new_first; 242*d1b39d41SJosh Poimboeuf new_first->prev = head; 243*d1b39d41SJosh Poimboeuf } 244*d1b39d41SJosh Poimboeuf 245*d1b39d41SJosh Poimboeuf /** 246*d1b39d41SJosh Poimboeuf * list_cut_position - cut a list into two 247*d1b39d41SJosh Poimboeuf * @list: a new list to add all removed entries 248*d1b39d41SJosh Poimboeuf * @head: a list with entries 249*d1b39d41SJosh Poimboeuf * @entry: an entry within head, could be the head itself 250*d1b39d41SJosh Poimboeuf * and if so we won't cut the list 251*d1b39d41SJosh Poimboeuf * 252*d1b39d41SJosh Poimboeuf * This helper moves the initial part of @head, up to and 253*d1b39d41SJosh Poimboeuf * including @entry, from @head to @list. You should 254*d1b39d41SJosh Poimboeuf * pass on @entry an element you know is on @head. @list 255*d1b39d41SJosh Poimboeuf * should be an empty list or a list you do not care about 256*d1b39d41SJosh Poimboeuf * losing its data. 257*d1b39d41SJosh Poimboeuf * 258*d1b39d41SJosh Poimboeuf */ 259*d1b39d41SJosh Poimboeuf static inline void list_cut_position(struct list_head *list, 260*d1b39d41SJosh Poimboeuf struct list_head *head, struct list_head *entry) 261*d1b39d41SJosh Poimboeuf { 262*d1b39d41SJosh Poimboeuf if (list_empty(head)) 263*d1b39d41SJosh Poimboeuf return; 264*d1b39d41SJosh Poimboeuf if (list_is_singular(head) && 265*d1b39d41SJosh Poimboeuf (head->next != entry && head != entry)) 266*d1b39d41SJosh Poimboeuf return; 267*d1b39d41SJosh Poimboeuf if (entry == head) 268*d1b39d41SJosh Poimboeuf INIT_LIST_HEAD(list); 269*d1b39d41SJosh Poimboeuf else 270*d1b39d41SJosh Poimboeuf __list_cut_position(list, head, entry); 271*d1b39d41SJosh Poimboeuf } 272*d1b39d41SJosh Poimboeuf 273*d1b39d41SJosh Poimboeuf static inline void __list_splice(const struct list_head *list, 274*d1b39d41SJosh Poimboeuf struct list_head *prev, 275*d1b39d41SJosh Poimboeuf struct list_head *next) 276*d1b39d41SJosh Poimboeuf { 277*d1b39d41SJosh Poimboeuf struct list_head *first = list->next; 278*d1b39d41SJosh Poimboeuf struct list_head *last = list->prev; 279*d1b39d41SJosh Poimboeuf 280*d1b39d41SJosh Poimboeuf first->prev = prev; 281*d1b39d41SJosh Poimboeuf prev->next = first; 282*d1b39d41SJosh Poimboeuf 283*d1b39d41SJosh Poimboeuf last->next = next; 284*d1b39d41SJosh Poimboeuf next->prev = last; 285*d1b39d41SJosh Poimboeuf } 286*d1b39d41SJosh Poimboeuf 287*d1b39d41SJosh Poimboeuf /** 288*d1b39d41SJosh Poimboeuf * list_splice - join two lists, this is designed for stacks 289*d1b39d41SJosh Poimboeuf * @list: the new list to add. 290*d1b39d41SJosh Poimboeuf * @head: the place to add it in the first list. 291*d1b39d41SJosh Poimboeuf */ 292*d1b39d41SJosh Poimboeuf static inline void list_splice(const struct list_head *list, 293*d1b39d41SJosh Poimboeuf struct list_head *head) 294*d1b39d41SJosh Poimboeuf { 295*d1b39d41SJosh Poimboeuf if (!list_empty(list)) 296*d1b39d41SJosh Poimboeuf __list_splice(list, head, head->next); 297*d1b39d41SJosh Poimboeuf } 298*d1b39d41SJosh Poimboeuf 299*d1b39d41SJosh Poimboeuf /** 300*d1b39d41SJosh Poimboeuf * list_splice_tail - join two lists, each list being a queue 301*d1b39d41SJosh Poimboeuf * @list: the new list to add. 302*d1b39d41SJosh Poimboeuf * @head: the place to add it in the first list. 303*d1b39d41SJosh Poimboeuf */ 304*d1b39d41SJosh Poimboeuf static inline void list_splice_tail(struct list_head *list, 305*d1b39d41SJosh Poimboeuf struct list_head *head) 306*d1b39d41SJosh Poimboeuf { 307*d1b39d41SJosh Poimboeuf if (!list_empty(list)) 308*d1b39d41SJosh Poimboeuf __list_splice(list, head->prev, head); 309*d1b39d41SJosh Poimboeuf } 310*d1b39d41SJosh Poimboeuf 311*d1b39d41SJosh Poimboeuf /** 312*d1b39d41SJosh Poimboeuf * list_splice_init - join two lists and reinitialise the emptied list. 313*d1b39d41SJosh Poimboeuf * @list: the new list to add. 314*d1b39d41SJosh Poimboeuf * @head: the place to add it in the first list. 315*d1b39d41SJosh Poimboeuf * 316*d1b39d41SJosh Poimboeuf * The list at @list is reinitialised 317*d1b39d41SJosh Poimboeuf */ 318*d1b39d41SJosh Poimboeuf static inline void list_splice_init(struct list_head *list, 319*d1b39d41SJosh Poimboeuf struct list_head *head) 320*d1b39d41SJosh Poimboeuf { 321*d1b39d41SJosh Poimboeuf if (!list_empty(list)) { 322*d1b39d41SJosh Poimboeuf __list_splice(list, head, head->next); 323*d1b39d41SJosh Poimboeuf INIT_LIST_HEAD(list); 324*d1b39d41SJosh Poimboeuf } 325*d1b39d41SJosh Poimboeuf } 326*d1b39d41SJosh Poimboeuf 327*d1b39d41SJosh Poimboeuf /** 328*d1b39d41SJosh Poimboeuf * list_splice_tail_init - join two lists and reinitialise the emptied list 329*d1b39d41SJosh Poimboeuf * @list: the new list to add. 330*d1b39d41SJosh Poimboeuf * @head: the place to add it in the first list. 331*d1b39d41SJosh Poimboeuf * 332*d1b39d41SJosh Poimboeuf * Each of the lists is a queue. 333*d1b39d41SJosh Poimboeuf * The list at @list is reinitialised 334*d1b39d41SJosh Poimboeuf */ 335*d1b39d41SJosh Poimboeuf static inline void list_splice_tail_init(struct list_head *list, 336*d1b39d41SJosh Poimboeuf struct list_head *head) 337*d1b39d41SJosh Poimboeuf { 338*d1b39d41SJosh Poimboeuf if (!list_empty(list)) { 339*d1b39d41SJosh Poimboeuf __list_splice(list, head->prev, head); 340*d1b39d41SJosh Poimboeuf INIT_LIST_HEAD(list); 341*d1b39d41SJosh Poimboeuf } 342*d1b39d41SJosh Poimboeuf } 343*d1b39d41SJosh Poimboeuf 344*d1b39d41SJosh Poimboeuf /** 345*d1b39d41SJosh Poimboeuf * list_entry - get the struct for this entry 346*d1b39d41SJosh Poimboeuf * @ptr: the &struct list_head pointer. 347*d1b39d41SJosh Poimboeuf * @type: the type of the struct this is embedded in. 348*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 349*d1b39d41SJosh Poimboeuf */ 350*d1b39d41SJosh Poimboeuf #define list_entry(ptr, type, member) \ 351*d1b39d41SJosh Poimboeuf container_of(ptr, type, member) 352*d1b39d41SJosh Poimboeuf 353*d1b39d41SJosh Poimboeuf /** 354*d1b39d41SJosh Poimboeuf * list_first_entry - get the first element from a list 355*d1b39d41SJosh Poimboeuf * @ptr: the list head to take the element from. 356*d1b39d41SJosh Poimboeuf * @type: the type of the struct this is embedded in. 357*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 358*d1b39d41SJosh Poimboeuf * 359*d1b39d41SJosh Poimboeuf * Note, that list is expected to be not empty. 360*d1b39d41SJosh Poimboeuf */ 361*d1b39d41SJosh Poimboeuf #define list_first_entry(ptr, type, member) \ 362*d1b39d41SJosh Poimboeuf list_entry((ptr)->next, type, member) 363*d1b39d41SJosh Poimboeuf 364*d1b39d41SJosh Poimboeuf /** 365*d1b39d41SJosh Poimboeuf * list_last_entry - get the last element from a list 366*d1b39d41SJosh Poimboeuf * @ptr: the list head to take the element from. 367*d1b39d41SJosh Poimboeuf * @type: the type of the struct this is embedded in. 368*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 369*d1b39d41SJosh Poimboeuf * 370*d1b39d41SJosh Poimboeuf * Note, that list is expected to be not empty. 371*d1b39d41SJosh Poimboeuf */ 372*d1b39d41SJosh Poimboeuf #define list_last_entry(ptr, type, member) \ 373*d1b39d41SJosh Poimboeuf list_entry((ptr)->prev, type, member) 374*d1b39d41SJosh Poimboeuf 375*d1b39d41SJosh Poimboeuf /** 376*d1b39d41SJosh Poimboeuf * list_first_entry_or_null - get the first element from a list 377*d1b39d41SJosh Poimboeuf * @ptr: the list head to take the element from. 378*d1b39d41SJosh Poimboeuf * @type: the type of the struct this is embedded in. 379*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 380*d1b39d41SJosh Poimboeuf * 381*d1b39d41SJosh Poimboeuf * Note that if the list is empty, it returns NULL. 382*d1b39d41SJosh Poimboeuf */ 383*d1b39d41SJosh Poimboeuf #define list_first_entry_or_null(ptr, type, member) \ 384*d1b39d41SJosh Poimboeuf (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL) 385*d1b39d41SJosh Poimboeuf 386*d1b39d41SJosh Poimboeuf /** 387*d1b39d41SJosh Poimboeuf * list_next_entry - get the next element in list 388*d1b39d41SJosh Poimboeuf * @pos: the type * to cursor 389*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 390*d1b39d41SJosh Poimboeuf */ 391*d1b39d41SJosh Poimboeuf #define list_next_entry(pos, member) \ 392*d1b39d41SJosh Poimboeuf list_entry((pos)->member.next, typeof(*(pos)), member) 393*d1b39d41SJosh Poimboeuf 394*d1b39d41SJosh Poimboeuf /** 395*d1b39d41SJosh Poimboeuf * list_prev_entry - get the prev element in list 396*d1b39d41SJosh Poimboeuf * @pos: the type * to cursor 397*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 398*d1b39d41SJosh Poimboeuf */ 399*d1b39d41SJosh Poimboeuf #define list_prev_entry(pos, member) \ 400*d1b39d41SJosh Poimboeuf list_entry((pos)->member.prev, typeof(*(pos)), member) 401*d1b39d41SJosh Poimboeuf 402*d1b39d41SJosh Poimboeuf /** 403*d1b39d41SJosh Poimboeuf * list_for_each - iterate over a list 404*d1b39d41SJosh Poimboeuf * @pos: the &struct list_head to use as a loop cursor. 405*d1b39d41SJosh Poimboeuf * @head: the head for your list. 406*d1b39d41SJosh Poimboeuf */ 407*d1b39d41SJosh Poimboeuf #define list_for_each(pos, head) \ 408*d1b39d41SJosh Poimboeuf for (pos = (head)->next; pos != (head); pos = pos->next) 409*d1b39d41SJosh Poimboeuf 410*d1b39d41SJosh Poimboeuf /** 411*d1b39d41SJosh Poimboeuf * list_for_each_prev - iterate over a list backwards 412*d1b39d41SJosh Poimboeuf * @pos: the &struct list_head to use as a loop cursor. 413*d1b39d41SJosh Poimboeuf * @head: the head for your list. 414*d1b39d41SJosh Poimboeuf */ 415*d1b39d41SJosh Poimboeuf #define list_for_each_prev(pos, head) \ 416*d1b39d41SJosh Poimboeuf for (pos = (head)->prev; pos != (head); pos = pos->prev) 417*d1b39d41SJosh Poimboeuf 418*d1b39d41SJosh Poimboeuf /** 419*d1b39d41SJosh Poimboeuf * list_for_each_safe - iterate over a list safe against removal of list entry 420*d1b39d41SJosh Poimboeuf * @pos: the &struct list_head to use as a loop cursor. 421*d1b39d41SJosh Poimboeuf * @n: another &struct list_head to use as temporary storage 422*d1b39d41SJosh Poimboeuf * @head: the head for your list. 423*d1b39d41SJosh Poimboeuf */ 424*d1b39d41SJosh Poimboeuf #define list_for_each_safe(pos, n, head) \ 425*d1b39d41SJosh Poimboeuf for (pos = (head)->next, n = pos->next; pos != (head); \ 426*d1b39d41SJosh Poimboeuf pos = n, n = pos->next) 427*d1b39d41SJosh Poimboeuf 428*d1b39d41SJosh Poimboeuf /** 429*d1b39d41SJosh Poimboeuf * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry 430*d1b39d41SJosh Poimboeuf * @pos: the &struct list_head to use as a loop cursor. 431*d1b39d41SJosh Poimboeuf * @n: another &struct list_head to use as temporary storage 432*d1b39d41SJosh Poimboeuf * @head: the head for your list. 433*d1b39d41SJosh Poimboeuf */ 434*d1b39d41SJosh Poimboeuf #define list_for_each_prev_safe(pos, n, head) \ 435*d1b39d41SJosh Poimboeuf for (pos = (head)->prev, n = pos->prev; \ 436*d1b39d41SJosh Poimboeuf pos != (head); \ 437*d1b39d41SJosh Poimboeuf pos = n, n = pos->prev) 438*d1b39d41SJosh Poimboeuf 439*d1b39d41SJosh Poimboeuf /** 440*d1b39d41SJosh Poimboeuf * list_for_each_entry - iterate over list of given type 441*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 442*d1b39d41SJosh Poimboeuf * @head: the head for your list. 443*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 444*d1b39d41SJosh Poimboeuf */ 445*d1b39d41SJosh Poimboeuf #define list_for_each_entry(pos, head, member) \ 446*d1b39d41SJosh Poimboeuf for (pos = list_first_entry(head, typeof(*pos), member); \ 447*d1b39d41SJosh Poimboeuf &pos->member != (head); \ 448*d1b39d41SJosh Poimboeuf pos = list_next_entry(pos, member)) 449*d1b39d41SJosh Poimboeuf 450*d1b39d41SJosh Poimboeuf /** 451*d1b39d41SJosh Poimboeuf * list_for_each_entry_reverse - iterate backwards over list of given type. 452*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 453*d1b39d41SJosh Poimboeuf * @head: the head for your list. 454*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 455*d1b39d41SJosh Poimboeuf */ 456*d1b39d41SJosh Poimboeuf #define list_for_each_entry_reverse(pos, head, member) \ 457*d1b39d41SJosh Poimboeuf for (pos = list_last_entry(head, typeof(*pos), member); \ 458*d1b39d41SJosh Poimboeuf &pos->member != (head); \ 459*d1b39d41SJosh Poimboeuf pos = list_prev_entry(pos, member)) 460*d1b39d41SJosh Poimboeuf 461*d1b39d41SJosh Poimboeuf /** 462*d1b39d41SJosh Poimboeuf * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() 463*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a start point 464*d1b39d41SJosh Poimboeuf * @head: the head of the list 465*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 466*d1b39d41SJosh Poimboeuf * 467*d1b39d41SJosh Poimboeuf * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). 468*d1b39d41SJosh Poimboeuf */ 469*d1b39d41SJosh Poimboeuf #define list_prepare_entry(pos, head, member) \ 470*d1b39d41SJosh Poimboeuf ((pos) ? : list_entry(head, typeof(*pos), member)) 471*d1b39d41SJosh Poimboeuf 472*d1b39d41SJosh Poimboeuf /** 473*d1b39d41SJosh Poimboeuf * list_for_each_entry_continue - continue iteration over list of given type 474*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 475*d1b39d41SJosh Poimboeuf * @head: the head for your list. 476*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 477*d1b39d41SJosh Poimboeuf * 478*d1b39d41SJosh Poimboeuf * Continue to iterate over list of given type, continuing after 479*d1b39d41SJosh Poimboeuf * the current position. 480*d1b39d41SJosh Poimboeuf */ 481*d1b39d41SJosh Poimboeuf #define list_for_each_entry_continue(pos, head, member) \ 482*d1b39d41SJosh Poimboeuf for (pos = list_next_entry(pos, member); \ 483*d1b39d41SJosh Poimboeuf &pos->member != (head); \ 484*d1b39d41SJosh Poimboeuf pos = list_next_entry(pos, member)) 485*d1b39d41SJosh Poimboeuf 486*d1b39d41SJosh Poimboeuf /** 487*d1b39d41SJosh Poimboeuf * list_for_each_entry_continue_reverse - iterate backwards from the given point 488*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 489*d1b39d41SJosh Poimboeuf * @head: the head for your list. 490*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 491*d1b39d41SJosh Poimboeuf * 492*d1b39d41SJosh Poimboeuf * Start to iterate over list of given type backwards, continuing after 493*d1b39d41SJosh Poimboeuf * the current position. 494*d1b39d41SJosh Poimboeuf */ 495*d1b39d41SJosh Poimboeuf #define list_for_each_entry_continue_reverse(pos, head, member) \ 496*d1b39d41SJosh Poimboeuf for (pos = list_prev_entry(pos, member); \ 497*d1b39d41SJosh Poimboeuf &pos->member != (head); \ 498*d1b39d41SJosh Poimboeuf pos = list_prev_entry(pos, member)) 499*d1b39d41SJosh Poimboeuf 500*d1b39d41SJosh Poimboeuf /** 501*d1b39d41SJosh Poimboeuf * list_for_each_entry_from - iterate over list of given type from the current point 502*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 503*d1b39d41SJosh Poimboeuf * @head: the head for your list. 504*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 505*d1b39d41SJosh Poimboeuf * 506*d1b39d41SJosh Poimboeuf * Iterate over list of given type, continuing from current position. 507*d1b39d41SJosh Poimboeuf */ 508*d1b39d41SJosh Poimboeuf #define list_for_each_entry_from(pos, head, member) \ 509*d1b39d41SJosh Poimboeuf for (; &pos->member != (head); \ 510*d1b39d41SJosh Poimboeuf pos = list_next_entry(pos, member)) 511*d1b39d41SJosh Poimboeuf 512*d1b39d41SJosh Poimboeuf /** 513*d1b39d41SJosh Poimboeuf * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 514*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 515*d1b39d41SJosh Poimboeuf * @n: another type * to use as temporary storage 516*d1b39d41SJosh Poimboeuf * @head: the head for your list. 517*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 518*d1b39d41SJosh Poimboeuf */ 519*d1b39d41SJosh Poimboeuf #define list_for_each_entry_safe(pos, n, head, member) \ 520*d1b39d41SJosh Poimboeuf for (pos = list_first_entry(head, typeof(*pos), member), \ 521*d1b39d41SJosh Poimboeuf n = list_next_entry(pos, member); \ 522*d1b39d41SJosh Poimboeuf &pos->member != (head); \ 523*d1b39d41SJosh Poimboeuf pos = n, n = list_next_entry(n, member)) 524*d1b39d41SJosh Poimboeuf 525*d1b39d41SJosh Poimboeuf /** 526*d1b39d41SJosh Poimboeuf * list_for_each_entry_safe_continue - continue list iteration safe against removal 527*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 528*d1b39d41SJosh Poimboeuf * @n: another type * to use as temporary storage 529*d1b39d41SJosh Poimboeuf * @head: the head for your list. 530*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 531*d1b39d41SJosh Poimboeuf * 532*d1b39d41SJosh Poimboeuf * Iterate over list of given type, continuing after current point, 533*d1b39d41SJosh Poimboeuf * safe against removal of list entry. 534*d1b39d41SJosh Poimboeuf */ 535*d1b39d41SJosh Poimboeuf #define list_for_each_entry_safe_continue(pos, n, head, member) \ 536*d1b39d41SJosh Poimboeuf for (pos = list_next_entry(pos, member), \ 537*d1b39d41SJosh Poimboeuf n = list_next_entry(pos, member); \ 538*d1b39d41SJosh Poimboeuf &pos->member != (head); \ 539*d1b39d41SJosh Poimboeuf pos = n, n = list_next_entry(n, member)) 540*d1b39d41SJosh Poimboeuf 541*d1b39d41SJosh Poimboeuf /** 542*d1b39d41SJosh Poimboeuf * list_for_each_entry_safe_from - iterate over list from current point safe against removal 543*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 544*d1b39d41SJosh Poimboeuf * @n: another type * to use as temporary storage 545*d1b39d41SJosh Poimboeuf * @head: the head for your list. 546*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 547*d1b39d41SJosh Poimboeuf * 548*d1b39d41SJosh Poimboeuf * Iterate over list of given type from current point, safe against 549*d1b39d41SJosh Poimboeuf * removal of list entry. 550*d1b39d41SJosh Poimboeuf */ 551*d1b39d41SJosh Poimboeuf #define list_for_each_entry_safe_from(pos, n, head, member) \ 552*d1b39d41SJosh Poimboeuf for (n = list_next_entry(pos, member); \ 553*d1b39d41SJosh Poimboeuf &pos->member != (head); \ 554*d1b39d41SJosh Poimboeuf pos = n, n = list_next_entry(n, member)) 555*d1b39d41SJosh Poimboeuf 556*d1b39d41SJosh Poimboeuf /** 557*d1b39d41SJosh Poimboeuf * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal 558*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 559*d1b39d41SJosh Poimboeuf * @n: another type * to use as temporary storage 560*d1b39d41SJosh Poimboeuf * @head: the head for your list. 561*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 562*d1b39d41SJosh Poimboeuf * 563*d1b39d41SJosh Poimboeuf * Iterate backwards over list of given type, safe against removal 564*d1b39d41SJosh Poimboeuf * of list entry. 565*d1b39d41SJosh Poimboeuf */ 566*d1b39d41SJosh Poimboeuf #define list_for_each_entry_safe_reverse(pos, n, head, member) \ 567*d1b39d41SJosh Poimboeuf for (pos = list_last_entry(head, typeof(*pos), member), \ 568*d1b39d41SJosh Poimboeuf n = list_prev_entry(pos, member); \ 569*d1b39d41SJosh Poimboeuf &pos->member != (head); \ 570*d1b39d41SJosh Poimboeuf pos = n, n = list_prev_entry(n, member)) 571*d1b39d41SJosh Poimboeuf 572*d1b39d41SJosh Poimboeuf /** 573*d1b39d41SJosh Poimboeuf * list_safe_reset_next - reset a stale list_for_each_entry_safe loop 574*d1b39d41SJosh Poimboeuf * @pos: the loop cursor used in the list_for_each_entry_safe loop 575*d1b39d41SJosh Poimboeuf * @n: temporary storage used in list_for_each_entry_safe 576*d1b39d41SJosh Poimboeuf * @member: the name of the list_head within the struct. 577*d1b39d41SJosh Poimboeuf * 578*d1b39d41SJosh Poimboeuf * list_safe_reset_next is not safe to use in general if the list may be 579*d1b39d41SJosh Poimboeuf * modified concurrently (eg. the lock is dropped in the loop body). An 580*d1b39d41SJosh Poimboeuf * exception to this is if the cursor element (pos) is pinned in the list, 581*d1b39d41SJosh Poimboeuf * and list_safe_reset_next is called after re-taking the lock and before 582*d1b39d41SJosh Poimboeuf * completing the current iteration of the loop body. 583*d1b39d41SJosh Poimboeuf */ 584*d1b39d41SJosh Poimboeuf #define list_safe_reset_next(pos, n, member) \ 585*d1b39d41SJosh Poimboeuf n = list_next_entry(pos, member) 586*d1b39d41SJosh Poimboeuf 587*d1b39d41SJosh Poimboeuf /* 588*d1b39d41SJosh Poimboeuf * Double linked lists with a single pointer list head. 589*d1b39d41SJosh Poimboeuf * Mostly useful for hash tables where the two pointer list head is 590*d1b39d41SJosh Poimboeuf * too wasteful. 591*d1b39d41SJosh Poimboeuf * You lose the ability to access the tail in O(1). 592*d1b39d41SJosh Poimboeuf */ 593*d1b39d41SJosh Poimboeuf 594*d1b39d41SJosh Poimboeuf #define HLIST_HEAD_INIT { .first = NULL } 595*d1b39d41SJosh Poimboeuf #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } 596*d1b39d41SJosh Poimboeuf #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) 597*d1b39d41SJosh Poimboeuf static inline void INIT_HLIST_NODE(struct hlist_node *h) 598*d1b39d41SJosh Poimboeuf { 599*d1b39d41SJosh Poimboeuf h->next = NULL; 600*d1b39d41SJosh Poimboeuf h->pprev = NULL; 601*d1b39d41SJosh Poimboeuf } 602*d1b39d41SJosh Poimboeuf 603*d1b39d41SJosh Poimboeuf static inline int hlist_unhashed(const struct hlist_node *h) 604*d1b39d41SJosh Poimboeuf { 605*d1b39d41SJosh Poimboeuf return !h->pprev; 606*d1b39d41SJosh Poimboeuf } 607*d1b39d41SJosh Poimboeuf 608*d1b39d41SJosh Poimboeuf static inline int hlist_empty(const struct hlist_head *h) 609*d1b39d41SJosh Poimboeuf { 610*d1b39d41SJosh Poimboeuf return !h->first; 611*d1b39d41SJosh Poimboeuf } 612*d1b39d41SJosh Poimboeuf 613*d1b39d41SJosh Poimboeuf static inline void __hlist_del(struct hlist_node *n) 614*d1b39d41SJosh Poimboeuf { 615*d1b39d41SJosh Poimboeuf struct hlist_node *next = n->next; 616*d1b39d41SJosh Poimboeuf struct hlist_node **pprev = n->pprev; 617*d1b39d41SJosh Poimboeuf 618*d1b39d41SJosh Poimboeuf WRITE_ONCE(*pprev, next); 619*d1b39d41SJosh Poimboeuf if (next) 620*d1b39d41SJosh Poimboeuf next->pprev = pprev; 621*d1b39d41SJosh Poimboeuf } 622*d1b39d41SJosh Poimboeuf 623*d1b39d41SJosh Poimboeuf static inline void hlist_del(struct hlist_node *n) 624*d1b39d41SJosh Poimboeuf { 625*d1b39d41SJosh Poimboeuf __hlist_del(n); 626*d1b39d41SJosh Poimboeuf n->next = LIST_POISON1; 627*d1b39d41SJosh Poimboeuf n->pprev = LIST_POISON2; 628*d1b39d41SJosh Poimboeuf } 629*d1b39d41SJosh Poimboeuf 630*d1b39d41SJosh Poimboeuf static inline void hlist_del_init(struct hlist_node *n) 631*d1b39d41SJosh Poimboeuf { 632*d1b39d41SJosh Poimboeuf if (!hlist_unhashed(n)) { 633*d1b39d41SJosh Poimboeuf __hlist_del(n); 634*d1b39d41SJosh Poimboeuf INIT_HLIST_NODE(n); 635*d1b39d41SJosh Poimboeuf } 636*d1b39d41SJosh Poimboeuf } 637*d1b39d41SJosh Poimboeuf 638*d1b39d41SJosh Poimboeuf static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) 639*d1b39d41SJosh Poimboeuf { 640*d1b39d41SJosh Poimboeuf struct hlist_node *first = h->first; 641*d1b39d41SJosh Poimboeuf n->next = first; 642*d1b39d41SJosh Poimboeuf if (first) 643*d1b39d41SJosh Poimboeuf first->pprev = &n->next; 644*d1b39d41SJosh Poimboeuf h->first = n; 645*d1b39d41SJosh Poimboeuf n->pprev = &h->first; 646*d1b39d41SJosh Poimboeuf } 647*d1b39d41SJosh Poimboeuf 648*d1b39d41SJosh Poimboeuf /* next must be != NULL */ 649*d1b39d41SJosh Poimboeuf static inline void hlist_add_before(struct hlist_node *n, 650*d1b39d41SJosh Poimboeuf struct hlist_node *next) 651*d1b39d41SJosh Poimboeuf { 652*d1b39d41SJosh Poimboeuf n->pprev = next->pprev; 653*d1b39d41SJosh Poimboeuf n->next = next; 654*d1b39d41SJosh Poimboeuf next->pprev = &n->next; 655*d1b39d41SJosh Poimboeuf *(n->pprev) = n; 656*d1b39d41SJosh Poimboeuf } 657*d1b39d41SJosh Poimboeuf 658*d1b39d41SJosh Poimboeuf static inline void hlist_add_behind(struct hlist_node *n, 659*d1b39d41SJosh Poimboeuf struct hlist_node *prev) 660*d1b39d41SJosh Poimboeuf { 661*d1b39d41SJosh Poimboeuf n->next = prev->next; 662*d1b39d41SJosh Poimboeuf prev->next = n; 663*d1b39d41SJosh Poimboeuf n->pprev = &prev->next; 664*d1b39d41SJosh Poimboeuf 665*d1b39d41SJosh Poimboeuf if (n->next) 666*d1b39d41SJosh Poimboeuf n->next->pprev = &n->next; 667*d1b39d41SJosh Poimboeuf } 668*d1b39d41SJosh Poimboeuf 669*d1b39d41SJosh Poimboeuf /* after that we'll appear to be on some hlist and hlist_del will work */ 670*d1b39d41SJosh Poimboeuf static inline void hlist_add_fake(struct hlist_node *n) 671*d1b39d41SJosh Poimboeuf { 672*d1b39d41SJosh Poimboeuf n->pprev = &n->next; 673*d1b39d41SJosh Poimboeuf } 674*d1b39d41SJosh Poimboeuf 675*d1b39d41SJosh Poimboeuf static inline bool hlist_fake(struct hlist_node *h) 676*d1b39d41SJosh Poimboeuf { 677*d1b39d41SJosh Poimboeuf return h->pprev == &h->next; 678*d1b39d41SJosh Poimboeuf } 679*d1b39d41SJosh Poimboeuf 680*d1b39d41SJosh Poimboeuf /* 681*d1b39d41SJosh Poimboeuf * Move a list from one list head to another. Fixup the pprev 682*d1b39d41SJosh Poimboeuf * reference of the first entry if it exists. 683*d1b39d41SJosh Poimboeuf */ 684*d1b39d41SJosh Poimboeuf static inline void hlist_move_list(struct hlist_head *old, 685*d1b39d41SJosh Poimboeuf struct hlist_head *new) 686*d1b39d41SJosh Poimboeuf { 687*d1b39d41SJosh Poimboeuf new->first = old->first; 688*d1b39d41SJosh Poimboeuf if (new->first) 689*d1b39d41SJosh Poimboeuf new->first->pprev = &new->first; 690*d1b39d41SJosh Poimboeuf old->first = NULL; 691*d1b39d41SJosh Poimboeuf } 692*d1b39d41SJosh Poimboeuf 693*d1b39d41SJosh Poimboeuf #define hlist_entry(ptr, type, member) container_of(ptr,type,member) 694*d1b39d41SJosh Poimboeuf 695*d1b39d41SJosh Poimboeuf #define hlist_for_each(pos, head) \ 696*d1b39d41SJosh Poimboeuf for (pos = (head)->first; pos ; pos = pos->next) 697*d1b39d41SJosh Poimboeuf 698*d1b39d41SJosh Poimboeuf #define hlist_for_each_safe(pos, n, head) \ 699*d1b39d41SJosh Poimboeuf for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ 700*d1b39d41SJosh Poimboeuf pos = n) 701*d1b39d41SJosh Poimboeuf 702*d1b39d41SJosh Poimboeuf #define hlist_entry_safe(ptr, type, member) \ 703*d1b39d41SJosh Poimboeuf ({ typeof(ptr) ____ptr = (ptr); \ 704*d1b39d41SJosh Poimboeuf ____ptr ? hlist_entry(____ptr, type, member) : NULL; \ 705*d1b39d41SJosh Poimboeuf }) 706*d1b39d41SJosh Poimboeuf 707*d1b39d41SJosh Poimboeuf /** 708*d1b39d41SJosh Poimboeuf * hlist_for_each_entry - iterate over list of given type 709*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 710*d1b39d41SJosh Poimboeuf * @head: the head for your list. 711*d1b39d41SJosh Poimboeuf * @member: the name of the hlist_node within the struct. 712*d1b39d41SJosh Poimboeuf */ 713*d1b39d41SJosh Poimboeuf #define hlist_for_each_entry(pos, head, member) \ 714*d1b39d41SJosh Poimboeuf for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ 715*d1b39d41SJosh Poimboeuf pos; \ 716*d1b39d41SJosh Poimboeuf pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 717*d1b39d41SJosh Poimboeuf 718*d1b39d41SJosh Poimboeuf /** 719*d1b39d41SJosh Poimboeuf * hlist_for_each_entry_continue - iterate over a hlist continuing after current point 720*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 721*d1b39d41SJosh Poimboeuf * @member: the name of the hlist_node within the struct. 722*d1b39d41SJosh Poimboeuf */ 723*d1b39d41SJosh Poimboeuf #define hlist_for_each_entry_continue(pos, member) \ 724*d1b39d41SJosh Poimboeuf for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\ 725*d1b39d41SJosh Poimboeuf pos; \ 726*d1b39d41SJosh Poimboeuf pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 727*d1b39d41SJosh Poimboeuf 728*d1b39d41SJosh Poimboeuf /** 729*d1b39d41SJosh Poimboeuf * hlist_for_each_entry_from - iterate over a hlist continuing from current point 730*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 731*d1b39d41SJosh Poimboeuf * @member: the name of the hlist_node within the struct. 732*d1b39d41SJosh Poimboeuf */ 733*d1b39d41SJosh Poimboeuf #define hlist_for_each_entry_from(pos, member) \ 734*d1b39d41SJosh Poimboeuf for (; pos; \ 735*d1b39d41SJosh Poimboeuf pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) 736*d1b39d41SJosh Poimboeuf 737*d1b39d41SJosh Poimboeuf /** 738*d1b39d41SJosh Poimboeuf * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry 739*d1b39d41SJosh Poimboeuf * @pos: the type * to use as a loop cursor. 740*d1b39d41SJosh Poimboeuf * @n: another &struct hlist_node to use as temporary storage 741*d1b39d41SJosh Poimboeuf * @head: the head for your list. 742*d1b39d41SJosh Poimboeuf * @member: the name of the hlist_node within the struct. 743*d1b39d41SJosh Poimboeuf */ 744*d1b39d41SJosh Poimboeuf #define hlist_for_each_entry_safe(pos, n, head, member) \ 745*d1b39d41SJosh Poimboeuf for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\ 746*d1b39d41SJosh Poimboeuf pos && ({ n = pos->member.next; 1; }); \ 747*d1b39d41SJosh Poimboeuf pos = hlist_entry_safe(n, typeof(*pos), member)) 748*d1b39d41SJosh Poimboeuf 7494fc62a89SWang Nan /** 7504fc62a89SWang Nan * list_del_range - deletes range of entries from list. 7514fc62a89SWang Nan * @begin: first element in the range to delete from the list. 7524fc62a89SWang Nan * @end: last element in the range to delete from the list. 7534fc62a89SWang Nan * Note: list_empty on the range of entries does not return true after this, 7544fc62a89SWang Nan * the entries is in an undefined state. 7554fc62a89SWang Nan */ 7564fc62a89SWang Nan static inline void list_del_range(struct list_head *begin, 7574fc62a89SWang Nan struct list_head *end) 7584fc62a89SWang Nan { 7594fc62a89SWang Nan begin->prev->next = end->next; 7604fc62a89SWang Nan end->next->prev = begin->prev; 7614fc62a89SWang Nan } 7624fc62a89SWang Nan 7634fc62a89SWang Nan /** 7644fc62a89SWang Nan * list_for_each_from - iterate over a list from one of its nodes 7654fc62a89SWang Nan * @pos: the &struct list_head to use as a loop cursor, from where to start 7664fc62a89SWang Nan * @head: the head for your list. 7674fc62a89SWang Nan */ 7684fc62a89SWang Nan #define list_for_each_from(pos, head) \ 7694fc62a89SWang Nan for (; pos != (head); pos = pos->next) 770*d1b39d41SJosh Poimboeuf 771*d1b39d41SJosh Poimboeuf #endif /* __TOOLS_LINUX_LIST_H */ 772