1700a0c64SWolfgang Denk #ifndef _LINUX_LIST_H 2700a0c64SWolfgang Denk #define _LINUX_LIST_H 3700a0c64SWolfgang Denk 4ef0255fcSJean-Christophe PLAGNIOL-VILLARD #include <linux/stddef.h> 5ef0255fcSJean-Christophe PLAGNIOL-VILLARD #include <linux/poison.h> 6ef0255fcSJean-Christophe PLAGNIOL-VILLARD 7700a0c64SWolfgang Denk #ifndef ARCH_HAS_PREFETCH 8700a0c64SWolfgang Denk #define ARCH_HAS_PREFETCH 9700a0c64SWolfgang Denk static inline void prefetch(const void *x) {;} 10700a0c64SWolfgang Denk #endif 11700a0c64SWolfgang Denk 12700a0c64SWolfgang Denk /* 13700a0c64SWolfgang Denk * Simple doubly linked list implementation. 14700a0c64SWolfgang Denk * 15700a0c64SWolfgang Denk * Some of the internal functions ("__xxx") are useful when 16700a0c64SWolfgang Denk * manipulating whole lists rather than single entries, as 17700a0c64SWolfgang Denk * sometimes we already know the next/prev entries and we can 18700a0c64SWolfgang Denk * generate better code by using them directly rather than 19700a0c64SWolfgang Denk * using the generic single-entry routines. 20700a0c64SWolfgang Denk */ 21700a0c64SWolfgang Denk 22700a0c64SWolfgang Denk struct list_head { 23700a0c64SWolfgang Denk struct list_head *next, *prev; 24700a0c64SWolfgang Denk }; 25700a0c64SWolfgang Denk 26700a0c64SWolfgang Denk #define LIST_HEAD_INIT(name) { &(name), &(name) } 27700a0c64SWolfgang Denk 28700a0c64SWolfgang Denk #define LIST_HEAD(name) \ 29700a0c64SWolfgang Denk struct list_head name = LIST_HEAD_INIT(name) 30700a0c64SWolfgang Denk 31ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void INIT_LIST_HEAD(struct list_head *list) 32ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 33ef0255fcSJean-Christophe PLAGNIOL-VILLARD list->next = list; 34ef0255fcSJean-Christophe PLAGNIOL-VILLARD list->prev = list; 35ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 36700a0c64SWolfgang Denk 37700a0c64SWolfgang Denk /* 38700a0c64SWolfgang Denk * Insert a new entry between two known consecutive entries. 39700a0c64SWolfgang Denk * 40700a0c64SWolfgang Denk * This is only for internal list manipulation where we know 41700a0c64SWolfgang Denk * the prev/next entries already! 42700a0c64SWolfgang Denk */ 43700a0c64SWolfgang Denk static inline void __list_add(struct list_head *new, 44700a0c64SWolfgang Denk struct list_head *prev, 45700a0c64SWolfgang Denk struct list_head *next) 46700a0c64SWolfgang Denk { 47700a0c64SWolfgang Denk next->prev = new; 48700a0c64SWolfgang Denk new->next = next; 49700a0c64SWolfgang Denk new->prev = prev; 50700a0c64SWolfgang Denk prev->next = new; 51700a0c64SWolfgang Denk } 52700a0c64SWolfgang Denk 53700a0c64SWolfgang Denk /** 54700a0c64SWolfgang Denk * list_add - add a new entry 55700a0c64SWolfgang Denk * @new: new entry to be added 56700a0c64SWolfgang Denk * @head: list head to add it after 57700a0c64SWolfgang Denk * 58700a0c64SWolfgang Denk * Insert a new entry after the specified head. 59700a0c64SWolfgang Denk * This is good for implementing stacks. 60700a0c64SWolfgang Denk */ 61700a0c64SWolfgang Denk static inline void list_add(struct list_head *new, struct list_head *head) 62700a0c64SWolfgang Denk { 63700a0c64SWolfgang Denk __list_add(new, head, head->next); 64700a0c64SWolfgang Denk } 65700a0c64SWolfgang Denk 66700a0c64SWolfgang Denk /** 67700a0c64SWolfgang Denk * list_add_tail - add a new entry 68700a0c64SWolfgang Denk * @new: new entry to be added 69700a0c64SWolfgang Denk * @head: list head to add it before 70700a0c64SWolfgang Denk * 71700a0c64SWolfgang Denk * Insert a new entry before the specified head. 72700a0c64SWolfgang Denk * This is useful for implementing queues. 73700a0c64SWolfgang Denk */ 74700a0c64SWolfgang Denk static inline void list_add_tail(struct list_head *new, struct list_head *head) 75700a0c64SWolfgang Denk { 76700a0c64SWolfgang Denk __list_add(new, head->prev, head); 77700a0c64SWolfgang Denk } 78700a0c64SWolfgang Denk 79700a0c64SWolfgang Denk /* 80700a0c64SWolfgang Denk * Delete a list entry by making the prev/next entries 81700a0c64SWolfgang Denk * point to each other. 82700a0c64SWolfgang Denk * 83700a0c64SWolfgang Denk * This is only for internal list manipulation where we know 84700a0c64SWolfgang Denk * the prev/next entries already! 85700a0c64SWolfgang Denk */ 86700a0c64SWolfgang Denk static inline void __list_del(struct list_head *prev, struct list_head *next) 87700a0c64SWolfgang Denk { 88700a0c64SWolfgang Denk next->prev = prev; 89700a0c64SWolfgang Denk prev->next = next; 90700a0c64SWolfgang Denk } 91700a0c64SWolfgang Denk 92700a0c64SWolfgang Denk /** 93700a0c64SWolfgang Denk * list_del - deletes entry from list. 94700a0c64SWolfgang Denk * @entry: the element to delete from the list. 95ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Note: list_empty() on entry does not return true after this, the entry is 96ef0255fcSJean-Christophe PLAGNIOL-VILLARD * in an undefined state. 97700a0c64SWolfgang Denk */ 98700a0c64SWolfgang Denk static inline void list_del(struct list_head *entry) 99700a0c64SWolfgang Denk { 100700a0c64SWolfgang Denk __list_del(entry->prev, entry->next); 101ef0255fcSJean-Christophe PLAGNIOL-VILLARD entry->next = LIST_POISON1; 102ef0255fcSJean-Christophe PLAGNIOL-VILLARD entry->prev = LIST_POISON2; 103ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 104ef0255fcSJean-Christophe PLAGNIOL-VILLARD 105ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 106ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_replace - replace old entry by new one 107ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @old : the element to be replaced 108ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @new : the new element to insert 109ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 110ef0255fcSJean-Christophe PLAGNIOL-VILLARD * If @old was empty, it will be overwritten. 111ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 112ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_replace(struct list_head *old, 113ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *new) 114ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 115ef0255fcSJean-Christophe PLAGNIOL-VILLARD new->next = old->next; 116ef0255fcSJean-Christophe PLAGNIOL-VILLARD new->next->prev = new; 117ef0255fcSJean-Christophe PLAGNIOL-VILLARD new->prev = old->prev; 118ef0255fcSJean-Christophe PLAGNIOL-VILLARD new->prev->next = new; 119ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 120ef0255fcSJean-Christophe PLAGNIOL-VILLARD 121ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_replace_init(struct list_head *old, 122ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *new) 123ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 124ef0255fcSJean-Christophe PLAGNIOL-VILLARD list_replace(old, new); 125ef0255fcSJean-Christophe PLAGNIOL-VILLARD INIT_LIST_HEAD(old); 126700a0c64SWolfgang Denk } 127700a0c64SWolfgang Denk 128700a0c64SWolfgang Denk /** 129700a0c64SWolfgang Denk * list_del_init - deletes entry from list and reinitialize it. 130700a0c64SWolfgang Denk * @entry: the element to delete from the list. 131700a0c64SWolfgang Denk */ 132700a0c64SWolfgang Denk static inline void list_del_init(struct list_head *entry) 133700a0c64SWolfgang Denk { 134700a0c64SWolfgang Denk __list_del(entry->prev, entry->next); 135700a0c64SWolfgang Denk INIT_LIST_HEAD(entry); 136700a0c64SWolfgang Denk } 137700a0c64SWolfgang Denk 138700a0c64SWolfgang Denk /** 139700a0c64SWolfgang Denk * list_move - delete from one list and add as another's head 140700a0c64SWolfgang Denk * @list: the entry to move 141700a0c64SWolfgang Denk * @head: the head that will precede our entry 142700a0c64SWolfgang Denk */ 143700a0c64SWolfgang Denk static inline void list_move(struct list_head *list, struct list_head *head) 144700a0c64SWolfgang Denk { 145700a0c64SWolfgang Denk __list_del(list->prev, list->next); 146700a0c64SWolfgang Denk list_add(list, head); 147700a0c64SWolfgang Denk } 148700a0c64SWolfgang Denk 149700a0c64SWolfgang Denk /** 150700a0c64SWolfgang Denk * list_move_tail - delete from one list and add as another's tail 151700a0c64SWolfgang Denk * @list: the entry to move 152700a0c64SWolfgang Denk * @head: the head that will follow our entry 153700a0c64SWolfgang Denk */ 154700a0c64SWolfgang Denk static inline void list_move_tail(struct list_head *list, 155700a0c64SWolfgang Denk struct list_head *head) 156700a0c64SWolfgang Denk { 157700a0c64SWolfgang Denk __list_del(list->prev, list->next); 158700a0c64SWolfgang Denk list_add_tail(list, head); 159700a0c64SWolfgang Denk } 160700a0c64SWolfgang Denk 161700a0c64SWolfgang Denk /** 162ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_is_last - tests whether @list is the last entry in list @head 163ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @list: the entry to test 164ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head of the list 165ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 166ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int list_is_last(const struct list_head *list, 167ef0255fcSJean-Christophe PLAGNIOL-VILLARD const struct list_head *head) 168ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 169ef0255fcSJean-Christophe PLAGNIOL-VILLARD return list->next == head; 170ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 171ef0255fcSJean-Christophe PLAGNIOL-VILLARD 172ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 173700a0c64SWolfgang Denk * list_empty - tests whether a list is empty 174700a0c64SWolfgang Denk * @head: the list to test. 175700a0c64SWolfgang Denk */ 176ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int list_empty(const struct list_head *head) 177700a0c64SWolfgang Denk { 178700a0c64SWolfgang Denk return head->next == head; 179700a0c64SWolfgang Denk } 180700a0c64SWolfgang Denk 181ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 182ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_empty_careful - tests whether a list is empty and not being modified 183ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the list to test 184ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 185ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Description: 186ef0255fcSJean-Christophe PLAGNIOL-VILLARD * tests whether a list is empty _and_ checks that no other CPU might be 187ef0255fcSJean-Christophe PLAGNIOL-VILLARD * in the process of modifying either member (next or prev) 188ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 189ef0255fcSJean-Christophe PLAGNIOL-VILLARD * NOTE: using list_empty_careful() without synchronization 190ef0255fcSJean-Christophe PLAGNIOL-VILLARD * can only be safe if the only activity that can happen 191ef0255fcSJean-Christophe PLAGNIOL-VILLARD * to the list entry is list_del_init(). Eg. it cannot be used 192ef0255fcSJean-Christophe PLAGNIOL-VILLARD * if another CPU could re-list_add() it. 193ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 194ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int list_empty_careful(const struct list_head *head) 195700a0c64SWolfgang Denk { 196ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *next = head->next; 197ef0255fcSJean-Christophe PLAGNIOL-VILLARD return (next == head) && (next == head->prev); 198700a0c64SWolfgang Denk } 199700a0c64SWolfgang Denk 200700a0c64SWolfgang Denk /** 201ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_is_singular - tests whether a list has just one entry. 202ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the list to test. 203ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 204ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int list_is_singular(const struct list_head *head) 205ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 206ef0255fcSJean-Christophe PLAGNIOL-VILLARD return !list_empty(head) && (head->next == head->prev); 207ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 208ef0255fcSJean-Christophe PLAGNIOL-VILLARD 209ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void __list_cut_position(struct list_head *list, 210ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *head, struct list_head *entry) 211ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 212ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *new_first = entry->next; 213ef0255fcSJean-Christophe PLAGNIOL-VILLARD list->next = head->next; 214ef0255fcSJean-Christophe PLAGNIOL-VILLARD list->next->prev = list; 215ef0255fcSJean-Christophe PLAGNIOL-VILLARD list->prev = entry; 216ef0255fcSJean-Christophe PLAGNIOL-VILLARD entry->next = list; 217ef0255fcSJean-Christophe PLAGNIOL-VILLARD head->next = new_first; 218ef0255fcSJean-Christophe PLAGNIOL-VILLARD new_first->prev = head; 219ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 220ef0255fcSJean-Christophe PLAGNIOL-VILLARD 221ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 222ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_cut_position - cut a list into two 223ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @list: a new list to add all removed entries 224ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: a list with entries 225ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @entry: an entry within head, could be the head itself 226ef0255fcSJean-Christophe PLAGNIOL-VILLARD * and if so we won't cut the list 227ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 228ef0255fcSJean-Christophe PLAGNIOL-VILLARD * This helper moves the initial part of @head, up to and 229ef0255fcSJean-Christophe PLAGNIOL-VILLARD * including @entry, from @head to @list. You should 230ef0255fcSJean-Christophe PLAGNIOL-VILLARD * pass on @entry an element you know is on @head. @list 231ef0255fcSJean-Christophe PLAGNIOL-VILLARD * should be an empty list or a list you do not care about 232ef0255fcSJean-Christophe PLAGNIOL-VILLARD * losing its data. 233ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 234ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 235ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_cut_position(struct list_head *list, 236ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *head, struct list_head *entry) 237ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 238ef0255fcSJean-Christophe PLAGNIOL-VILLARD if (list_empty(head)) 239ef0255fcSJean-Christophe PLAGNIOL-VILLARD return; 240ef0255fcSJean-Christophe PLAGNIOL-VILLARD if (list_is_singular(head) && 241ef0255fcSJean-Christophe PLAGNIOL-VILLARD (head->next != entry && head != entry)) 242ef0255fcSJean-Christophe PLAGNIOL-VILLARD return; 243ef0255fcSJean-Christophe PLAGNIOL-VILLARD if (entry == head) 244ef0255fcSJean-Christophe PLAGNIOL-VILLARD INIT_LIST_HEAD(list); 245ef0255fcSJean-Christophe PLAGNIOL-VILLARD else 246ef0255fcSJean-Christophe PLAGNIOL-VILLARD __list_cut_position(list, head, entry); 247ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 248ef0255fcSJean-Christophe PLAGNIOL-VILLARD 249ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void __list_splice(const struct list_head *list, 250ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *prev, 251ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *next) 252ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 253ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *first = list->next; 254ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *last = list->prev; 255ef0255fcSJean-Christophe PLAGNIOL-VILLARD 256ef0255fcSJean-Christophe PLAGNIOL-VILLARD first->prev = prev; 257ef0255fcSJean-Christophe PLAGNIOL-VILLARD prev->next = first; 258ef0255fcSJean-Christophe PLAGNIOL-VILLARD 259ef0255fcSJean-Christophe PLAGNIOL-VILLARD last->next = next; 260ef0255fcSJean-Christophe PLAGNIOL-VILLARD next->prev = last; 261ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 262ef0255fcSJean-Christophe PLAGNIOL-VILLARD 263ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 264ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_splice - join two lists, this is designed for stacks 265700a0c64SWolfgang Denk * @list: the new list to add. 266700a0c64SWolfgang Denk * @head: the place to add it in the first list. 267700a0c64SWolfgang Denk */ 268ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_splice(const struct list_head *list, 269ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *head) 270700a0c64SWolfgang Denk { 271700a0c64SWolfgang Denk if (!list_empty(list)) 272ef0255fcSJean-Christophe PLAGNIOL-VILLARD __list_splice(list, head, head->next); 273ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 274ef0255fcSJean-Christophe PLAGNIOL-VILLARD 275ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 276ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_splice_tail - join two lists, each list being a queue 277ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @list: the new list to add. 278ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the place to add it in the first list. 279ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 280ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_splice_tail(struct list_head *list, 281ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *head) 282ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 283ef0255fcSJean-Christophe PLAGNIOL-VILLARD if (!list_empty(list)) 284ef0255fcSJean-Christophe PLAGNIOL-VILLARD __list_splice(list, head->prev, head); 285700a0c64SWolfgang Denk } 286700a0c64SWolfgang Denk 287700a0c64SWolfgang Denk /** 288700a0c64SWolfgang Denk * list_splice_init - join two lists and reinitialise the emptied list. 289700a0c64SWolfgang Denk * @list: the new list to add. 290700a0c64SWolfgang Denk * @head: the place to add it in the first list. 291700a0c64SWolfgang Denk * 292700a0c64SWolfgang Denk * The list at @list is reinitialised 293700a0c64SWolfgang Denk */ 294700a0c64SWolfgang Denk static inline void list_splice_init(struct list_head *list, 295700a0c64SWolfgang Denk struct list_head *head) 296700a0c64SWolfgang Denk { 297700a0c64SWolfgang Denk if (!list_empty(list)) { 298ef0255fcSJean-Christophe PLAGNIOL-VILLARD __list_splice(list, head, head->next); 299ef0255fcSJean-Christophe PLAGNIOL-VILLARD INIT_LIST_HEAD(list); 300ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 301ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 302ef0255fcSJean-Christophe PLAGNIOL-VILLARD 303ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 304ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_splice_tail_init - join two lists and reinitialise the emptied list 305ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @list: the new list to add. 306ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the place to add it in the first list. 307ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 308ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Each of the lists is a queue. 309ef0255fcSJean-Christophe PLAGNIOL-VILLARD * The list at @list is reinitialised 310ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 311ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void list_splice_tail_init(struct list_head *list, 312ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct list_head *head) 313ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 314ef0255fcSJean-Christophe PLAGNIOL-VILLARD if (!list_empty(list)) { 315ef0255fcSJean-Christophe PLAGNIOL-VILLARD __list_splice(list, head->prev, head); 316700a0c64SWolfgang Denk INIT_LIST_HEAD(list); 317700a0c64SWolfgang Denk } 318700a0c64SWolfgang Denk } 319700a0c64SWolfgang Denk 320700a0c64SWolfgang Denk /** 321700a0c64SWolfgang Denk * list_entry - get the struct for this entry 322700a0c64SWolfgang Denk * @ptr: the &struct list_head pointer. 323700a0c64SWolfgang Denk * @type: the type of the struct this is embedded in. 324700a0c64SWolfgang Denk * @member: the name of the list_struct within the struct. 325700a0c64SWolfgang Denk */ 326700a0c64SWolfgang Denk #define list_entry(ptr, type, member) \ 327ef0255fcSJean-Christophe PLAGNIOL-VILLARD container_of(ptr, type, member) 328ef0255fcSJean-Christophe PLAGNIOL-VILLARD 329ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 330ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_first_entry - get the first element from a list 331ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @ptr: the list head to take the element from. 332ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @type: the type of the struct this is embedded in. 333ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the list_struct within the struct. 334ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 335ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Note, that list is expected to be not empty. 336ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 337ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_first_entry(ptr, type, member) \ 338ef0255fcSJean-Christophe PLAGNIOL-VILLARD list_entry((ptr)->next, type, member) 339700a0c64SWolfgang Denk 340700a0c64SWolfgang Denk /** 341*5023bd7aSSimon Glass * list_last_entry - get the last element from a list 342*5023bd7aSSimon Glass * @ptr: the list head to take the element from. 343*5023bd7aSSimon Glass * @type: the type of the struct this is embedded in. 344*5023bd7aSSimon Glass * @member: the name of the list_struct within the struct. 345*5023bd7aSSimon Glass * 346*5023bd7aSSimon Glass * Note, that list is expected to be not empty. 347*5023bd7aSSimon Glass */ 348*5023bd7aSSimon Glass #define list_last_entry(ptr, type, member) \ 349*5023bd7aSSimon Glass list_entry((ptr)->prev, type, member) 350*5023bd7aSSimon Glass 351*5023bd7aSSimon Glass /** 352700a0c64SWolfgang Denk * list_for_each - iterate over a list 353ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct list_head to use as a loop cursor. 354700a0c64SWolfgang Denk * @head: the head for your list. 355700a0c64SWolfgang Denk */ 356700a0c64SWolfgang Denk #define list_for_each(pos, head) \ 357ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (head)->next; prefetch(pos->next), pos != (head); \ 358ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = pos->next) 359ef0255fcSJean-Christophe PLAGNIOL-VILLARD 360ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 361ef0255fcSJean-Christophe PLAGNIOL-VILLARD * __list_for_each - iterate over a list 362ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct list_head to use as a loop cursor. 363ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 364ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 365ef0255fcSJean-Christophe PLAGNIOL-VILLARD * This variant differs from list_for_each() in that it's the 366ef0255fcSJean-Christophe PLAGNIOL-VILLARD * simplest possible list iteration code, no prefetching is done. 367ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Use this for code that knows the list to be very short (empty 368ef0255fcSJean-Christophe PLAGNIOL-VILLARD * or 1 entry) most of the time. 369ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 370ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define __list_for_each(pos, head) \ 371ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (head)->next; pos != (head); pos = pos->next) 372ef0255fcSJean-Christophe PLAGNIOL-VILLARD 373700a0c64SWolfgang Denk /** 374700a0c64SWolfgang Denk * list_for_each_prev - iterate over a list backwards 375ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct list_head to use as a loop cursor. 376700a0c64SWolfgang Denk * @head: the head for your list. 377700a0c64SWolfgang Denk */ 378700a0c64SWolfgang Denk #define list_for_each_prev(pos, head) \ 379ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ 380ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = pos->prev) 381700a0c64SWolfgang Denk 382700a0c64SWolfgang Denk /** 383700a0c64SWolfgang Denk * list_for_each_safe - iterate over a list safe against removal of list entry 384ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct list_head to use as a loop cursor. 385700a0c64SWolfgang Denk * @n: another &struct list_head to use as temporary storage 386700a0c64SWolfgang Denk * @head: the head for your list. 387700a0c64SWolfgang Denk */ 388700a0c64SWolfgang Denk #define list_for_each_safe(pos, n, head) \ 389700a0c64SWolfgang Denk for (pos = (head)->next, n = pos->next; pos != (head); \ 390700a0c64SWolfgang Denk pos = n, n = pos->next) 391700a0c64SWolfgang Denk 392700a0c64SWolfgang Denk /** 393ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry 394ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct list_head to use as a loop cursor. 395ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @n: another &struct list_head to use as temporary storage 396ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 397ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 398ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_prev_safe(pos, n, head) \ 399ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (head)->prev, n = pos->prev; \ 400ef0255fcSJean-Christophe PLAGNIOL-VILLARD prefetch(pos->prev), pos != (head); \ 401ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = n, n = pos->prev) 402ef0255fcSJean-Christophe PLAGNIOL-VILLARD 403ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 404700a0c64SWolfgang Denk * list_for_each_entry - iterate over list of given type 405ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 406700a0c64SWolfgang Denk * @head: the head for your list. 407700a0c64SWolfgang Denk * @member: the name of the list_struct within the struct. 408700a0c64SWolfgang Denk */ 409700a0c64SWolfgang Denk #define list_for_each_entry(pos, head, member) \ 410ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = list_entry((head)->next, typeof(*pos), member); \ 411ef0255fcSJean-Christophe PLAGNIOL-VILLARD prefetch(pos->member.next), &pos->member != (head); \ 412ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = list_entry(pos->member.next, typeof(*pos), member)) 413ef0255fcSJean-Christophe PLAGNIOL-VILLARD 414ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 415ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_for_each_entry_reverse - iterate backwards over list of given type. 416ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 417ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 418ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the list_struct within the struct. 419ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 420ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_reverse(pos, head, member) \ 421ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = list_entry((head)->prev, typeof(*pos), member); \ 422ef0255fcSJean-Christophe PLAGNIOL-VILLARD prefetch(pos->member.prev), &pos->member != (head); \ 423ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = list_entry(pos->member.prev, typeof(*pos), member)) 424ef0255fcSJean-Christophe PLAGNIOL-VILLARD 425ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 426ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() 427ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a start point 428ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head of the list 429ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the list_struct within the struct. 430ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 431ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). 432ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 433ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_prepare_entry(pos, head, member) \ 434ef0255fcSJean-Christophe PLAGNIOL-VILLARD ((pos) ? : list_entry(head, typeof(*pos), member)) 435ef0255fcSJean-Christophe PLAGNIOL-VILLARD 436ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 437ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_for_each_entry_continue - continue iteration over list of given type 438ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 439ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 440ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the list_struct within the struct. 441ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 442ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Continue to iterate over list of given type, continuing after 443ef0255fcSJean-Christophe PLAGNIOL-VILLARD * the current position. 444ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 445ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_continue(pos, head, member) \ 446ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = list_entry(pos->member.next, typeof(*pos), member); \ 447ef0255fcSJean-Christophe PLAGNIOL-VILLARD prefetch(pos->member.next), &pos->member != (head); \ 448ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = list_entry(pos->member.next, typeof(*pos), member)) 449ef0255fcSJean-Christophe PLAGNIOL-VILLARD 450ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 451ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_for_each_entry_continue_reverse - iterate backwards from the given point 452ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 453ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 454ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the list_struct within the struct. 455ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 456ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Start to iterate over list of given type backwards, continuing after 457ef0255fcSJean-Christophe PLAGNIOL-VILLARD * the current position. 458ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 459ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_continue_reverse(pos, head, member) \ 460ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = list_entry(pos->member.prev, typeof(*pos), member); \ 461ef0255fcSJean-Christophe PLAGNIOL-VILLARD prefetch(pos->member.prev), &pos->member != (head); \ 462ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = list_entry(pos->member.prev, typeof(*pos), member)) 463ef0255fcSJean-Christophe PLAGNIOL-VILLARD 464ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 465ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_for_each_entry_from - iterate over list of given type from the current point 466ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 467ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 468ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the list_struct within the struct. 469ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 470ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Iterate over list of given type, continuing from current position. 471ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 472ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_from(pos, head, member) \ 473ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (; prefetch(pos->member.next), &pos->member != (head); \ 474ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = list_entry(pos->member.next, typeof(*pos), member)) 475700a0c64SWolfgang Denk 476700a0c64SWolfgang Denk /** 477700a0c64SWolfgang Denk * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 478ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 479700a0c64SWolfgang Denk * @n: another type * to use as temporary storage 480700a0c64SWolfgang Denk * @head: the head for your list. 481700a0c64SWolfgang Denk * @member: the name of the list_struct within the struct. 482700a0c64SWolfgang Denk */ 483700a0c64SWolfgang Denk #define list_for_each_entry_safe(pos, n, head, member) \ 484700a0c64SWolfgang Denk for (pos = list_entry((head)->next, typeof(*pos), member), \ 485700a0c64SWolfgang Denk n = list_entry(pos->member.next, typeof(*pos), member); \ 486700a0c64SWolfgang Denk &pos->member != (head); \ 487700a0c64SWolfgang Denk pos = n, n = list_entry(n->member.next, typeof(*n), member)) 488700a0c64SWolfgang Denk 489700a0c64SWolfgang Denk /** 490ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_for_each_entry_safe_continue 491ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 492ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @n: another type * to use as temporary storage 493700a0c64SWolfgang Denk * @head: the head for your list. 494700a0c64SWolfgang Denk * @member: the name of the list_struct within the struct. 495ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 496ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Iterate over list of given type, continuing after current point, 497ef0255fcSJean-Christophe PLAGNIOL-VILLARD * safe against removal of list entry. 498700a0c64SWolfgang Denk */ 499ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_safe_continue(pos, n, head, member) \ 500700a0c64SWolfgang Denk for (pos = list_entry(pos->member.next, typeof(*pos), member), \ 501ef0255fcSJean-Christophe PLAGNIOL-VILLARD n = list_entry(pos->member.next, typeof(*pos), member); \ 502700a0c64SWolfgang Denk &pos->member != (head); \ 503ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = n, n = list_entry(n->member.next, typeof(*n), member)) 504ef0255fcSJean-Christophe PLAGNIOL-VILLARD 505ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 506ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_for_each_entry_safe_from 507ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 508ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @n: another type * to use as temporary storage 509ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 510ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the list_struct within the struct. 511ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 512ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Iterate over list of given type from current point, safe against 513ef0255fcSJean-Christophe PLAGNIOL-VILLARD * removal of list entry. 514ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 515ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_safe_from(pos, n, head, member) \ 516ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (n = list_entry(pos->member.next, typeof(*pos), member); \ 517ef0255fcSJean-Christophe PLAGNIOL-VILLARD &pos->member != (head); \ 518ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = n, n = list_entry(n->member.next, typeof(*n), member)) 519ef0255fcSJean-Christophe PLAGNIOL-VILLARD 520ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 521ef0255fcSJean-Christophe PLAGNIOL-VILLARD * list_for_each_entry_safe_reverse 522ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the type * to use as a loop cursor. 523ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @n: another type * to use as temporary storage 524ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 525ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the list_struct within the struct. 526ef0255fcSJean-Christophe PLAGNIOL-VILLARD * 527ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Iterate backwards over list of given type, safe against removal 528ef0255fcSJean-Christophe PLAGNIOL-VILLARD * of list entry. 529ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 530ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define list_for_each_entry_safe_reverse(pos, n, head, member) \ 531ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = list_entry((head)->prev, typeof(*pos), member), \ 532ef0255fcSJean-Christophe PLAGNIOL-VILLARD n = list_entry(pos->member.prev, typeof(*pos), member); \ 533ef0255fcSJean-Christophe PLAGNIOL-VILLARD &pos->member != (head); \ 534ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = n, n = list_entry(n->member.prev, typeof(*n), member)) 535ef0255fcSJean-Christophe PLAGNIOL-VILLARD 536ef0255fcSJean-Christophe PLAGNIOL-VILLARD /* 537ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Double linked lists with a single pointer list head. 538ef0255fcSJean-Christophe PLAGNIOL-VILLARD * Mostly useful for hash tables where the two pointer list head is 539ef0255fcSJean-Christophe PLAGNIOL-VILLARD * too wasteful. 540ef0255fcSJean-Christophe PLAGNIOL-VILLARD * You lose the ability to access the tail in O(1). 541ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 542ef0255fcSJean-Christophe PLAGNIOL-VILLARD 543ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_head { 544ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node *first; 545ef0255fcSJean-Christophe PLAGNIOL-VILLARD }; 546ef0255fcSJean-Christophe PLAGNIOL-VILLARD 547ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node { 548ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node *next, **pprev; 549ef0255fcSJean-Christophe PLAGNIOL-VILLARD }; 550ef0255fcSJean-Christophe PLAGNIOL-VILLARD 551ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define HLIST_HEAD_INIT { .first = NULL } 552ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } 553ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) 554ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void INIT_HLIST_NODE(struct hlist_node *h) 555ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 556ef0255fcSJean-Christophe PLAGNIOL-VILLARD h->next = NULL; 557ef0255fcSJean-Christophe PLAGNIOL-VILLARD h->pprev = NULL; 558ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 559ef0255fcSJean-Christophe PLAGNIOL-VILLARD 560ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int hlist_unhashed(const struct hlist_node *h) 561ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 562ef0255fcSJean-Christophe PLAGNIOL-VILLARD return !h->pprev; 563ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 564ef0255fcSJean-Christophe PLAGNIOL-VILLARD 565ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline int hlist_empty(const struct hlist_head *h) 566ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 567ef0255fcSJean-Christophe PLAGNIOL-VILLARD return !h->first; 568ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 569ef0255fcSJean-Christophe PLAGNIOL-VILLARD 570ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void __hlist_del(struct hlist_node *n) 571ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 572ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node *next = n->next; 573ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node **pprev = n->pprev; 574ef0255fcSJean-Christophe PLAGNIOL-VILLARD *pprev = next; 575ef0255fcSJean-Christophe PLAGNIOL-VILLARD if (next) 576ef0255fcSJean-Christophe PLAGNIOL-VILLARD next->pprev = pprev; 577ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 578ef0255fcSJean-Christophe PLAGNIOL-VILLARD 579ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_del(struct hlist_node *n) 580ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 581ef0255fcSJean-Christophe PLAGNIOL-VILLARD __hlist_del(n); 582ef0255fcSJean-Christophe PLAGNIOL-VILLARD n->next = LIST_POISON1; 583ef0255fcSJean-Christophe PLAGNIOL-VILLARD n->pprev = LIST_POISON2; 584ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 585ef0255fcSJean-Christophe PLAGNIOL-VILLARD 586ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_del_init(struct hlist_node *n) 587ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 588ef0255fcSJean-Christophe PLAGNIOL-VILLARD if (!hlist_unhashed(n)) { 589ef0255fcSJean-Christophe PLAGNIOL-VILLARD __hlist_del(n); 590ef0255fcSJean-Christophe PLAGNIOL-VILLARD INIT_HLIST_NODE(n); 591ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 592ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 593ef0255fcSJean-Christophe PLAGNIOL-VILLARD 594ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) 595ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 596ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node *first = h->first; 597ef0255fcSJean-Christophe PLAGNIOL-VILLARD n->next = first; 598ef0255fcSJean-Christophe PLAGNIOL-VILLARD if (first) 599ef0255fcSJean-Christophe PLAGNIOL-VILLARD first->pprev = &n->next; 600ef0255fcSJean-Christophe PLAGNIOL-VILLARD h->first = n; 601ef0255fcSJean-Christophe PLAGNIOL-VILLARD n->pprev = &h->first; 602ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 603ef0255fcSJean-Christophe PLAGNIOL-VILLARD 604ef0255fcSJean-Christophe PLAGNIOL-VILLARD /* next must be != NULL */ 605ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_add_before(struct hlist_node *n, 606ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node *next) 607ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 608ef0255fcSJean-Christophe PLAGNIOL-VILLARD n->pprev = next->pprev; 609ef0255fcSJean-Christophe PLAGNIOL-VILLARD n->next = next; 610ef0255fcSJean-Christophe PLAGNIOL-VILLARD next->pprev = &n->next; 611ef0255fcSJean-Christophe PLAGNIOL-VILLARD *(n->pprev) = n; 612ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 613ef0255fcSJean-Christophe PLAGNIOL-VILLARD 614ef0255fcSJean-Christophe PLAGNIOL-VILLARD static inline void hlist_add_after(struct hlist_node *n, 615ef0255fcSJean-Christophe PLAGNIOL-VILLARD struct hlist_node *next) 616ef0255fcSJean-Christophe PLAGNIOL-VILLARD { 617ef0255fcSJean-Christophe PLAGNIOL-VILLARD next->next = n->next; 618ef0255fcSJean-Christophe PLAGNIOL-VILLARD n->next = next; 619ef0255fcSJean-Christophe PLAGNIOL-VILLARD next->pprev = &n->next; 620ef0255fcSJean-Christophe PLAGNIOL-VILLARD 621ef0255fcSJean-Christophe PLAGNIOL-VILLARD if(next->next) 622ef0255fcSJean-Christophe PLAGNIOL-VILLARD next->next->pprev = &next->next; 623ef0255fcSJean-Christophe PLAGNIOL-VILLARD } 624ef0255fcSJean-Christophe PLAGNIOL-VILLARD 625ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_entry(ptr, type, member) container_of(ptr,type,member) 626ef0255fcSJean-Christophe PLAGNIOL-VILLARD 627ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each(pos, head) \ 628ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ 629ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = pos->next) 630ef0255fcSJean-Christophe PLAGNIOL-VILLARD 631ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_safe(pos, n, head) \ 632ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ 633ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = n) 634ef0255fcSJean-Christophe PLAGNIOL-VILLARD 635ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 636ef0255fcSJean-Christophe PLAGNIOL-VILLARD * hlist_for_each_entry - iterate over list of given type 637ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @tpos: the type * to use as a loop cursor. 638ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct hlist_node to use as a loop cursor. 639ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 640ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the hlist_node within the struct. 641ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 642ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_entry(tpos, pos, head, member) \ 643ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (head)->first; \ 644ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos && ({ prefetch(pos->next); 1;}) && \ 645ef0255fcSJean-Christophe PLAGNIOL-VILLARD ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 646ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = pos->next) 647ef0255fcSJean-Christophe PLAGNIOL-VILLARD 648ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 649ef0255fcSJean-Christophe PLAGNIOL-VILLARD * hlist_for_each_entry_continue - iterate over a hlist continuing after current point 650ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @tpos: the type * to use as a loop cursor. 651ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct hlist_node to use as a loop cursor. 652ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the hlist_node within the struct. 653ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 654ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_entry_continue(tpos, pos, member) \ 655ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (pos)->next; \ 656ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos && ({ prefetch(pos->next); 1;}) && \ 657ef0255fcSJean-Christophe PLAGNIOL-VILLARD ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 658ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = pos->next) 659ef0255fcSJean-Christophe PLAGNIOL-VILLARD 660ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 661ef0255fcSJean-Christophe PLAGNIOL-VILLARD * hlist_for_each_entry_from - iterate over a hlist continuing from current point 662ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @tpos: the type * to use as a loop cursor. 663ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct hlist_node to use as a loop cursor. 664ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the hlist_node within the struct. 665ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 666ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_entry_from(tpos, pos, member) \ 667ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (; pos && ({ prefetch(pos->next); 1;}) && \ 668ef0255fcSJean-Christophe PLAGNIOL-VILLARD ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 669ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = pos->next) 670ef0255fcSJean-Christophe PLAGNIOL-VILLARD 671ef0255fcSJean-Christophe PLAGNIOL-VILLARD /** 672ef0255fcSJean-Christophe PLAGNIOL-VILLARD * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry 673ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @tpos: the type * to use as a loop cursor. 674ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @pos: the &struct hlist_node to use as a loop cursor. 675ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @n: another &struct hlist_node to use as temporary storage 676ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @head: the head for your list. 677ef0255fcSJean-Christophe PLAGNIOL-VILLARD * @member: the name of the hlist_node within the struct. 678ef0255fcSJean-Christophe PLAGNIOL-VILLARD */ 679ef0255fcSJean-Christophe PLAGNIOL-VILLARD #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ 680ef0255fcSJean-Christophe PLAGNIOL-VILLARD for (pos = (head)->first; \ 681ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos && ({ n = pos->next; 1; }) && \ 682ef0255fcSJean-Christophe PLAGNIOL-VILLARD ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ 683ef0255fcSJean-Christophe PLAGNIOL-VILLARD pos = n) 684700a0c64SWolfgang Denk 685700a0c64SWolfgang Denk #endif 686