1 | #ifndef _LINUX_LIST_H |
---|
2 | #define _LINUX_LIST_H |
---|
3 | |
---|
4 | #include <stddef.h> |
---|
5 | |
---|
6 | #define container_of(ptr, type, member) ({ \ |
---|
7 | typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
---|
8 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
---|
9 | |
---|
10 | /* |
---|
11 | * These are non-NULL pointers that will result in page faults |
---|
12 | * under normal circumstances, used to verify that nobody uses |
---|
13 | * non-initialized list entries. |
---|
14 | */ |
---|
15 | #define LIST_POISON1 ((void *) 0x00100100) |
---|
16 | #define LIST_POISON2 ((void *) 0x00200200) |
---|
17 | |
---|
18 | /* |
---|
19 | * Simple doubly linked list implementation. |
---|
20 | * |
---|
21 | * Some of the internal functions ("__xxx") are useful when |
---|
22 | * manipulating whole lists rather than single entries, as |
---|
23 | * sometimes we already know the next/prev entries and we can |
---|
24 | * generate better code by using them directly rather than |
---|
25 | * using the generic single-entry routines. |
---|
26 | */ |
---|
27 | |
---|
28 | struct list_head { |
---|
29 | struct list_head *next, *prev; |
---|
30 | }; |
---|
31 | |
---|
32 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
---|
33 | |
---|
34 | #define LIST_HEAD(name) \ |
---|
35 | struct list_head name = LIST_HEAD_INIT(name) |
---|
36 | |
---|
37 | #define INIT_LIST_HEAD(ptr) do { \ |
---|
38 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
---|
39 | } while (0) |
---|
40 | |
---|
41 | /* |
---|
42 | * Insert a new entry between two known consecutive entries. |
---|
43 | * |
---|
44 | * This is only for internal list manipulation where we know |
---|
45 | * the prev/next entries already! |
---|
46 | */ |
---|
47 | static inline void __list_add(struct list_head *new, |
---|
48 | struct list_head *prev, |
---|
49 | struct list_head *next) |
---|
50 | { |
---|
51 | next->prev = new; |
---|
52 | new->next = next; |
---|
53 | new->prev = prev; |
---|
54 | prev->next = new; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * list_add - add a new entry |
---|
59 | * @new: new entry to be added |
---|
60 | * @head: list head to add it after |
---|
61 | * |
---|
62 | * Insert a new entry after the specified head. |
---|
63 | * This is good for implementing stacks. |
---|
64 | */ |
---|
65 | static inline void list_add(struct list_head *new, struct list_head *head) |
---|
66 | { |
---|
67 | __list_add(new, head, head->next); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * list_add_tail - add a new entry |
---|
72 | * @new: new entry to be added |
---|
73 | * @head: list head to add it before |
---|
74 | * |
---|
75 | * Insert a new entry before the specified head. |
---|
76 | * This is useful for implementing queues. |
---|
77 | */ |
---|
78 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
---|
79 | { |
---|
80 | __list_add(new, head->prev, head); |
---|
81 | } |
---|
82 | |
---|
83 | /* |
---|
84 | * Delete a list entry by making the prev/next entries |
---|
85 | * point to each other. |
---|
86 | * |
---|
87 | * This is only for internal list manipulation where we know |
---|
88 | * the prev/next entries already! |
---|
89 | */ |
---|
90 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
---|
91 | { |
---|
92 | next->prev = prev; |
---|
93 | prev->next = next; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * list_del - deletes entry from list. |
---|
98 | * @entry: the element to delete from the list. |
---|
99 | * Note: list_empty on entry does not return true after this, the entry is |
---|
100 | * in an undefined state. |
---|
101 | */ |
---|
102 | static inline void list_del(struct list_head *entry) |
---|
103 | { |
---|
104 | __list_del(entry->prev, entry->next); |
---|
105 | entry->next = LIST_POISON1; |
---|
106 | entry->prev = LIST_POISON2; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * list_empty - tests whether a list is empty |
---|
111 | * @head: the list to test. |
---|
112 | */ |
---|
113 | static inline int list_empty(const struct list_head *head) |
---|
114 | { |
---|
115 | return head->next == head; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * list_entry - get the struct for this entry |
---|
120 | * @ptr: the &struct list_head pointer. |
---|
121 | * @type: the type of the struct this is embedded in. |
---|
122 | * @member: the name of the list_struct within the struct. |
---|
123 | */ |
---|
124 | #define list_entry(ptr, type, member) \ |
---|
125 | container_of(ptr, type, member) |
---|
126 | |
---|
127 | /** |
---|
128 | * list_first_entry - get the first element from a list |
---|
129 | * @ptr: the list head to take the element from. |
---|
130 | * @type: the type of the struct this is embedded in. |
---|
131 | * @member: the name of the list_head within the struct. |
---|
132 | * |
---|
133 | * Note, that list is expected to be not empty. |
---|
134 | */ |
---|
135 | #define list_first_entry(ptr, type, member) \ |
---|
136 | list_entry((ptr)->next, type, member) |
---|
137 | |
---|
138 | /** |
---|
139 | * list_for_each_entry - iterate over list of given type |
---|
140 | * @pos: the type * to use as a loop counter. |
---|
141 | * @head: the head for your list. |
---|
142 | * @member: the name of the list_struct within the struct. |
---|
143 | */ |
---|
144 | #define list_for_each_entry(pos, head, member) \ |
---|
145 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
---|
146 | &pos->member != (head); \ |
---|
147 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
---|
148 | |
---|
149 | /** |
---|
150 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
---|
151 | * @pos: the type * to use as a loop counter. |
---|
152 | * @n: another type * to use as temporary storage |
---|
153 | * @head: the head for your list. |
---|
154 | * @member: the name of the list_struct within the struct. |
---|
155 | */ |
---|
156 | #define list_for_each_entry_safe(pos, n, head, member) \ |
---|
157 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
---|
158 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
---|
159 | &pos->member != (head); \ |
---|
160 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
---|
161 | |
---|
162 | #endif |
---|