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