1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3 /* Taken from Linux kernel code, but de-kernelized for userspace. */
4 #include <stddef.h>
5
6 #undef LIST_HEAD_INIT
7 #undef LIST_HEAD
8 #undef INIT_LIST_HEAD
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 #define container_of(ptr, type, member) ({ \
19 typeof( ((type *)0)->member ) *__mptr = (ptr); \
20 (type *)( (char *)__mptr - offsetof(type,member) );})
21
22 /*
23 * Simple doubly linked list implementation.
24 *
25 * Some of the internal functions ("__xxx") are useful when
26 * manipulating whole lists rather than single entries, as
27 * sometimes we already know the next/prev entries and we can
28 * generate better code by using them directly rather than
29 * using the generic single-entry routines.
30 */
31
32 struct list_head {
33 struct list_head *next, *prev;
34 };
35
36 #define LIST_HEAD_INIT(name) { &(name), &(name) }
37
38 #define LIST_HEAD(name) \
39 struct list_head name = LIST_HEAD_INIT(name)
40
41 #define INIT_LIST_HEAD(ptr) do { \
42 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
43 } while (0)
44
45 #define list_top(head, type, member) \
46 ({ \
47 struct list_head *_head = (head); \
48 list_empty(_head) ? NULL : list_entry(_head->next, type, member); \
49 })
50
51 /*
52 * Insert a new entry between two known consecutive entries.
53 *
54 * This is only for internal list manipulation where we know
55 * the prev/next entries already!
56 */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)57 static inline void __list_add(struct list_head *new,
58 struct list_head *prev,
59 struct list_head *next)
60 {
61 next->prev = new;
62 new->next = next;
63 new->prev = prev;
64 prev->next = new;
65 }
66
67 /**
68 * list_add - add a new entry
69 * @new: new entry to be added
70 * @head: list head to add it after
71 *
72 * Insert a new entry after the specified head.
73 * This is good for implementing stacks.
74 */
list_add(struct list_head * new,struct list_head * head)75 static inline void list_add(struct list_head *new, struct list_head *head)
76 {
77 __list_add(new, head, head->next);
78 }
79
80 /**
81 * list_add_tail - add a new entry
82 * @new: new entry to be added
83 * @head: list head to add it before
84 *
85 * Insert a new entry before the specified head.
86 * This is useful for implementing queues.
87 */
list_add_tail(struct list_head * new,struct list_head * head)88 static inline void list_add_tail(struct list_head *new, struct list_head *head)
89 {
90 __list_add(new, head->prev, head);
91 }
92
93 /*
94 * Insert a new entry between two known consecutive entries.
95 *
96 * This is only for internal list manipulation where we know
97 * the prev/next entries already!
98 */
__list_add_rcu(struct list_head * new,struct list_head * prev,struct list_head * next)99 static __inline__ void __list_add_rcu(struct list_head * new,
100 struct list_head * prev,
101 struct list_head * next)
102 {
103 new->next = next;
104 new->prev = prev;
105 next->prev = new;
106 prev->next = new;
107 }
108
109 /**
110 * list_add_rcu - add a new entry to rcu-protected list
111 * @new: new entry to be added
112 * @head: list head to add it after
113 *
114 * Insert a new entry after the specified head.
115 * This is good for implementing stacks.
116 */
list_add_rcu(struct list_head * new,struct list_head * head)117 static __inline__ void list_add_rcu(struct list_head *new, struct list_head *head)
118 {
119 __list_add_rcu(new, head, head->next);
120 }
121
122 /**
123 * list_add_tail_rcu - add a new entry to rcu-protected list
124 * @new: new entry to be added
125 * @head: list head to add it before
126 *
127 * Insert a new entry before the specified head.
128 * This is useful for implementing queues.
129 */
list_add_tail_rcu(struct list_head * new,struct list_head * head)130 static __inline__ void list_add_tail_rcu(struct list_head *new, struct list_head *head)
131 {
132 __list_add_rcu(new, head->prev, head);
133 }
134
135 /*
136 * Delete a list entry by making the prev/next entries
137 * point to each other.
138 *
139 * This is only for internal list manipulation where we know
140 * the prev/next entries already!
141 */
__list_del(struct list_head * prev,struct list_head * next)142 static inline void __list_del(struct list_head * prev, struct list_head * next)
143 {
144 next->prev = prev;
145 prev->next = next;
146 }
147
148 /**
149 * list_del - deletes entry from list.
150 * @entry: the element to delete from the list.
151 * Note: list_empty on entry does not return true after this, the entry is
152 * in an undefined state.
153 */
list_del(struct list_head * entry)154 static inline void list_del(struct list_head *entry)
155 {
156 __list_del(entry->prev, entry->next);
157 entry->next = LIST_POISON1;
158 entry->prev = LIST_POISON2;
159 }
160
161 /**
162 * list_del_rcu - deletes entry from list without re-initialization
163 * @entry: the element to delete from the list.
164 *
165 * Note: list_empty on entry does not return true after this,
166 * the entry is in an undefined state. It is useful for RCU based
167 * lockfree traversal.
168 *
169 * In particular, it means that we can not poison the forward
170 * pointers that may still be used for walking the list.
171 */
list_del_rcu(struct list_head * entry)172 static inline void list_del_rcu(struct list_head *entry)
173 {
174 __list_del(entry->prev, entry->next);
175 entry->prev = LIST_POISON2;
176 }
177
178 /**
179 * list_del_init - deletes entry from list and reinitialize it.
180 * @entry: the element to delete from the list.
181 */
list_del_init(struct list_head * entry)182 static inline void list_del_init(struct list_head *entry)
183 {
184 __list_del(entry->prev, entry->next);
185 INIT_LIST_HEAD(entry);
186 }
187
188 /**
189 * list_move - delete from one list and add as another's head
190 * @list: the entry to move
191 * @head: the head that will precede our entry
192 */
list_move(struct list_head * list,struct list_head * head)193 static inline void list_move(struct list_head *list, struct list_head *head)
194 {
195 __list_del(list->prev, list->next);
196 list_add(list, head);
197 }
198
199 /**
200 * list_move_tail - delete from one list and add as another's tail
201 * @list: the entry to move
202 * @head: the head that will follow our entry
203 */
list_move_tail(struct list_head * list,struct list_head * head)204 static inline void list_move_tail(struct list_head *list,
205 struct list_head *head)
206 {
207 __list_del(list->prev, list->next);
208 list_add_tail(list, head);
209 }
210
211 /**
212 * list_empty - tests whether a list is empty
213 * @head: the list to test.
214 */
list_empty(struct list_head * head)215 static inline int list_empty(struct list_head *head)
216 {
217 return head->next == head;
218 }
219
__list_splice(struct list_head * list,struct list_head * head)220 static inline void __list_splice(struct list_head *list,
221 struct list_head *head)
222 {
223 struct list_head *first = list->next;
224 struct list_head *last = list->prev;
225 struct list_head *at = head->next;
226
227 first->prev = head;
228 head->next = first;
229
230 last->next = at;
231 at->prev = last;
232 }
233
234 /**
235 * list_splice - join two lists
236 * @list: the new list to add.
237 * @head: the place to add it in the first list.
238 */
list_splice(struct list_head * list,struct list_head * head)239 static inline void list_splice(struct list_head *list, struct list_head *head)
240 {
241 if (!list_empty(list))
242 __list_splice(list, head);
243 }
244
245 /**
246 * list_splice_init - join two lists and reinitialise the emptied list.
247 * @list: the new list to add.
248 * @head: the place to add it in the first list.
249 *
250 * The list at @list is reinitialised
251 */
list_splice_init(struct list_head * list,struct list_head * head)252 static inline void list_splice_init(struct list_head *list,
253 struct list_head *head)
254 {
255 if (!list_empty(list)) {
256 __list_splice(list, head);
257 INIT_LIST_HEAD(list);
258 }
259 }
260
261 /**
262 * list_entry - get the struct for this entry
263 * @ptr: the &struct list_head pointer.
264 * @type: the type of the struct this is embedded in.
265 * @member: the name of the list_struct within the struct.
266 */
267 #define list_entry(ptr, type, member) \
268 container_of(ptr, type, member)
269
270 /**
271 * list_for_each - iterate over a list
272 * @pos: the &struct list_head to use as a loop counter.
273 * @head: the head for your list.
274 */
275 #define list_for_each(pos, head) \
276 for (pos = (head)->next; pos != (head); pos = pos->next)
277
278 /**
279 * list_for_each_prev - iterate over a list backwards
280 * @pos: the &struct list_head to use as a loop counter.
281 * @head: the head for your list.
282 */
283 #define list_for_each_prev(pos, head) \
284 for (pos = (head)->prev; pos != (head); pos = pos->prev)
285
286 /**
287 * list_for_each_safe - iterate over a list safe against removal of list entry
288 * @pos: the &struct list_head to use as a loop counter.
289 * @n: another &struct list_head to use as temporary storage
290 * @head: the head for your list.
291 */
292 #define list_for_each_safe(pos, n, head) \
293 for (pos = (head)->next, n = pos->next; pos != (head); \
294 pos = n, n = pos->next)
295
296 /**
297 * list_for_each_entry - iterate over list of given type
298 * @pos: the type * to use as a loop counter.
299 * @head: the head for your list.
300 * @member: the name of the list_struct within the struct.
301 */
302 #define list_for_each_entry(pos, head, member) \
303 for (pos = list_entry((head)->next, typeof(*pos), member); \
304 &pos->member != (head); \
305 pos = list_entry(pos->member.next, typeof(*pos), member))
306
307 /**
308 * list_for_each_entry_reverse - iterate backwards over list of given type.
309 * @pos: the type * to use as a loop counter.
310 * @head: the head for your list.
311 * @member: the name of the list_struct within the struct.
312 */
313 #define list_for_each_entry_reverse(pos, head, member) \
314 for (pos = list_entry((head)->prev, typeof(*pos), member); \
315 &pos->member != (head); \
316 pos = list_entry(pos->member.prev, typeof(*pos), member))
317
318
319 /**
320 * list_for_each_entry_continue - iterate over list of given type
321 * continuing after existing point
322 * @pos: the type * to use as a loop counter.
323 * @head: the head for your list.
324 * @member: the name of the list_struct within the struct.
325 */
326 #define list_for_each_entry_continue(pos, head, member) \
327 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
328 &pos->member != (head); \
329 pos = list_entry(pos->member.next, typeof(*pos), member))
330
331 /**
332 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
333 * @pos: the type * to use as a loop counter.
334 * @n: another type * to use as temporary storage
335 * @head: the head for your list.
336 * @member: the name of the list_struct within the struct.
337 */
338 #define list_for_each_entry_safe(pos, n, head, member) \
339 for (pos = list_entry((head)->next, typeof(*pos), member), \
340 n = list_entry(pos->member.next, typeof(*pos), member); \
341 &pos->member != (head); \
342 pos = n, n = list_entry(n->member.next, typeof(*n), member))
343
344
345 /*
346 * Double linked lists with a single pointer list head.
347 * Mostly useful for hash tables where the two pointer list head is
348 * too wasteful.
349 * You lose the ability to access the tail in O(1).
350 */
351
352 struct hlist_head {
353 struct hlist_node *first;
354 };
355
356 struct hlist_node {
357 struct hlist_node *next, **pprev;
358 };
359
360 #define HLIST_HEAD_INIT { .first = NULL }
361 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
362 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
363 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
364
hlist_unhashed(struct hlist_node * h)365 static __inline__ int hlist_unhashed(struct hlist_node *h)
366 {
367 return !h->pprev;
368 }
369
hlist_empty(struct hlist_head * h)370 static __inline__ int hlist_empty(struct hlist_head *h)
371 {
372 return !h->first;
373 }
374
__hlist_del(struct hlist_node * n)375 static __inline__ void __hlist_del(struct hlist_node *n)
376 {
377 struct hlist_node *next = n->next;
378 struct hlist_node **pprev = n->pprev;
379 *pprev = next;
380 if (next)
381 next->pprev = pprev;
382 }
383
hlist_del(struct hlist_node * n)384 static __inline__ void hlist_del(struct hlist_node *n)
385 {
386 __hlist_del(n);
387 n->next = LIST_POISON1;
388 n->pprev = LIST_POISON2;
389 }
390
391 /**
392 * hlist_del_rcu - deletes entry from hash list without re-initialization
393 * @entry: the element to delete from the hash list.
394 *
395 * Note: list_unhashed() on entry does not return true after this,
396 * the entry is in an undefined state. It is useful for RCU based
397 * lockfree traversal.
398 *
399 * In particular, it means that we can not poison the forward
400 * pointers that may still be used for walking the hash list.
401 */
hlist_del_rcu(struct hlist_node * n)402 static inline void hlist_del_rcu(struct hlist_node *n)
403 {
404 __hlist_del(n);
405 n->pprev = LIST_POISON2;
406 }
407
hlist_del_init(struct hlist_node * n)408 static __inline__ void hlist_del_init(struct hlist_node *n)
409 {
410 if (n->pprev) {
411 __hlist_del(n);
412 INIT_HLIST_NODE(n);
413 }
414 }
415
416 #define hlist_del_rcu_init hlist_del_init
417
hlist_add_head(struct hlist_node * n,struct hlist_head * h)418 static __inline__ void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
419 {
420 struct hlist_node *first = h->first;
421 n->next = first;
422 if (first)
423 first->pprev = &n->next;
424 h->first = n;
425 n->pprev = &h->first;
426 }
427
hlist_add_head_rcu(struct hlist_node * n,struct hlist_head * h)428 static __inline__ void hlist_add_head_rcu(struct hlist_node *n, struct hlist_head *h)
429 {
430 struct hlist_node *first = h->first;
431 n->next = first;
432 n->pprev = &h->first;
433 if (first)
434 first->pprev = &n->next;
435 h->first = n;
436 }
437
438 /* next must be != NULL */
hlist_add_before(struct hlist_node * n,struct hlist_node * next)439 static __inline__ void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
440 {
441 n->pprev = next->pprev;
442 n->next = next;
443 next->pprev = &n->next;
444 *(n->pprev) = n;
445 }
446
hlist_add_after(struct hlist_node * n,struct hlist_node * next)447 static __inline__ void hlist_add_after(struct hlist_node *n,
448 struct hlist_node *next)
449 {
450 next->next = n->next;
451 *(next->pprev) = n;
452 n->next = next;
453 }
454
455 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
456
457 /* Cannot easily do prefetch unfortunately */
458 #define hlist_for_each(pos, head) \
459 for (pos = (head)->first; pos; pos = pos->next)
460
461 #define hlist_for_each_safe(pos, n, head) \
462 for (pos = (head)->first; n = pos ? pos->next : 0, pos; \
463 pos = n)
464
465 /**
466 * hlist_for_each_entry - iterate over list of given type
467 * @tpos: the type * to use as a loop counter.
468 * @pos: the &struct hlist_node to use as a loop counter.
469 * @head: the head for your list.
470 * @member: the name of the hlist_node within the struct.
471 */
472 #define hlist_for_each_entry(tpos, pos, head, member) \
473 for (pos = (head)->first; \
474 pos && ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
475 pos = pos->next)
476
477 /**
478 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
479 * @tpos: the type * to use as a loop counter.
480 * @pos: the &struct hlist_node to use as a loop counter.
481 * @member: the name of the hlist_node within the struct.
482 */
483 #define hlist_for_each_entry_continue(tpos, pos, member) \
484 for (pos = (pos)->next; \
485 pos && ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
486 pos = pos->next)
487
488 /**
489 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
490 * @tpos: the type * to use as a loop counter.
491 * @pos: the &struct hlist_node to use as a loop counter.
492 * @member: the name of the hlist_node within the struct.
493 */
494 #define hlist_for_each_entry_from(tpos, pos, member) \
495 for (; pos && ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
496 pos = pos->next)
497
498 /**
499 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
500 * @tpos: the type * to use as a loop counter.
501 * @pos: the &struct hlist_node to use as a loop counter.
502 * @n: another &struct hlist_node to use as temporary storage
503 * @head: the head for your list.
504 * @member: the name of the hlist_node within the struct.
505 */
506 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
507 for (pos = (head)->first; \
508 pos && ({ n = pos->next; 1; }) && \
509 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
510 pos = n)
511
512 #endif
513