list.h (2456B)
1 #ifndef __LIST_H 2 #define __LIST_H 3 4 #include <kernel.h> 5 6 struct list_head { 7 struct list_head *prev; 8 struct list_head *next; 9 }; 10 11 #define LIST_HEAD_INIT(name) { &(name), &(name) } 12 #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name) 13 14 #define list_entry(ptr, type, member) container_of(ptr, type, member) 15 #define list_first_entry(head_ptr, type, member) container_of((head_ptr)->next, type, member) 16 17 /* list_for_each - iterate over a list 18 * pos: the &struct list_head to use as a loop cursor. 19 * head: the head for your list. 20 */ 21 #define list_for_each(pos, head) \ 22 for (pos = (head)->next; pos != (head); \ 23 pos = pos->next) 24 25 /* list_for_each_safe - iterate over a list safe against removal of list entry 26 * pos: the &struct list_head to use as a loop cursor. 27 * n: another &struct list_head to use as temporary storage 28 * head: the head for your list. 29 */ 30 #define list_for_each_safe(pos, n, head) \ 31 for (pos = (head)->next, n = pos->next; pos != (head); \ 32 pos = n, n = pos->next) 33 34 static inline void 35 INIT_LIST_HEAD(struct list_head *list) 36 { 37 list->prev = list; 38 list->next = list; 39 } 40 41 /* Note: list_empty() on entry does not return true after this, the entry is 42 * in an undefined state. 43 */ 44 static inline void 45 list_del(struct list_head *entry) 46 { 47 entry->prev->next = entry->next; 48 entry->next->prev = entry->prev; 49 entry->next = NULL; 50 entry->prev = NULL; 51 } 52 53 static inline void 54 list_add(struct list_head *entry, struct list_head *head) 55 { 56 entry->next = head->next; 57 entry->prev = head; 58 head->next->prev = entry; 59 head->next = entry; 60 } 61 62 static inline void 63 list_add_tail(struct list_head *entry, struct list_head *head) 64 { 65 entry->next = head; 66 entry->prev = head->prev; 67 head->prev->next = entry; 68 head->prev = entry; 69 } 70 71 static inline void 72 list_move_tail(struct list_head *entry, struct list_head *head) 73 { 74 list_del(entry); 75 list_add_tail(entry, head); 76 } 77 78 static inline int 79 list_empty(const struct list_head *head) 80 { 81 return head->next == head; 82 } 83 84 /* list_is_singular - tests whether a list has just one entry. */ 85 static inline int 86 list_is_singular(const struct list_head *head) 87 { 88 return !list_empty(head) && (head->next == head->prev); 89 } 90 91 /* list_is_last - tests whether @list is the last entry in list @head 92 * list: the entry to test 93 * head: the head of the list 94 */ 95 static inline int 96 list_is_last(const struct list_head *list, const struct list_head *head) 97 { 98 return list->next == head; 99 } 100 101 #endif /* __LIST_H */