]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/hlist.h
Merge pull request #4236 from mihalicyn/github_check_fixes
[mirror_lxc.git] / src / lxc / hlist.h
CommitLineData
4780b5e7
CB
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#ifndef __LXC_HLIST_H
4#define __LXC_HLIST_H
5
6struct list_head {
7 struct list_head *next, *prev;
8};
9
10struct hlist_head {
11 struct hlist_node *first;
12};
13
14struct hlist_node {
15 struct hlist_node *next, **pprev;
16};
17
18/*
19 * These are non-NULL pointers that will result in page faults
20 * under normal circumstances, used to verify that nobody uses
21 * non-initialized list entries.
22 */
23#define LIST_POISON1 ((void *) 0x100)
24#define LIST_POISON2 ((void *) 0x122)
25
26/*
27 * Circular doubly linked list implementation.
28 *
29 * Some of the internal functions ("__xxx") are useful when
30 * manipulating whole lists rather than single entries, as
31 * sometimes we already know the next/prev entries and we can
32 * generate better code by using them directly rather than
33 * using the generic single-entry routines.
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/**
42 * INIT_LIST_HEAD - Initialize a list_head structure
43 * @list: list_head structure to be initialized.
44 *
45 * Initializes the list_head to point to itself. If it is a list header,
46 * the result is an empty list.
47 */
48static inline void INIT_LIST_HEAD(struct list_head *list)
49{
50 list->next = list;
51 list->prev = list;
52}
53
54/*
55 * Insert a new entry between two known consecutive entries.
56 *
57 * This is only for internal list manipulation where we know
58 * the prev/next entries already!
59 */
60static inline void __list_add(struct list_head *new,
61 struct list_head *prev,
62 struct list_head *next)
63{
64 next->prev = new;
65 new->next = next;
66 new->prev = prev;
67 prev->next = new;
68}
69
70/**
71 * list_add - add a new entry
72 * @new: new entry to be added
73 * @head: list head to add it after
74 *
75 * Insert a new entry after the specified head.
76 * This is good for implementing stacks.
77 */
78static inline void list_add(struct list_head *new, struct list_head *head)
79{
80 __list_add(new, head, head->next);
81}
82
83
84/**
85 * list_add_tail - add a new entry
86 * @new: new entry to be added
87 * @head: list head to add it before
88 *
89 * Insert a new entry before the specified head.
90 * This is useful for implementing queues.
91 */
92static inline void list_add_tail(struct list_head *new, struct list_head *head)
93{
94 __list_add(new, head->prev, head);
95}
96
97/*
98 * Delete a list entry by making the prev/next entries
99 * point to each other.
100 *
101 * This is only for internal list manipulation where we know
102 * the prev/next entries already!
103 */
104static inline void __list_del(struct list_head * prev, struct list_head * next)
105{
106 next->prev = prev;
107 prev->next = next;
108}
109
110static inline void __list_del_entry(struct list_head *entry)
111{
112 __list_del(entry->prev, entry->next);
113}
114
115/**
116 * list_del - deletes entry from list.
117 * @entry: the element to delete from the list.
118 * Note: list_empty() on entry does not return true after this, the entry is
119 * in an undefined state.
120 */
121static inline void list_del(struct list_head *entry)
122{
123 __list_del_entry(entry);
124 entry->next = LIST_POISON1;
125 entry->prev = LIST_POISON2;
126}
127
128/**
129 * list_replace - replace old entry by new one
130 * @old : the element to be replaced
131 * @new : the new element to insert
132 *
133 * If @old was empty, it will be overwritten.
134 */
135static inline void list_replace(struct list_head *old,
136 struct list_head *new)
137{
138 new->next = old->next;
139 new->next->prev = new;
140 new->prev = old->prev;
141 new->prev->next = new;
142}
143
144/**
145 * list_replace_init - replace old entry by new one and initialize the old one
146 * @old : the element to be replaced
147 * @new : the new element to insert
148 *
149 * If @old was empty, it will be overwritten.
150 */
151static inline void list_replace_init(struct list_head *old,
152 struct list_head *new)
153{
154 list_replace(old, new);
155 INIT_LIST_HEAD(old);
156}
157
158/**
159 * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
160 * @entry1: the location to place entry2
161 * @entry2: the location to place entry1
162 */
163static inline void list_swap(struct list_head *entry1,
164 struct list_head *entry2)
165{
166 struct list_head *pos = entry2->prev;
167
168 list_del(entry2);
169 list_replace(entry1, entry2);
170 if (pos == entry1)
171 pos = entry2;
172 list_add(entry1, pos);
173}
174
175/**
176 * list_del_init - deletes entry from list and reinitialize it.
177 * @entry: the element to delete from the list.
178 */
179static inline void list_del_init(struct list_head *entry)
180{
181 __list_del_entry(entry);
182 INIT_LIST_HEAD(entry);
183}
184
185/**
186 * list_move - delete from one list and add as another's head
187 * @list: the entry to move
188 * @head: the head that will precede our entry
189 */
190static inline void list_move(struct list_head *list, struct list_head *head)
191{
192 __list_del_entry(list);
193 list_add(list, head);
194}
195
196/**
197 * list_move_tail - delete from one list and add as another's tail
198 * @list: the entry to move
199 * @head: the head that will follow our entry
200 */
201static inline void list_move_tail(struct list_head *list,
202 struct list_head *head)
203{
204 __list_del_entry(list);
205 list_add_tail(list, head);
206}
207
208/**
209 * list_bulk_move_tail - move a subsection of a list to its tail
210 * @head: the head that will follow our entry
211 * @first: first entry to move
212 * @last: last entry to move, can be the same as first
213 *
214 * Move all entries between @first and including @last before @head.
215 * All three entries must belong to the same linked list.
216 */
217static inline void list_bulk_move_tail(struct list_head *head,
218 struct list_head *first,
219 struct list_head *last)
220{
221 first->prev->next = last->next;
222 last->next->prev = first->prev;
223
224 head->prev->next = first;
225 first->prev = head->prev;
226
227 last->next = head;
228 head->prev = last;
229}
230
231/**
232 * list_is_first -- tests whether @list is the first entry in list @head
233 * @list: the entry to test
234 * @head: the head of the list
235 */
236static inline int list_is_first(const struct list_head *list,
237 const struct list_head *head)
238{
239 return list->prev == head;
240}
241
242/**
243 * list_is_last - tests whether @list is the last entry in list @head
244 * @list: the entry to test
245 * @head: the head of the list
246 */
247static inline int list_is_last(const struct list_head *list,
248 const struct list_head *head)
249{
250 return list->next == head;
251}
252
253/**
254 * list_empty - tests whether a list is empty
255 * @head: the list to test.
256 */
257static inline int list_empty(const struct list_head *head)
258{
259 return head->next == head;
260}
261
262/**
263 * list_rotate_left - rotate the list to the left
264 * @head: the head of the list
265 */
266static inline void list_rotate_left(struct list_head *head)
267{
268 struct list_head *first;
269
270 if (!list_empty(head)) {
271 first = head->next;
272 list_move_tail(first, head);
273 }
274}
275
276/**
277 * list_rotate_to_front() - Rotate list to specific item.
278 * @list: The desired new front of the list.
279 * @head: The head of the list.
280 *
281 * Rotates list so that @list becomes the new front of the list.
282 */
283static inline void list_rotate_to_front(struct list_head *list,
284 struct list_head *head)
285{
286 /*
287 * Deletes the list head from the list denoted by @head and
288 * places it as the tail of @list, this effectively rotates the
289 * list so that @list is at the front.
290 */
291 list_move_tail(head, list);
292}
293
294/**
295 * list_is_singular - tests whether a list has just one entry.
296 * @head: the list to test.
297 */
298static inline int list_is_singular(const struct list_head *head)
299{
300 return !list_empty(head) && (head->next == head->prev);
301}
302
303static inline void __list_cut_position(struct list_head *list,
304 struct list_head *head, struct list_head *entry)
305{
306 struct list_head *new_first = entry->next;
307 list->next = head->next;
308 list->next->prev = list;
309 list->prev = entry;
310 entry->next = list;
311 head->next = new_first;
312 new_first->prev = head;
313}
314
315/**
316 * list_cut_position - cut a list into two
317 * @list: a new list to add all removed entries
318 * @head: a list with entries
319 * @entry: an entry within head, could be the head itself
320 * and if so we won't cut the list
321 *
322 * This helper moves the initial part of @head, up to and
323 * including @entry, from @head to @list. You should
324 * pass on @entry an element you know is on @head. @list
325 * should be an empty list or a list you do not care about
326 * losing its data.
327 *
328 */
329static inline void list_cut_position(struct list_head *list,
330 struct list_head *head, struct list_head *entry)
331{
332 if (list_empty(head))
333 return;
334 if (list_is_singular(head) &&
335 (head->next != entry && head != entry))
336 return;
337 if (entry == head)
338 INIT_LIST_HEAD(list);
339 else
340 __list_cut_position(list, head, entry);
341}
342
343/**
344 * list_cut_before - cut a list into two, before given entry
345 * @list: a new list to add all removed entries
346 * @head: a list with entries
347 * @entry: an entry within head, could be the head itself
348 *
349 * This helper moves the initial part of @head, up to but
350 * excluding @entry, from @head to @list. You should pass
351 * in @entry an element you know is on @head. @list should
352 * be an empty list or a list you do not care about losing
353 * its data.
354 * If @entry == @head, all entries on @head are moved to
355 * @list.
356 */
357static inline void list_cut_before(struct list_head *list,
358 struct list_head *head,
359 struct list_head *entry)
360{
361 if (head->next == entry) {
362 INIT_LIST_HEAD(list);
363 return;
364 }
365 list->next = head->next;
366 list->next->prev = list;
367 list->prev = entry->prev;
368 list->prev->next = list;
369 head->next = entry;
370 entry->prev = head;
371}
372
373static inline void __list_splice(const struct list_head *list,
374 struct list_head *prev,
375 struct list_head *next)
376{
377 struct list_head *first = list->next;
378 struct list_head *last = list->prev;
379
380 first->prev = prev;
381 prev->next = first;
382
383 last->next = next;
384 next->prev = last;
385}
386
387/**
388 * list_splice - join two lists, this is designed for stacks
389 * @list: the new list to add.
390 * @head: the place to add it in the first list.
391 */
392static inline void list_splice(const struct list_head *list,
393 struct list_head *head)
394{
395 if (!list_empty(list))
396 __list_splice(list, head, head->next);
397}
398
399/**
400 * list_splice_tail - join two lists, each list being a queue
401 * @list: the new list to add.
402 * @head: the place to add it in the first list.
403 */
404static inline void list_splice_tail(struct list_head *list,
405 struct list_head *head)
406{
407 if (!list_empty(list))
408 __list_splice(list, head->prev, head);
409}
410
411/**
412 * list_splice_init - join two lists and reinitialise the emptied list.
413 * @list: the new list to add.
414 * @head: the place to add it in the first list.
415 *
416 * The list at @list is reinitialised
417 */
418static inline void list_splice_init(struct list_head *list,
419 struct list_head *head)
420{
421 if (!list_empty(list)) {
422 __list_splice(list, head, head->next);
423 INIT_LIST_HEAD(list);
424 }
425}
426
427/**
428 * list_splice_tail_init - join two lists and reinitialise the emptied list
429 * @list: the new list to add.
430 * @head: the place to add it in the first list.
431 *
432 * Each of the lists is a queue.
433 * The list at @list is reinitialised
434 */
435static inline void list_splice_tail_init(struct list_head *list,
436 struct list_head *head)
437{
438 if (!list_empty(list)) {
439 __list_splice(list, head->prev, head);
440 INIT_LIST_HEAD(list);
441 }
442}
443
444/**
445 * list_entry - get the struct for this entry
446 * @ptr: the &struct list_head pointer.
447 * @type: the type of the struct this is embedded in.
448 * @member: the name of the list_head within the struct.
449 */
450#define list_entry(ptr, type, member) \
451 container_of(ptr, type, member)
452
453/**
454 * list_first_entry - get the first element from a list
455 * @ptr: the list head to take the element from.
456 * @type: the type of the struct this is embedded in.
457 * @member: the name of the list_head within the struct.
458 *
459 * Note, that list is expected to be not empty.
460 */
461#define list_first_entry(ptr, type, member) \
462 list_entry((ptr)->next, type, member)
463
464/**
465 * list_last_entry - get the last element from a list
466 * @ptr: the list head to take the element from.
467 * @type: the type of the struct this is embedded in.
468 * @member: the name of the list_head within the struct.
469 *
470 * Note, that list is expected to be not empty.
471 */
472#define list_last_entry(ptr, type, member) \
473 list_entry((ptr)->prev, type, member)
474
475/**
476 * list_first_entry_or_null - get the first element from a list
477 * @ptr: the list head to take the element from.
478 * @type: the type of the struct this is embedded in.
479 * @member: the name of the list_head within the struct.
480 *
481 * Note that if the list is empty, it returns NULL.
482 */
483#define list_first_entry_or_null(ptr, type, member) ({ \
484 struct list_head *head__ = (ptr); \
485 struct list_head *pos__ = head__->next; \
486 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
487})
488
489/**
490 * list_next_entry - get the next element in list
491 * @pos: the type * to cursor
492 * @member: the name of the list_head within the struct.
493 */
494#define list_next_entry(pos, member) \
495 list_entry((pos)->member.next, typeof(*(pos)), member)
496
497/**
498 * list_prev_entry - get the prev element in list
499 * @pos: the type * to cursor
500 * @member: the name of the list_head within the struct.
501 */
502#define list_prev_entry(pos, member) \
503 list_entry((pos)->member.prev, typeof(*(pos)), member)
504
505/**
506 * list_for_each - iterate over a list
507 * @pos: the &struct list_head to use as a loop cursor.
508 * @head: the head for your list.
509 */
510#define list_for_each(pos, head) \
511 for (pos = (head)->next; pos != (head); pos = pos->next)
512
513/**
514 * list_for_each_continue - continue iteration over a list
515 * @pos: the &struct list_head to use as a loop cursor.
516 * @head: the head for your list.
517 *
518 * Continue to iterate over a list, continuing after the current position.
519 */
520#define list_for_each_continue(pos, head) \
521 for (pos = pos->next; pos != (head); pos = pos->next)
522
523/**
524 * list_for_each_prev - iterate over a list backwards
525 * @pos: the &struct list_head to use as a loop cursor.
526 * @head: the head for your list.
527 */
528#define list_for_each_prev(pos, head) \
529 for (pos = (head)->prev; pos != (head); pos = pos->prev)
530
531/**
532 * list_for_each_safe - iterate over a list safe against removal of list entry
533 * @pos: the &struct list_head to use as a loop cursor.
534 * @n: another &struct list_head to use as temporary storage
535 * @head: the head for your list.
536 */
537#define list_for_each_safe(pos, n, head) \
538 for (pos = (head)->next, n = pos->next; pos != (head); \
539 pos = n, n = pos->next)
540
541/**
542 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
543 * @pos: the &struct list_head to use as a loop cursor.
544 * @n: another &struct list_head to use as temporary storage
545 * @head: the head for your list.
546 */
547#define list_for_each_prev_safe(pos, n, head) \
548 for (pos = (head)->prev, n = pos->prev; \
549 pos != (head); \
550 pos = n, n = pos->prev)
551
552/**
553 * list_entry_is_head - test if the entry points to the head of the list
554 * @pos: the type * to cursor
555 * @head: the head for your list.
556 * @member: the name of the list_head within the struct.
557 */
558#define list_entry_is_head(pos, head, member) \
559 (&pos->member == (head))
560
561/**
562 * list_for_each_entry - iterate over list of given type
563 * @pos: the type * to use as a loop cursor.
564 * @head: the head for your list.
565 * @member: the name of the list_head within the struct.
566 */
567#define list_for_each_entry(pos, head, member) \
568 for (pos = list_first_entry(head, typeof(*pos), member); \
569 !list_entry_is_head(pos, head, member); \
570 pos = list_next_entry(pos, member))
571
572/**
573 * list_for_each_entry_reverse - iterate backwards over list of given type.
574 * @pos: the type * to use as a loop cursor.
575 * @head: the head for your list.
576 * @member: the name of the list_head within the struct.
577 */
578#define list_for_each_entry_reverse(pos, head, member) \
579 for (pos = list_last_entry(head, typeof(*pos), member); \
580 !list_entry_is_head(pos, head, member); \
581 pos = list_prev_entry(pos, member))
582
583/**
584 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
585 * @pos: the type * to use as a start point
586 * @head: the head of the list
587 * @member: the name of the list_head within the struct.
588 *
589 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
590 */
591#define list_prepare_entry(pos, head, member) \
592 ((pos) ? : list_entry(head, typeof(*pos), member))
593
594/**
595 * list_for_each_entry_continue - continue iteration over list of given type
596 * @pos: the type * to use as a loop cursor.
597 * @head: the head for your list.
598 * @member: the name of the list_head within the struct.
599 *
600 * Continue to iterate over list of given type, continuing after
601 * the current position.
602 */
603#define list_for_each_entry_continue(pos, head, member) \
604 for (pos = list_next_entry(pos, member); \
605 !list_entry_is_head(pos, head, member); \
606 pos = list_next_entry(pos, member))
607
608/**
609 * list_for_each_entry_continue_reverse - iterate backwards from the given point
610 * @pos: the type * to use as a loop cursor.
611 * @head: the head for your list.
612 * @member: the name of the list_head within the struct.
613 *
614 * Start to iterate over list of given type backwards, continuing after
615 * the current position.
616 */
617#define list_for_each_entry_continue_reverse(pos, head, member) \
618 for (pos = list_prev_entry(pos, member); \
619 !list_entry_is_head(pos, head, member); \
620 pos = list_prev_entry(pos, member))
621
622/**
623 * list_for_each_entry_from - iterate over list of given type from the current point
624 * @pos: the type * to use as a loop cursor.
625 * @head: the head for your list.
626 * @member: the name of the list_head within the struct.
627 *
628 * Iterate over list of given type, continuing from current position.
629 */
630#define list_for_each_entry_from(pos, head, member) \
631 for (; !list_entry_is_head(pos, head, member); \
632 pos = list_next_entry(pos, member))
633
634/**
635 * list_for_each_entry_from_reverse - iterate backwards over list of given type
636 * from the current point
637 * @pos: the type * to use as a loop cursor.
638 * @head: the head for your list.
639 * @member: the name of the list_head within the struct.
640 *
641 * Iterate backwards over list of given type, continuing from current position.
642 */
643#define list_for_each_entry_from_reverse(pos, head, member) \
644 for (; !list_entry_is_head(pos, head, member); \
645 pos = list_prev_entry(pos, member))
646
647/**
648 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
649 * @pos: the type * to use as a loop cursor.
650 * @n: another type * to use as temporary storage
651 * @head: the head for your list.
652 * @member: the name of the list_head within the struct.
653 */
654#define list_for_each_entry_safe(pos, n, head, member) \
655 for (pos = list_first_entry(head, typeof(*pos), member), \
656 n = list_next_entry(pos, member); \
657 !list_entry_is_head(pos, head, member); \
658 pos = n, n = list_next_entry(n, member))
659
660/**
661 * list_for_each_entry_safe_continue - continue list iteration safe against removal
662 * @pos: the type * to use as a loop cursor.
663 * @n: another type * to use as temporary storage
664 * @head: the head for your list.
665 * @member: the name of the list_head within the struct.
666 *
667 * Iterate over list of given type, continuing after current point,
668 * safe against removal of list entry.
669 */
670#define list_for_each_entry_safe_continue(pos, n, head, member) \
671 for (pos = list_next_entry(pos, member), \
672 n = list_next_entry(pos, member); \
673 !list_entry_is_head(pos, head, member); \
674 pos = n, n = list_next_entry(n, member))
675
676/**
677 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
678 * @pos: the type * to use as a loop cursor.
679 * @n: another type * to use as temporary storage
680 * @head: the head for your list.
681 * @member: the name of the list_head within the struct.
682 *
683 * Iterate over list of given type from current point, safe against
684 * removal of list entry.
685 */
686#define list_for_each_entry_safe_from(pos, n, head, member) \
687 for (n = list_next_entry(pos, member); \
688 !list_entry_is_head(pos, head, member); \
689 pos = n, n = list_next_entry(n, member))
690
691/**
692 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
693 * @pos: the type * to use as a loop cursor.
694 * @n: another type * to use as temporary storage
695 * @head: the head for your list.
696 * @member: the name of the list_head within the struct.
697 *
698 * Iterate backwards over list of given type, safe against removal
699 * of list entry.
700 */
701#define list_for_each_entry_safe_reverse(pos, n, head, member) \
702 for (pos = list_last_entry(head, typeof(*pos), member), \
703 n = list_prev_entry(pos, member); \
704 !list_entry_is_head(pos, head, member); \
705 pos = n, n = list_prev_entry(n, member))
706
707/**
708 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
709 * @pos: the loop cursor used in the list_for_each_entry_safe loop
710 * @n: temporary storage used in list_for_each_entry_safe
711 * @member: the name of the list_head within the struct.
712 *
713 * list_safe_reset_next is not safe to use in general if the list may be
714 * modified concurrently (eg. the lock is dropped in the loop body). An
715 * exception to this is if the cursor element (pos) is pinned in the list,
716 * and list_safe_reset_next is called after re-taking the lock and before
717 * completing the current iteration of the loop body.
718 */
719#define list_safe_reset_next(pos, n, member) \
720 n = list_next_entry(pos, member)
721
722/*
723 * Double linked lists with a single pointer list head.
724 * Mostly useful for hash tables where the two pointer list head is
725 * too wasteful.
726 * You lose the ability to access the tail in O(1).
727 */
728
729#define HLIST_HEAD_INIT { .first = NULL }
730#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
731#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
732static inline void INIT_HLIST_NODE(struct hlist_node *h)
733{
734 h->next = NULL;
735 h->pprev = NULL;
736}
737
738/**
739 * hlist_unhashed - Has node been removed from list and reinitialized?
740 * @h: Node to be checked
741 *
742 * Not that not all removal functions will leave a node in unhashed
743 * state. For example, hlist_nulls_del_init_rcu() does leave the
744 * node in unhashed state, but hlist_nulls_del() does not.
745 */
746static inline int hlist_unhashed(const struct hlist_node *h)
747{
748 return !h->pprev;
749}
750
751/**
752 * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
753 * @h: Node to be checked
754 */
755static inline int hlist_unhashed_lockless(const struct hlist_node *h)
756{
757 return !h->pprev;
758}
759
760/**
761 * hlist_empty - Is the specified hlist_head structure an empty hlist?
762 * @h: Structure to check.
763 */
764static inline int hlist_empty(const struct hlist_head *h)
765{
766 return !h->first;
767}
768
769static inline void __hlist_del(struct hlist_node *n)
770{
771 struct hlist_node *next = n->next;
772 struct hlist_node **pprev = n->pprev;
773
774 *pprev = next;
775 if (next)
776 next->pprev = pprev;
777}
778
779/**
780 * hlist_del - Delete the specified hlist_node from its list
781 * @n: Node to delete.
782 *
783 * Note that this function leaves the node in hashed state. Use
784 * hlist_del_init() or similar instead to unhash @n.
785 */
786static inline void hlist_del(struct hlist_node *n)
787{
788 __hlist_del(n);
789 n->next = LIST_POISON1;
790 n->pprev = LIST_POISON2;
791}
792
793/**
794 * hlist_del_init - Delete the specified hlist_node from its list and initialize
795 * @n: Node to delete.
796 *
797 * Note that this function leaves the node in unhashed state.
798 */
799static inline void hlist_del_init(struct hlist_node *n)
800{
801 if (!hlist_unhashed(n)) {
802 __hlist_del(n);
803 INIT_HLIST_NODE(n);
804 }
805}
806
807/**
808 * hlist_add_head - add a new entry at the beginning of the hlist
809 * @n: new entry to be added
810 * @h: hlist head to add it after
811 *
812 * Insert a new entry after the specified head.
813 * This is good for implementing stacks.
814 */
815static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
816{
817 struct hlist_node *first = h->first;
818 n->next = first;
819 if (first)
820 first->pprev = &n->next;
821 h->first = n;
822 n->pprev = &h->first;
823}
824
825/**
826 * hlist_add_before - add a new entry before the one specified
827 * @n: new entry to be added
828 * @next: hlist node to add it before, which must be non-NULL
829 */
830static inline void hlist_add_before(struct hlist_node *n,
831 struct hlist_node *next)
832{
833 n->pprev = next->pprev;
834 n->next = next;
835 next->pprev = &n->next;
836 *(n->pprev) = n;
837}
838
839/**
840 * hlist_add_behind - add a new entry after the one specified
841 * @n: new entry to be added
842 * @prev: hlist node to add it after, which must be non-NULL
843 */
844static inline void hlist_add_behind(struct hlist_node *n,
845 struct hlist_node *prev)
846{
847 n->next = prev->next;
848 prev->next = n;
849 n->pprev = &prev->next;
850
851 if (n->next)
852 n->next->pprev = &n->next;
853}
854
855/**
856 * hlist_add_fake - create a fake hlist consisting of a single headless node
857 * @n: Node to make a fake list out of
858 *
859 * This makes @n appear to be its own predecessor on a headless hlist.
860 * The point of this is to allow things like hlist_del() to work correctly
861 * in cases where there is no list.
862 */
863static inline void hlist_add_fake(struct hlist_node *n)
864{
865 n->pprev = &n->next;
866}
867
868/**
869 * hlist_fake: Is this node a fake hlist?
870 * @h: Node to check for being a self-referential fake hlist.
871 */
872static inline bool hlist_fake(struct hlist_node *h)
873{
874 return h->pprev == &h->next;
875}
876
877/**
878 * hlist_is_singular_node - is node the only element of the specified hlist?
879 * @n: Node to check for singularity.
880 * @h: Header for potentially singular list.
881 *
882 * Check whether the node is the only node of the head without
883 * accessing head, thus avoiding unnecessary cache misses.
884 */
885static inline bool
886hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
887{
888 return !n->next && n->pprev == &h->first;
889}
890
891/**
892 * hlist_move_list - Move an hlist
893 * @old: hlist_head for old list.
894 * @new: hlist_head for new list.
895 *
896 * Move a list from one list head to another. Fixup the pprev
897 * reference of the first entry if it exists.
898 */
899static inline void hlist_move_list(struct hlist_head *old,
900 struct hlist_head *new)
901{
902 new->first = old->first;
903 if (new->first)
904 new->first->pprev = &new->first;
905 old->first = NULL;
906}
907
908#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
909
910#define hlist_for_each(pos, head) \
911 for (pos = (head)->first; pos ; pos = pos->next)
912
913#define hlist_for_each_safe(pos, n, head) \
914 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
915 pos = n)
916
917#define hlist_entry_safe(ptr, type, member) \
918 ({ typeof(ptr) ____ptr = (ptr); \
919 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
920 })
921
922/**
923 * hlist_for_each_entry - iterate over list of given type
924 * @pos: the type * to use as a loop cursor.
925 * @head: the head for your list.
926 * @member: the name of the hlist_node within the struct.
927 */
928#define hlist_for_each_entry(pos, head, member) \
929 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
930 pos; \
931 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
932
933/**
934 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
935 * @pos: the type * to use as a loop cursor.
936 * @member: the name of the hlist_node within the struct.
937 */
938#define hlist_for_each_entry_continue(pos, member) \
939 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
940 pos; \
941 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
942
943/**
944 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
945 * @pos: the type * to use as a loop cursor.
946 * @member: the name of the hlist_node within the struct.
947 */
948#define hlist_for_each_entry_from(pos, member) \
949 for (; pos; \
950 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
951
952/**
953 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
954 * @pos: the type * to use as a loop cursor.
955 * @n: a &struct hlist_node to use as temporary storage
956 * @head: the head for your list.
957 * @member: the name of the hlist_node within the struct.
958 */
959#define hlist_for_each_entry_safe(pos, n, head, member) \
960 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
961 pos && ({ n = pos->member.next; 1; }); \
962 pos = hlist_entry_safe(n, typeof(*pos), member))
963
d696c45e
CB
964#define list_len(pos, head, member) \
965 ({ \
966 size_t __list_len__ = 0; \
967 \
968 list_for_each_entry(pos, head, member) { \
969 (__list_len__)++; \
970 } \
971 \
972 __list_len__; \
973 })
87d0990c 974
4780b5e7 975#endif /* __LXC_HLIST_H */