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