Stefan Richter | 9f6d3c4 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 1 | struct list { |
Stefan Richter | 92c16f7 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 2 | struct list *next, *prev; |
Stefan Richter | 9f6d3c4 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 3 | }; |
| 4 | |
| 5 | static inline void |
| 6 | list_init(struct list *list) |
| 7 | { |
Stefan Richter | 92c16f7 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 8 | list->next = list; |
| 9 | list->prev = list; |
Stefan Richter | 9f6d3c4 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 10 | } |
| 11 | |
| 12 | static inline int |
| 13 | list_empty(struct list *list) |
| 14 | { |
Stefan Richter | 92c16f7 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 15 | return list->next == list; |
Stefan Richter | 9f6d3c4 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | static inline void |
| 19 | list_insert(struct list *link, struct list *new_link) |
| 20 | { |
Stefan Richter | 92c16f7 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 21 | new_link->prev = link->prev; |
| 22 | new_link->next = link; |
| 23 | new_link->prev->next = new_link; |
| 24 | new_link->next->prev = new_link; |
Stefan Richter | 9f6d3c4 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static inline void |
| 28 | list_append(struct list *list, struct list *new_link) |
| 29 | { |
Stefan Richter | 92c16f7 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 30 | list_insert((struct list *)list, new_link); |
Stefan Richter | 9f6d3c4 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static inline void |
| 34 | list_prepend(struct list *list, struct list *new_link) |
| 35 | { |
Stefan Richter | 92c16f7 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 36 | list_insert(list->next, new_link); |
Stefan Richter | 9f6d3c4 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static inline void |
| 40 | list_remove(struct list *link) |
| 41 | { |
Stefan Richter | 92c16f7 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 42 | link->prev->next = link->next; |
| 43 | link->next->prev = link->prev; |
Stefan Richter | 9f6d3c4 | 2010-07-22 11:58:05 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | #define list_entry(link, type, member) \ |
| 47 | ((type *)((char *)(link)-(unsigned long)(&((type *)0)->member))) |
| 48 | |
| 49 | #define list_head(list, type, member) \ |
| 50 | list_entry((list)->next, type, member) |
| 51 | |
| 52 | #define list_tail(list, type, member) \ |
| 53 | list_entry((list)->prev, type, member) |
| 54 | |
| 55 | #define list_next(elm, member) \ |
| 56 | list_entry((elm)->member.next, typeof(*elm), member) |
| 57 | |
| 58 | #define list_for_each_entry(pos, list, member) \ |
| 59 | for (pos = list_head(list, typeof(*pos), member); \ |
| 60 | &pos->member != (list); \ |
| 61 | pos = list_next(pos, member)) |
| 62 | |