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