1 | /* Copyright (C) 2002-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifndef _LIST_H |
20 | #define _LIST_H 1 |
21 | |
22 | /* Internal: doubly linked lists. */ |
23 | |
24 | /* The definitions of this file are adopted from those which can be |
25 | found in the Linux kernel headers to enable people familiar with |
26 | the latter find their way in these sources as well. */ |
27 | |
28 | #include <list_t.h> |
29 | #include <atomic.h> |
30 | |
31 | /* Define a variable with the head and tail of the list. */ |
32 | #define LIST_HEAD(name) \ |
33 | list_t name = { &(name), &(name) } |
34 | |
35 | /* Initialize a new list head. */ |
36 | #define INIT_LIST_HEAD(ptr) \ |
37 | (ptr)->next = (ptr)->prev = (ptr) |
38 | |
39 | |
40 | /* Add new element at the head of the list. */ |
41 | static inline void |
42 | list_add (list_t *newp, list_t *head) |
43 | { |
44 | newp->next = head->next; |
45 | newp->prev = head; |
46 | head->next->prev = newp; |
47 | atomic_write_barrier (); |
48 | head->next = newp; |
49 | } |
50 | |
51 | |
52 | /* Remove element from list. */ |
53 | static inline void |
54 | list_del (list_t *elem) |
55 | { |
56 | elem->next->prev = elem->prev; |
57 | elem->prev->next = elem->next; |
58 | } |
59 | |
60 | |
61 | /* Join two lists. */ |
62 | static inline void |
63 | list_splice (list_t *add, list_t *head) |
64 | { |
65 | /* Do nothing if the list which gets added is empty. */ |
66 | if (add != add->next) |
67 | { |
68 | add->next->prev = head; |
69 | add->prev->next = head->next; |
70 | head->next->prev = add->prev; |
71 | head->next = add->next; |
72 | } |
73 | } |
74 | |
75 | |
76 | /* Get typed element from list at a given position. */ |
77 | #define list_entry(ptr, type, member) \ |
78 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
79 | |
80 | |
81 | |
82 | /* Iterate forward over the elements of the list. */ |
83 | #define list_for_each(pos, head) \ |
84 | for (pos = (head)->next; pos != (head); pos = pos->next) |
85 | |
86 | |
87 | /* Iterate forward over the elements of the list. */ |
88 | #define list_for_each_prev(pos, head) \ |
89 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
90 | |
91 | |
92 | /* Iterate backwards over the elements list. The list elements can be |
93 | removed from the list while doing this. */ |
94 | #define list_for_each_prev_safe(pos, p, head) \ |
95 | for (pos = (head)->prev, p = pos->prev; \ |
96 | pos != (head); \ |
97 | pos = p, p = pos->prev) |
98 | |
99 | #endif /* list.h */ |
100 | |