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