1 #ifndef _LIST_H_
2 #define _LIST_H_
3 
4 #ifndef offsetof
5 #define offsetof(type, md) ((unsigned long)&((type *)0)->md)
6 #endif
7 
8 #ifndef container_of
9 #define container_of(ptr, type, member) \
10   ((type *)((char *)(ptr) - offsetof(type, member)))
11 #endif
12 
13 struct list_item {
14 	struct list_item *next;
15 	struct list_item *prev;
16 };
17 
18 struct list {
19 	struct list_item *head;
20 	struct list_item *tail;
21 };
22 
23 #define LIST_INIT(name) { 0, 0 }
24 
25 #define LIST(name) \
26 	struct list name = LIST_INIT(name)
27 
28 #define list_entry(ptr, type, member) \
29 	container_of(ptr, type, member)
30 
list_init(struct list * list)31 static inline void list_init(struct list *list)
32 {
33 	list->head = 0;
34 	list->tail = 0;
35 }
36 
list_append(struct list * list,struct list_item * item)37 static inline void list_append(struct list *list, struct list_item *item)
38 {
39 	item->next = 0;
40 	item->prev = list->tail;
41 	if (list->tail != 0)
42 		list->tail->next = item;
43 	else
44 		list->head = item;
45 	list->tail = item;
46 }
47 
list_prepend(struct list * list,struct list_item * item)48 static inline void list_prepend(struct list *list, struct list_item *item)
49 {
50 	item->prev = 0;
51 	item->next = list->head;
52 	if (list->head == 0)
53 		list->tail = item;
54 	list->head = item;
55 }
56 
list_insert(struct list * list,struct list_item * after,struct list_item * item)57 static inline void list_insert(struct list *list, struct list_item *after, struct list_item *item)
58 {
59 	if (after == 0) {
60 		list_prepend(list, item);
61 		return;
62 	}
63 	item->prev = after;
64 	item->next = after->next;
65 	after->next = item;
66 	if (item->next)
67 		item->next->prev = item;
68 	if (list->tail == after)
69 		list->tail = item;
70 }
71 
list_remove(struct list * list,struct list_item * item)72 static inline void list_remove(struct list *list, struct list_item *item)
73 {
74 	if (item->next)
75 		item->next->prev = item->prev;
76 	if (list->head == item) {
77 		list->head = item->next;
78 		if (list->head == 0)
79 			list->tail = 0;
80 	} else {
81 		item->prev->next = item->next;
82 		if (list->tail == item)
83 			list->tail = item->prev;
84 	}
85 	item->prev = item->next = 0;
86 }
87 
list_pop(struct list * list)88 static inline struct list_item *list_pop(struct list *list)
89 {
90 	struct list_item *item;
91 	item = list->head;
92 	if (item == 0)
93 		return 0;
94 	list_remove(list, item);
95 	return item;
96 }
97 
list_last(struct list * list)98 static inline struct list_item *list_last(struct list *list)
99 {
100 	return list->tail;
101 }
102 
list_first(struct list * list)103 static inline struct list_item *list_first(struct list *list)
104 {
105 	return list->head;
106 }
107 
108 
list_next(struct list_item * item)109 static inline struct list_item *list_next(struct list_item *item)
110 {
111 	return item->next;
112 }
113 
114 #define list_push list_append
115 
116 #define list_for_each(_list, _iter) \
117   for (_iter = (_list)->head; (_iter) != 0; _iter = (_iter)->next)
118 
119 #define list_for_each_after(_node, _iter) \
120   for (_iter = (_node)->next; (_iter) != 0; _iter = (_iter)->next)
121 
122 #define list_for_each_safe(_list, _iter, _bkup) \
123   for (_iter = (_list)->head; (_iter) != 0 && ((_bkup = (_iter)->next) || 1); _iter = (_bkup))
124 
125 #define list_for_each_safe_after(_node, _iter, _bkup) \
126   for (_iter = (_node)->next; (_iter) != 0 && ((_bkup = (_iter)->next) || 1); _iter = (_bkup))
127 
128 #endif
129