1 
2 /*
3  * Simple list implementation mostly take from the Linux Kernel
4  */
5 
6 #ifndef _LIST_H_
7 #define _LIST_H_
8 
9 struct list_head {
10 	struct list_head *next, *prev;
11 };
12 
13 void INIT_LIST_HEAD(struct list_head *list);
14 void __list_add(struct list_head *new, struct list_head *prev,
15 		struct list_head *next);
16 void __list_del(struct list_head *prev, struct list_head *next);
17 void list_add(struct list_head *new, struct list_head *head);
18 void list_add_tail(struct list_head *new, struct list_head *head);
19 void list_del(struct list_head *entry);
20 void list_replace(struct list_head *old, struct list_head *new);
21 
22 #define LIST_HEAD_INIT(name) { &(name), &(name) }
23 
24 #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
25 
26 #define list_for_each(pos, head) \
27 	for (pos = (head)->next; pos->next, pos != (head); pos = pos->next)
28 
29 #define offsetof(type, member) ((int) &((type *)0)->member)
30 
31 #define container_of(ptr, type, member) ({ \
32 	const typeof(((type *)0)->member) *__mptr = (ptr); \
33 	(type *)((char *)__mptr - offsetof(type, member)); })
34 
35 #define list_entry(ptr, type, member) \
36 	container_of(ptr, type, member)
37 
38 #define list_for_each_entry(pos, head, member) \
39 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
40 	     pos->member.next, &pos->member != (head); 	\
41 	     pos = list_entry(pos->member.next, typeof(*pos), member))
42 
43 #define list_for_each_entry_safe(pos, n, head, member)			\
44 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
45 		n = list_entry(pos->member.next, typeof(*pos), member);	\
46 	     &pos->member != (head); 					\
47 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
48 
49 #define list_for_each_safe(pos, n, head) \
50 	for (pos = (head)->next, n = pos->next; pos != (head); \
51 		pos = n, n = pos->next)
52 
53 #endif
54