Commit | Line | Data |
---|---|---|
d3d3857f MJ |
1 | // SPDX-FileCopyrightText: 2002 Free Software Foundation, Inc. |
2 | // SPDX-FileCopyrightText: 2009 Pierre-Marc Fournier | |
3 | // SPDX-FileCopyrightText: 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | // | |
5 | // SPDX-License-Identifier: LGPL-2.1-or-later | |
6 | ||
a00718e7 | 7 | /* |
a00718e7 MD |
8 | * (originally part of the GNU C Library) |
9 | * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
a00718e7 | 10 | */ |
7a50dcf7 | 11 | |
34cfb3e3 MD |
12 | #ifndef _CDS_LIST_H |
13 | #define _CDS_LIST_H 1 | |
7a50dcf7 | 14 | |
c7f4490a MD |
15 | #include <urcu/compiler.h> |
16 | ||
e6decba5 MD |
17 | /* |
18 | * The definitions of this file are adopted from those which can be | |
19 | * found in the Linux kernel headers to enable people familiar with the | |
20 | * latter find their way in these sources as well. | |
21 | */ | |
7a50dcf7 | 22 | |
e6decba5 MD |
23 | /* Basic type for the double-link list. */ |
24 | struct cds_list_head { | |
25 | struct cds_list_head *next, *prev; | |
34cfb3e3 | 26 | }; |
7a50dcf7 | 27 | |
e6decba5 | 28 | /* Define a variable with the head and tail of the list. */ |
16aa9ee8 | 29 | #define CDS_LIST_HEAD(name) \ |
e6decba5 | 30 | struct cds_list_head name = { &(name), &(name) } |
7a50dcf7 | 31 | |
e6decba5 | 32 | /* Initialize a new list head. */ |
16aa9ee8 | 33 | #define CDS_INIT_LIST_HEAD(ptr) \ |
e6decba5 | 34 | (ptr)->next = (ptr)->prev = (ptr) |
7a50dcf7 | 35 | |
ce4a1f76 | 36 | #define CDS_LIST_HEAD_INIT(name) { .next = &(name), .prev = &(name) } |
7a50dcf7 | 37 | |
e6decba5 MD |
38 | /* Add new element at the head of the list. */ |
39 | static inline | |
40 | void cds_list_add(struct cds_list_head *newp, struct cds_list_head *head) | |
7a50dcf7 | 41 | { |
e6decba5 MD |
42 | head->next->prev = newp; |
43 | newp->next = head->next; | |
44 | newp->prev = head; | |
45 | head->next = newp; | |
7a50dcf7 MD |
46 | } |
47 | ||
e6decba5 MD |
48 | /* Add new element at the tail of the list. */ |
49 | static inline | |
50 | void cds_list_add_tail(struct cds_list_head *newp, struct cds_list_head *head) | |
7a50dcf7 | 51 | { |
e6decba5 MD |
52 | head->prev->next = newp; |
53 | newp->next = head; | |
54 | newp->prev = head->prev; | |
55 | head->prev = newp; | |
7a50dcf7 MD |
56 | } |
57 | ||
e6decba5 MD |
58 | /* Remove element from list. */ |
59 | static inline | |
60 | void __cds_list_del(struct cds_list_head *prev, struct cds_list_head *next) | |
63ff4873 | 61 | { |
e6decba5 MD |
62 | next->prev = prev; |
63 | prev->next = next; | |
63ff4873 MD |
64 | } |
65 | ||
e6decba5 MD |
66 | /* Remove element from list. */ |
67 | static inline | |
68 | void cds_list_del(struct cds_list_head *elem) | |
7a50dcf7 | 69 | { |
e6decba5 | 70 | __cds_list_del(elem->prev, elem->next); |
7a50dcf7 MD |
71 | } |
72 | ||
3ec07d9f | 73 | /* Remove element from list, initializing the element's list pointers. */ |
e6decba5 MD |
74 | static inline |
75 | void cds_list_del_init(struct cds_list_head *elem) | |
3ec07d9f PM |
76 | { |
77 | cds_list_del(elem); | |
78 | CDS_INIT_LIST_HEAD(elem); | |
79 | } | |
80 | ||
e6decba5 MD |
81 | /* Delete from list, add to another list as head. */ |
82 | static inline | |
83 | void cds_list_move(struct cds_list_head *elem, struct cds_list_head *head) | |
ac956d00 | 84 | { |
e6decba5 MD |
85 | __cds_list_del(elem->prev, elem->next); |
86 | cds_list_add(elem, head); | |
ac956d00 | 87 | } |
7a50dcf7 | 88 | |
e6decba5 MD |
89 | /* Replace an old entry. */ |
90 | static inline | |
91 | void cds_list_replace(struct cds_list_head *old, struct cds_list_head *_new) | |
5db941e8 MD |
92 | { |
93 | _new->next = old->next; | |
94 | _new->prev = old->prev; | |
95 | _new->prev->next = _new; | |
96 | _new->next->prev = _new; | |
97 | } | |
98 | ||
e6decba5 MD |
99 | /* Join two lists. */ |
100 | static inline | |
101 | void cds_list_splice(struct cds_list_head *add, struct cds_list_head *head) | |
7a50dcf7 | 102 | { |
e6decba5 MD |
103 | /* Do nothing if the list which gets added is empty. */ |
104 | if (add != add->next) { | |
105 | add->next->prev = head; | |
106 | add->prev->next = head->next; | |
107 | head->next->prev = add->prev; | |
108 | head->next = add->next; | |
109 | } | |
7a50dcf7 MD |
110 | } |
111 | ||
e6decba5 | 112 | /* Get typed element from list at a given position. */ |
c7f4490a | 113 | #define cds_list_entry(ptr, type, member) caa_container_of(ptr, type, member) |
7a50dcf7 MD |
114 | |
115 | ||
58090b34 | 116 | /* Get first entry from a list. */ |
78654362 MD |
117 | #define cds_list_first_entry(ptr, type, member) \ |
118 | cds_list_entry((ptr)->next, type, member) | |
58090b34 | 119 | |
e6decba5 | 120 | /* Iterate forward over the elements of the list. */ |
16aa9ee8 | 121 | #define cds_list_for_each(pos, head) \ |
c7f4490a | 122 | for (pos = (head)->next; (pos) != (head); pos = (pos)->next) |
7a50dcf7 | 123 | |
e6decba5 MD |
124 | /* |
125 | * Iterate forward over the elements list. The list elements can be | |
126 | * removed from the list while doing this. | |
127 | */ | |
7e5b9a4d | 128 | #define cds_list_for_each_safe(pos, p, head) \ |
c7f4490a MD |
129 | for (pos = (head)->next, p = (pos)->next; \ |
130 | (pos) != (head); \ | |
131 | pos = (p), p = (pos)->next) | |
7a50dcf7 | 132 | |
e6decba5 | 133 | /* Iterate backward over the elements of the list. */ |
16aa9ee8 | 134 | #define cds_list_for_each_prev(pos, head) \ |
c7f4490a | 135 | for (pos = (head)->prev; (pos) != (head); pos = (pos)->prev) |
7a50dcf7 | 136 | |
e6decba5 MD |
137 | /* |
138 | * Iterate backwards over the elements list. The list elements can be | |
139 | * removed from the list while doing this. | |
140 | */ | |
16aa9ee8 | 141 | #define cds_list_for_each_prev_safe(pos, p, head) \ |
c7f4490a MD |
142 | for (pos = (head)->prev, p = (pos)->prev; \ |
143 | (pos) != (head); \ | |
144 | pos = (p), p = (pos)->prev) | |
7a50dcf7 | 145 | |
e6decba5 | 146 | #define cds_list_for_each_entry(pos, head, member) \ |
c7f4490a MD |
147 | for (pos = cds_list_entry((head)->next, __typeof__(*(pos)), member); \ |
148 | &(pos)->member != (head); \ | |
149 | pos = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member)) | |
7a50dcf7 | 150 | |
e6decba5 | 151 | #define cds_list_for_each_entry_reverse(pos, head, member) \ |
c7f4490a MD |
152 | for (pos = cds_list_entry((head)->prev, __typeof__(*(pos)), member); \ |
153 | &(pos)->member != (head); \ | |
154 | pos = cds_list_entry((pos)->member.prev, __typeof__(*(pos)), member)) | |
7a50dcf7 | 155 | |
e6decba5 | 156 | #define cds_list_for_each_entry_safe(pos, p, head, member) \ |
c7f4490a MD |
157 | for (pos = cds_list_entry((head)->next, __typeof__(*(pos)), member), \ |
158 | p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member); \ | |
159 | &(pos)->member != (head); \ | |
160 | pos = (p), p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member)) | |
7a50dcf7 | 161 | |
16d8b5df JL |
162 | /* |
163 | * Same as cds_list_for_each_entry_safe, but starts from "pos" which should | |
164 | * point to an entry within the list. | |
165 | */ | |
166 | #define cds_list_for_each_entry_safe_from(pos, p, head, member) \ | |
c7f4490a MD |
167 | for (p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member); \ |
168 | &(pos)->member != (head); \ | |
169 | pos = (p), p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member)) | |
16d8b5df | 170 | |
e6decba5 MD |
171 | static inline |
172 | int cds_list_empty(struct cds_list_head *head) | |
7a50dcf7 MD |
173 | { |
174 | return head == head->next; | |
175 | } | |
176 | ||
e6decba5 MD |
177 | static inline |
178 | void cds_list_replace_init(struct cds_list_head *old, | |
179 | struct cds_list_head *_new) | |
7a50dcf7 | 180 | { |
34cfb3e3 | 181 | struct cds_list_head *head = old->next; |
e6decba5 | 182 | |
16aa9ee8 DG |
183 | cds_list_del(old); |
184 | cds_list_add_tail(_new, head); | |
185 | CDS_INIT_LIST_HEAD(old); | |
7a50dcf7 MD |
186 | } |
187 | ||
34cfb3e3 | 188 | #endif /* _CDS_LIST_H */ |