1 #ifndef _LINUX_LIST_H 2 #define _LINUX_LIST_H 3 4 5 /* 6 * Simple doubly linked list implementation. 7 * 8 * Some of the internal functions ("__xxx") are useful when 9 * manipulating whole lists rather than single entries, as 10 * sometimes we already know the next/prev entries and we can 11 * generate better code by using them directly rather than 12 * using the generic single-entry routines. 13 */ 14 15 struct list_head { 16 struct list_head *next, *prev; 17 }; 18 19 #define LIST_HEAD_INIT(name) { &(name), &(name) } 20 21 #define LIST_HEAD(name) \ 22 struct list_head name = LIST_HEAD_INIT(name) 23 24 #define INIT_LIST_HEAD(ptr) do { \ 25 (ptr)->next = (ptr); (ptr)->prev = (ptr); \ 26 } while (0) 27 28 29 /* 30 * Insert a new entry between two known consecutive entries. 31 * 32 * This is only for internal list manipulation where we know 33 * the prev/next entries already! 34 */ __list_add(struct list_head * new,struct list_head * prev,struct list_head * next)35 static inline void __list_add(struct list_head *new, 36 struct list_head *prev, 37 struct list_head *next) 38 { 39 next->prev = new; 40 new->next = next; 41 new->prev = prev; 42 prev->next = new; 43 } 44 45 /** 46 * list_add - add a new entry 47 * @new: new entry to be added 48 * @head: list head to add it after 49 * 50 * Insert a new entry after the specified head. 51 * This is good for implementing stacks. 52 */ list_add(struct list_head * new,struct list_head * head)53 static inline void list_add(struct list_head *new, struct list_head *head) 54 { 55 __list_add(new, head, head->next); 56 } 57 58 /** 59 * list_add_tail - add a new entry 60 * @new: new entry to be added 61 * @head: list head to add it before 62 * 63 * Insert a new entry before the specified head. 64 * This is useful for implementing queues. 65 */ list_add_tail(struct list_head * new,struct list_head * head)66 static inline void list_add_tail(struct list_head *new, struct list_head *head) 67 { 68 __list_add(new, head->prev, head); 69 } 70 71 /* 72 * Delete a list entry by making the prev/next entries 73 * point to each other. 74 * 75 * This is only for internal list manipulation where we know 76 * the prev/next entries already! 77 */ __list_del(struct list_head * prev,struct list_head * next)78 static inline void __list_del(struct list_head *prev, struct list_head *next) 79 { 80 next->prev = prev; 81 prev->next = next; 82 } 83 84 /** 85 * list_del - deletes entry from list. 86 * @entry: the element to delete from the list. 87 * Note: list_empty on entry does not return true after this, the entry is in an undefined state. 88 */ list_del(struct list_head * entry)89 static inline void list_del(struct list_head *entry) 90 { 91 __list_del(entry->prev, entry->next); 92 entry->next = (void *) 0; 93 entry->prev = (void *) 0; 94 } 95 96 /** 97 * list_del_init - deletes entry from list and reinitialize it. 98 * @entry: the element to delete from the list. 99 */ list_del_init(struct list_head * entry)100 static inline void list_del_init(struct list_head *entry) 101 { 102 __list_del(entry->prev, entry->next); 103 INIT_LIST_HEAD(entry); 104 } 105 106 /** 107 * list_move - delete from one list and add as another's head 108 * @list: the entry to move 109 * @head: the head that will precede our entry 110 */ list_move(struct list_head * list,struct list_head * head)111 static inline void list_move(struct list_head *list, struct list_head *head) 112 { 113 __list_del(list->prev, list->next); 114 list_add(list, head); 115 } 116 117 /** 118 * list_move_tail - delete from one list and add as another's tail 119 * @list: the entry to move 120 * @head: the head that will follow our entry 121 */ list_move_tail(struct list_head * list,struct list_head * head)122 static inline void list_move_tail(struct list_head *list, 123 struct list_head *head) 124 { 125 __list_del(list->prev, list->next); 126 list_add_tail(list, head); 127 } 128 129 /** 130 * list_empty - tests whether a list is empty 131 * @head: the list to test. 132 */ list_empty(struct list_head * head)133 static inline int list_empty(struct list_head *head) 134 { 135 return head->next == head; 136 } 137 __list_splice(struct list_head * list,struct list_head * head)138 static inline void __list_splice(struct list_head *list, 139 struct list_head *head) 140 { 141 struct list_head *first = list->next; 142 struct list_head *last = list->prev; 143 struct list_head *at = head->next; 144 145 first->prev = head; 146 head->next = first; 147 148 last->next = at; 149 at->prev = last; 150 } 151 152 /** 153 * list_splice - join two lists 154 * @list: the new list to add. 155 * @head: the place to add it in the first list. 156 */ list_splice(struct list_head * list,struct list_head * head)157 static inline void list_splice(struct list_head *list, struct list_head *head) 158 { 159 if (!list_empty(list)) 160 __list_splice(list, head); 161 } 162 163 /** 164 * list_splice_init - join two lists and reinitialise the emptied list. 165 * @list: the new list to add. 166 * @head: the place to add it in the first list. 167 * 168 * The list at @list is reinitialised 169 */ list_splice_init(struct list_head * list,struct list_head * head)170 static inline void list_splice_init(struct list_head *list, 171 struct list_head *head) 172 { 173 if (!list_empty(list)) { 174 __list_splice(list, head); 175 INIT_LIST_HEAD(list); 176 } 177 } 178 179 /** 180 * list_entry - get the struct for this entry 181 * @ptr: the &struct list_head pointer. 182 * @type: the type of the struct this is embedded in. 183 * @member: the name of the list_struct within the struct. 184 */ 185 #define list_entry(ptr, type, member) \ 186 ((type *)((char *)(ptr)-(uintptr_t)(&((type *)0)->member))) 187 188 /** 189 * list_for_each - iterate over a list 190 * @pos: the &struct list_head to use as a loop counter. 191 * @head: the head for your list. 192 */ 193 #define list_for_each(pos, head) \ 194 for (pos = (head)->next; pos != (head); \ 195 pos = pos->next) 196 /** 197 * list_for_each_prev - iterate over a list backwards 198 * @pos: the &struct list_head to use as a loop counter. 199 * @head: the head for your list. 200 */ 201 #define list_for_each_prev(pos, head) \ 202 for (pos = (head)->prev; pos != (head); \ 203 pos = pos->prev) 204 205 /** 206 * list_for_each_safe - iterate over a list safe against removal of list entry 207 * @pos: the &struct list_head to use as a loop counter. 208 * @n: another &struct list_head to use as temporary storage 209 * @head: the head for your list. 210 */ 211 #define list_for_each_safe(pos, n, head) \ 212 for (pos = (head)->next, n = pos->next; pos != (head); \ 213 pos = n, n = pos->next) 214 215 /** 216 * list_for_each_entry - iterate over list of given type 217 * @pos: the type * to use as a loop counter. 218 * @head: the head for your list. 219 * @member: the name of the list_struct within the struct. 220 */ 221 #define list_for_each_entry(pos, head, member) \ 222 for (pos = list_entry((head)->next, typeof(*pos), member); \ 223 &pos->member != (head); \ 224 pos = list_entry(pos->member.next, typeof(*pos), member)) 225 226 /** 227 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 228 * @pos: the type * to use as a loop counter. 229 * @n: another type * to use as temporary storage 230 * @head: the head for your list. 231 * @member: the name of the list_struct within the struct. 232 */ 233 #define list_for_each_entry_safe(pos, n, head, member) \ 234 for (pos = list_entry((head)->next, typeof(*pos), member), \ 235 n = list_entry(pos->member.next, typeof(*pos), member); \ 236 &pos->member != (head); \ 237 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 238 239 /** 240 * list_for_each_entry_continue - iterate over list of given type 241 * continuing after existing point 242 * @pos: the type * to use as a loop counter. 243 * @head: the head for your list. 244 * @member: the name of the list_struct within the struct. 245 */ 246 #define list_for_each_entry_continue(pos, head, member) \ 247 for (pos = list_entry(pos->member.next, typeof(*pos), member); \ 248 &pos->member != (head); \ 249 pos = list_entry(pos->member.next, typeof(*pos), member)) 250 251 #endif 252