1*0a9064fbSMasahiro Yamada #ifndef LIST_H 2*0a9064fbSMasahiro Yamada #define LIST_H 3*0a9064fbSMasahiro Yamada 4*0a9064fbSMasahiro Yamada /* 5*0a9064fbSMasahiro Yamada * Copied from include/linux/... 6*0a9064fbSMasahiro Yamada */ 7*0a9064fbSMasahiro Yamada 8*0a9064fbSMasahiro Yamada #undef offsetof 9*0a9064fbSMasahiro Yamada #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 10*0a9064fbSMasahiro Yamada 11*0a9064fbSMasahiro Yamada /** 12*0a9064fbSMasahiro Yamada * container_of - cast a member of a structure out to the containing structure 13*0a9064fbSMasahiro Yamada * @ptr: the pointer to the member. 14*0a9064fbSMasahiro Yamada * @type: the type of the container struct this is embedded in. 15*0a9064fbSMasahiro Yamada * @member: the name of the member within the struct. 16*0a9064fbSMasahiro Yamada * 17*0a9064fbSMasahiro Yamada */ 18*0a9064fbSMasahiro Yamada #define container_of(ptr, type, member) ({ \ 19*0a9064fbSMasahiro Yamada const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 20*0a9064fbSMasahiro Yamada (type *)( (char *)__mptr - offsetof(type,member) );}) 21*0a9064fbSMasahiro Yamada 22*0a9064fbSMasahiro Yamada 23*0a9064fbSMasahiro Yamada struct list_head { 24*0a9064fbSMasahiro Yamada struct list_head *next, *prev; 25*0a9064fbSMasahiro Yamada }; 26*0a9064fbSMasahiro Yamada 27*0a9064fbSMasahiro Yamada 28*0a9064fbSMasahiro Yamada #define LIST_HEAD_INIT(name) { &(name), &(name) } 29*0a9064fbSMasahiro Yamada 30*0a9064fbSMasahiro Yamada #define LIST_HEAD(name) \ 31*0a9064fbSMasahiro Yamada struct list_head name = LIST_HEAD_INIT(name) 32*0a9064fbSMasahiro Yamada 33*0a9064fbSMasahiro Yamada /** 34*0a9064fbSMasahiro Yamada * list_entry - get the struct for this entry 35*0a9064fbSMasahiro Yamada * @ptr: the &struct list_head pointer. 36*0a9064fbSMasahiro Yamada * @type: the type of the struct this is embedded in. 37*0a9064fbSMasahiro Yamada * @member: the name of the list_struct within the struct. 38*0a9064fbSMasahiro Yamada */ 39*0a9064fbSMasahiro Yamada #define list_entry(ptr, type, member) \ 40*0a9064fbSMasahiro Yamada container_of(ptr, type, member) 41*0a9064fbSMasahiro Yamada 42*0a9064fbSMasahiro Yamada /** 43*0a9064fbSMasahiro Yamada * list_for_each_entry - iterate over list of given type 44*0a9064fbSMasahiro Yamada * @pos: the type * to use as a loop cursor. 45*0a9064fbSMasahiro Yamada * @head: the head for your list. 46*0a9064fbSMasahiro Yamada * @member: the name of the list_struct within the struct. 47*0a9064fbSMasahiro Yamada */ 48*0a9064fbSMasahiro Yamada #define list_for_each_entry(pos, head, member) \ 49*0a9064fbSMasahiro Yamada for (pos = list_entry((head)->next, typeof(*pos), member); \ 50*0a9064fbSMasahiro Yamada &pos->member != (head); \ 51*0a9064fbSMasahiro Yamada pos = list_entry(pos->member.next, typeof(*pos), member)) 52*0a9064fbSMasahiro Yamada 53*0a9064fbSMasahiro Yamada /** 54*0a9064fbSMasahiro Yamada * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 55*0a9064fbSMasahiro Yamada * @pos: the type * to use as a loop cursor. 56*0a9064fbSMasahiro Yamada * @n: another type * to use as temporary storage 57*0a9064fbSMasahiro Yamada * @head: the head for your list. 58*0a9064fbSMasahiro Yamada * @member: the name of the list_struct within the struct. 59*0a9064fbSMasahiro Yamada */ 60*0a9064fbSMasahiro Yamada #define list_for_each_entry_safe(pos, n, head, member) \ 61*0a9064fbSMasahiro Yamada for (pos = list_entry((head)->next, typeof(*pos), member), \ 62*0a9064fbSMasahiro Yamada n = list_entry(pos->member.next, typeof(*pos), member); \ 63*0a9064fbSMasahiro Yamada &pos->member != (head); \ 64*0a9064fbSMasahiro Yamada pos = n, n = list_entry(n->member.next, typeof(*n), member)) 65*0a9064fbSMasahiro Yamada 66*0a9064fbSMasahiro Yamada /** 67*0a9064fbSMasahiro Yamada * list_empty - tests whether a list is empty 68*0a9064fbSMasahiro Yamada * @head: the list to test. 69*0a9064fbSMasahiro Yamada */ 70*0a9064fbSMasahiro Yamada static inline int list_empty(const struct list_head *head) 71*0a9064fbSMasahiro Yamada { 72*0a9064fbSMasahiro Yamada return head->next == head; 73*0a9064fbSMasahiro Yamada } 74*0a9064fbSMasahiro Yamada 75*0a9064fbSMasahiro Yamada /* 76*0a9064fbSMasahiro Yamada * Insert a new entry between two known consecutive entries. 77*0a9064fbSMasahiro Yamada * 78*0a9064fbSMasahiro Yamada * This is only for internal list manipulation where we know 79*0a9064fbSMasahiro Yamada * the prev/next entries already! 80*0a9064fbSMasahiro Yamada */ 81*0a9064fbSMasahiro Yamada static inline void __list_add(struct list_head *_new, 82*0a9064fbSMasahiro Yamada struct list_head *prev, 83*0a9064fbSMasahiro Yamada struct list_head *next) 84*0a9064fbSMasahiro Yamada { 85*0a9064fbSMasahiro Yamada next->prev = _new; 86*0a9064fbSMasahiro Yamada _new->next = next; 87*0a9064fbSMasahiro Yamada _new->prev = prev; 88*0a9064fbSMasahiro Yamada prev->next = _new; 89*0a9064fbSMasahiro Yamada } 90*0a9064fbSMasahiro Yamada 91*0a9064fbSMasahiro Yamada /** 92*0a9064fbSMasahiro Yamada * list_add_tail - add a new entry 93*0a9064fbSMasahiro Yamada * @new: new entry to be added 94*0a9064fbSMasahiro Yamada * @head: list head to add it before 95*0a9064fbSMasahiro Yamada * 96*0a9064fbSMasahiro Yamada * Insert a new entry before the specified head. 97*0a9064fbSMasahiro Yamada * This is useful for implementing queues. 98*0a9064fbSMasahiro Yamada */ 99*0a9064fbSMasahiro Yamada static inline void list_add_tail(struct list_head *_new, struct list_head *head) 100*0a9064fbSMasahiro Yamada { 101*0a9064fbSMasahiro Yamada __list_add(_new, head->prev, head); 102*0a9064fbSMasahiro Yamada } 103*0a9064fbSMasahiro Yamada 104*0a9064fbSMasahiro Yamada /* 105*0a9064fbSMasahiro Yamada * Delete a list entry by making the prev/next entries 106*0a9064fbSMasahiro Yamada * point to each other. 107*0a9064fbSMasahiro Yamada * 108*0a9064fbSMasahiro Yamada * This is only for internal list manipulation where we know 109*0a9064fbSMasahiro Yamada * the prev/next entries already! 110*0a9064fbSMasahiro Yamada */ 111*0a9064fbSMasahiro Yamada static inline void __list_del(struct list_head *prev, struct list_head *next) 112*0a9064fbSMasahiro Yamada { 113*0a9064fbSMasahiro Yamada next->prev = prev; 114*0a9064fbSMasahiro Yamada prev->next = next; 115*0a9064fbSMasahiro Yamada } 116*0a9064fbSMasahiro Yamada 117*0a9064fbSMasahiro Yamada #define LIST_POISON1 ((void *) 0x00100100) 118*0a9064fbSMasahiro Yamada #define LIST_POISON2 ((void *) 0x00200200) 119*0a9064fbSMasahiro Yamada /** 120*0a9064fbSMasahiro Yamada * list_del - deletes entry from list. 121*0a9064fbSMasahiro Yamada * @entry: the element to delete from the list. 122*0a9064fbSMasahiro Yamada * Note: list_empty() on entry does not return true after this, the entry is 123*0a9064fbSMasahiro Yamada * in an undefined state. 124*0a9064fbSMasahiro Yamada */ 125*0a9064fbSMasahiro Yamada static inline void list_del(struct list_head *entry) 126*0a9064fbSMasahiro Yamada { 127*0a9064fbSMasahiro Yamada __list_del(entry->prev, entry->next); 128*0a9064fbSMasahiro Yamada entry->next = (struct list_head*)LIST_POISON1; 129*0a9064fbSMasahiro Yamada entry->prev = (struct list_head*)LIST_POISON2; 130*0a9064fbSMasahiro Yamada } 131*0a9064fbSMasahiro Yamada #endif 132