2 * Copyright (C) 2002 Free Software Foundation, Inc.
3 * (originally part of the GNU C Library)
4 * Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
6 * Copyright (C) 2009 Pierre-Marc Fournier
7 * Conversion to RCU list.
8 * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 * The definitions of this file are adopted from those which can be
30 * found in the Linux kernel headers to enable people familiar with the
31 * latter find their way in these sources as well.
34 /* Basic type for the double-link list. */
35 struct cds_list_head
{
36 struct cds_list_head
*next
, *prev
;
39 /* Define a variable with the head and tail of the list. */
40 #define CDS_LIST_HEAD(name) \
41 struct cds_list_head name = { &(name), &(name) }
43 /* Initialize a new list head. */
44 #define CDS_INIT_LIST_HEAD(ptr) \
45 (ptr)->next = (ptr)->prev = (ptr)
47 #define CDS_LIST_HEAD_INIT(name) { .prev = &(name), .next = &(name) }
49 /* Add new element at the head of the list. */
51 void cds_list_add(struct cds_list_head
*newp
, struct cds_list_head
*head
)
53 head
->next
->prev
= newp
;
54 newp
->next
= head
->next
;
59 /* Add new element at the tail of the list. */
61 void cds_list_add_tail(struct cds_list_head
*newp
, struct cds_list_head
*head
)
63 head
->prev
->next
= newp
;
65 newp
->prev
= head
->prev
;
69 /* Remove element from list. */
71 void __cds_list_del(struct cds_list_head
*prev
, struct cds_list_head
*next
)
77 /* Remove element from list. */
79 void cds_list_del(struct cds_list_head
*elem
)
81 __cds_list_del(elem
->prev
, elem
->next
);
84 /* Remove element from list, initializing the element's list pointers. */
86 void cds_list_del_init(struct cds_list_head
*elem
)
89 CDS_INIT_LIST_HEAD(elem
);
92 /* Delete from list, add to another list as head. */
94 void cds_list_move(struct cds_list_head
*elem
, struct cds_list_head
*head
)
96 __cds_list_del(elem
->prev
, elem
->next
);
97 cds_list_add(elem
, head
);
100 /* Replace an old entry. */
102 void cds_list_replace(struct cds_list_head
*old
, struct cds_list_head
*_new
)
104 _new
->next
= old
->next
;
105 _new
->prev
= old
->prev
;
106 _new
->prev
->next
= _new
;
107 _new
->next
->prev
= _new
;
110 /* Join two lists. */
112 void cds_list_splice(struct cds_list_head
*add
, struct cds_list_head
*head
)
114 /* Do nothing if the list which gets added is empty. */
115 if (add
!= add
->next
) {
116 add
->next
->prev
= head
;
117 add
->prev
->next
= head
->next
;
118 head
->next
->prev
= add
->prev
;
119 head
->next
= add
->next
;
123 /* Get typed element from list at a given position. */
124 #define cds_list_entry(ptr, type, member) \
125 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
128 /* Get first entry from a list. */
129 #define cds_list_first_entry(ptr, type, member) \
130 cds_list_entry((ptr)->next, type, member)
132 /* Iterate forward over the elements of the list. */
133 #define cds_list_for_each(pos, head) \
134 for (pos = (head)->next; pos != (head); pos = pos->next)
137 * Iterate forward over the elements list. The list elements can be
138 * removed from the list while doing this.
140 #define cds_list_for_each_safe(pos, p, head) \
141 for (pos = (head)->next, p = pos->next; \
143 pos = p, p = pos->next)
145 /* Iterate backward over the elements of the list. */
146 #define cds_list_for_each_prev(pos, head) \
147 for (pos = (head)->prev; pos != (head); pos = pos->prev)
150 * Iterate backwards over the elements list. The list elements can be
151 * removed from the list while doing this.
153 #define cds_list_for_each_prev_safe(pos, p, head) \
154 for (pos = (head)->prev, p = pos->prev; \
156 pos = p, p = pos->prev)
158 #define cds_list_for_each_entry(pos, head, member) \
159 for (pos = cds_list_entry((head)->next, __typeof__(*pos), member); \
160 &pos->member != (head); \
161 pos = cds_list_entry(pos->member.next, __typeof__(*pos), member))
163 #define cds_list_for_each_entry_reverse(pos, head, member) \
164 for (pos = cds_list_entry((head)->prev, __typeof__(*pos), member); \
165 &pos->member != (head); \
166 pos = cds_list_entry(pos->member.prev, __typeof__(*pos), member))
168 #define cds_list_for_each_entry_safe(pos, p, head, member) \
169 for (pos = cds_list_entry((head)->next, __typeof__(*pos), member), \
170 p = cds_list_entry(pos->member.next, __typeof__(*pos), member); \
171 &pos->member != (head); \
172 pos = p, p = cds_list_entry(pos->member.next, __typeof__(*pos), member))
175 * Same as cds_list_for_each_entry_safe, but starts from "pos" which should
176 * point to an entry within the list.
178 #define cds_list_for_each_entry_safe_from(pos, p, head, member) \
179 for (p = cds_list_entry(pos->member.next, __typeof__(*pos), member); \
180 &pos->member != (head); \
181 pos = p, p = cds_list_entry(pos->member.next, __typeof__(*pos), member))
184 int cds_list_empty(struct cds_list_head
*head
)
186 return head
== head
->next
;
190 void cds_list_replace_init(struct cds_list_head
*old
,
191 struct cds_list_head
*_new
)
193 struct cds_list_head
*head
= old
->next
;
196 cds_list_add_tail(_new
, head
);
197 CDS_INIT_LIST_HEAD(old
);
200 #endif /* _CDS_LIST_H */
This page took 0.036851 seconds and 4 git commands to generate.