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>
5 // SPDX-License-Identifier: LGPL-2.1-or-later
8 * (originally part of the GNU C Library)
9 * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
15 #include <urcu/compiler.h>
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.
23 /* Basic type for the double-link list. */
24 struct cds_list_head
{
25 struct cds_list_head
*next
, *prev
;
28 /* Define a variable with the head and tail of the list. */
29 #define CDS_LIST_HEAD(name) \
30 struct cds_list_head name = { &(name), &(name) }
32 /* Initialize a new list head. */
33 #define CDS_INIT_LIST_HEAD(ptr) \
34 (ptr)->next = (ptr)->prev = (ptr)
36 #define CDS_LIST_HEAD_INIT(name) { .next = &(name), .prev = &(name) }
38 /* Add new element at the head of the list. */
40 void cds_list_add(struct cds_list_head
*newp
, struct cds_list_head
*head
)
42 head
->next
->prev
= newp
;
43 newp
->next
= head
->next
;
48 /* Add new element at the tail of the list. */
50 void cds_list_add_tail(struct cds_list_head
*newp
, struct cds_list_head
*head
)
52 head
->prev
->next
= newp
;
54 newp
->prev
= head
->prev
;
58 /* Remove element from list. */
60 void __cds_list_del(struct cds_list_head
*prev
, struct cds_list_head
*next
)
66 /* Remove element from list. */
68 void cds_list_del(struct cds_list_head
*elem
)
70 __cds_list_del(elem
->prev
, elem
->next
);
73 /* Remove element from list, initializing the element's list pointers. */
75 void cds_list_del_init(struct cds_list_head
*elem
)
78 CDS_INIT_LIST_HEAD(elem
);
81 /* Delete from list, add to another list as head. */
83 void cds_list_move(struct cds_list_head
*elem
, struct cds_list_head
*head
)
85 __cds_list_del(elem
->prev
, elem
->next
);
86 cds_list_add(elem
, head
);
89 /* Replace an old entry. */
91 void cds_list_replace(struct cds_list_head
*old
, struct cds_list_head
*_new
)
93 _new
->next
= old
->next
;
94 _new
->prev
= old
->prev
;
95 _new
->prev
->next
= _new
;
96 _new
->next
->prev
= _new
;
101 void cds_list_splice(struct cds_list_head
*add
, struct cds_list_head
*head
)
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
;
112 /* Get typed element from list at a given position. */
113 #define cds_list_entry(ptr, type, member) caa_container_of(ptr, type, member)
116 /* Get first entry from a list. */
117 #define cds_list_first_entry(ptr, type, member) \
118 cds_list_entry((ptr)->next, type, member)
120 /* Iterate forward over the elements of the list. */
121 #define cds_list_for_each(pos, head) \
122 for (pos = (head)->next; (pos) != (head); pos = (pos)->next)
125 * Iterate forward over the elements list. The list elements can be
126 * removed from the list while doing this.
128 #define cds_list_for_each_safe(pos, p, head) \
129 for (pos = (head)->next, p = (pos)->next; \
131 pos = (p), p = (pos)->next)
133 /* Iterate backward over the elements of the list. */
134 #define cds_list_for_each_prev(pos, head) \
135 for (pos = (head)->prev; (pos) != (head); pos = (pos)->prev)
138 * Iterate backwards over the elements list. The list elements can be
139 * removed from the list while doing this.
141 #define cds_list_for_each_prev_safe(pos, p, head) \
142 for (pos = (head)->prev, p = (pos)->prev; \
144 pos = (p), p = (pos)->prev)
146 #define cds_list_for_each_entry(pos, head, member) \
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))
151 #define cds_list_for_each_entry_reverse(pos, head, member) \
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))
156 #define cds_list_for_each_entry_safe(pos, p, head, member) \
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))
163 * Same as cds_list_for_each_entry_safe, but starts from "pos" which should
164 * point to an entry within the list.
166 #define cds_list_for_each_entry_safe_from(pos, p, head, member) \
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))
172 int cds_list_empty(struct cds_list_head
*head
)
174 return head
== head
->next
;
178 void cds_list_replace_init(struct cds_list_head
*old
,
179 struct cds_list_head
*_new
)
181 struct cds_list_head
*head
= old
->next
;
184 cds_list_add_tail(_new
, head
);
185 CDS_INIT_LIST_HEAD(old
);
188 #endif /* _CDS_LIST_H */
This page took 0.032469 seconds and 4 git commands to generate.